Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Feb 7, 2024
2 parents 81548d0 + 59acad5 commit c155ad4
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 1,138 deletions.
1,000 changes: 0 additions & 1,000 deletions rust/Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ colored = "2.1.0"
downcast-rs = "1.2.0"
lazy_static = "1.4.0"
num-bigint = "0.4.4"
reqwest = { version = "0.11.24", features = ["json", "multipart"] }
tokio = { version = "1.36.0", features = ["full"] }
llvm-sys = "170.0.1"
derive = { path = "./derive" }
libloading = "0.8.1"
Expand Down
1 change: 1 addition & 0 deletions rust/derive/src/def_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
classes.insert(stringify!(#right_class).to_string(), #left_class::export_info());
#left_class::gen_funcs_info();
#left_class::gen_overrides_info();
#left_class::modify_shadow_name(stringify!(#right_class));
)*
#(
submodules.insert(stringify!(#submodules).to_string(), #submodules::init());
Expand Down
26 changes: 16 additions & 10 deletions rust/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,34 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
)*
let classid = new_class_id();
let mut ret = RustClass::new(
stringify!(#name),
"",
members,
None,
None,
classid
);
STD_CLASS_TABLE.with(|std| {
std.borrow_mut().push(ret);
});
unsafe {
STD_CLASS_TABLE.push(ret);
}
classid
}

pub fn gen_funcs_info() {
STD_CLASS_TABLE.with(|std| {
std.borrow_mut()[Self::export_info()].functions = Self::function_export();
});
unsafe {
STD_CLASS_TABLE[Self::export_info()].functions = Self::function_export();
}
}

pub fn gen_overrides_info() {
STD_CLASS_TABLE.with(|std| {
std.borrow_mut()[Self::export_info()].overrides = Self::override_export();
});
unsafe {
STD_CLASS_TABLE[Self::export_info()].overrides = Self::override_export();
}
}

pub fn modify_shadow_name(name: &'static str) {
unsafe {
STD_CLASS_TABLE[Self::export_info()].name = name;
}
}

pub fn export_info() -> usize {
Expand Down
2 changes: 1 addition & 1 deletion rust/examples/expr.trc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ test expr
*/
print(1.0+9.0)
print("hello")
#print(1+"ppp")
print(1+"ppp")
5 changes: 3 additions & 2 deletions rust/src/base/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use colored::Colorize;
use gettextrs::gettext;
use std::error::Error;
use std::fmt::{Debug, Display};
Expand Down Expand Up @@ -70,8 +71,8 @@ impl Display for RuntimeError {
{}:{}"#,
gettext!(ERROR_IN_LINE, self.content.get_line()),
gettext!(IN_MODULE, self.content.get_module_name()),
gettext(self.info.error_type.clone()),
self.info.message
gettext(self.info.error_type.clone()).red(),
self.info.message.red()
);
write!(f, "{}", s)
}
Expand Down
49 changes: 29 additions & 20 deletions rust/src/base/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ use crate::{
scope::{Type, TypeAllowNull, Var},
token::TokenType,
},
hash_map,
tvm::DynaData,
};
use downcast_rs::{impl_downcast, Downcast};
use lazy_static::lazy_static;
use std::sync::OnceLock;
use std::{
borrow::{Borrow, BorrowMut},
sync::OnceLock,
};
use std::{
cell::RefCell,
collections::HashMap,
Expand Down Expand Up @@ -97,10 +101,10 @@ pub trait ClassInterface: Downcast + Sync + Send + ClassClone + Debug + Display

fn has_attr(&self, attrname: &str) -> Option<Type>;

fn get_name(&self) -> &str;

fn get_id(&self) -> usize;

fn get_name(&self) -> &str;

fn is_any(&self) -> bool {
self.get_id() == 0
}
Expand Down Expand Up @@ -137,28 +141,28 @@ impl FunctionInterface for RustFunction {

#[derive(Debug, Clone)]
pub struct RustClass {
pub name: String,
pub members: HashMap<String, Var>,
pub functions: HashMap<String, RustFunction>,
pub overrides: HashMap<TokenType, IOType>,
pub id: usize,
pub name: &'static str,
}

/// 约定,0号id是any类型
impl RustClass {
pub fn new(
name: impl Into<String>,
name: &'static str,
members: HashMap<String, Var>,
functions: Option<HashMap<String, RustFunction>>,
overrides: Option<HashMap<TokenType, IOType>>,
id: usize,
) -> RustClass {
RustClass {
name: name.into(),
members,
functions: functions.unwrap_or_else(|| HashMap::new()),
overrides: overrides.unwrap_or_else(|| HashMap::new()),
id,
name,
}
}

Expand Down Expand Up @@ -194,7 +198,7 @@ impl ClassInterface for RustClass {
}

fn get_name(&self) -> &str {
&self.name
self.name
}

fn get_id(&self) -> usize {
Expand All @@ -215,23 +219,30 @@ impl Display for RustClass {
}
}

thread_local! {
pub static STD_FUNC_TABLE: RefCell<Vec<StdlibFunc>> = RefCell::new(vec![]);
pub static STD_CLASS_TABLE: RefCell<Vec<RustClass>> = RefCell::new(vec![]);
pub static mut STD_FUNC_TABLE: Vec<StdlibFunc> = vec![];
pub static mut STD_CLASS_TABLE: Vec<RustClass> = vec![];

pub fn get_stdlib() -> &'static Stdlib {
static INIT: OnceLock<Stdlib> = OnceLock::new();
INIT.get_or_init(|| {
let mut ret = crate::tvm::stdlib::import_stdlib();
ret
})
}

pub fn new_class_id() -> usize {
STD_CLASS_TABLE.with(|std| std.borrow().len())
unsafe { STD_CLASS_TABLE.len() }
}

impl RustFunction {
pub fn new(name: impl Into<String>, ptr: StdlibFunc, io: IOType) -> RustFunction {
let buildin_id = unsafe {
STD_FUNC_TABLE.push(ptr);
STD_FUNC_TABLE.len()
} - 1;
Self {
name: name.into(),
buildin_id: STD_FUNC_TABLE.with(|std| {
std.borrow_mut().push(ptr);
std.borrow().len()
}) - 1,
buildin_id,
ptr,
io,
}
Expand Down Expand Up @@ -282,16 +293,14 @@ impl Stdlib {
}

lazy_static! {
pub static ref ANY_TYPE: RustClass =
RustClass::new("any", HashMap::new(), None, None, new_class_id());
pub static ref STDLIB_ROOT: Stdlib = crate::tvm::stdlib::init();
pub static ref ANY_TYPE: RustClass = RustClass::new("any", HashMap::new(), None, None, 0);
}

/// 获取到标准库的类的个数,从而区分标准库和用户自定义的类
pub fn get_stdclass_end() -> usize {
static STD_NUM: OnceLock<usize> = OnceLock::new();
*STD_NUM.get_or_init(|| {
let mut num = STD_CLASS_TABLE.with(|std| std.borrow().len());
*STD_NUM.get_or_init(|| unsafe {
let mut num = STD_CLASS_TABLE.len();
num
})
}
Loading

0 comments on commit c155ad4

Please sign in to comment.