Skip to content

Commit

Permalink
refactor(trc):split stdlib for trc,prepare for rust module define
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 5, 2024
1 parent cfa68ef commit 7980f02
Show file tree
Hide file tree
Showing 69 changed files with 1,290 additions and 1,184 deletions.
56 changes: 52 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ rustyline = { version = "14.0", features = ["with-file-history"] }
suffix_array = "0.5.0"
paste = "1"
shadow-rs = "0"
libcore = { path = "./libcore" }
collection_literals = "1"
thiserror = "1"

[build-dependencies]
shadow-rs = "0"

[profile.release]
panic = "abort"
lto = true
codegen-units = 1
strip = true

[workspace]
members = ["derive", ".", "stdlib"]
members = ["derive", ".", "stdlib", "libcore"]

[package.metadata.i18n]
# The available locales for your application, default: ["en"].
Expand Down
22 changes: 12 additions & 10 deletions derive/src/def_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,31 @@ pub fn def_impl(context: TokenStream) -> TokenStream {
.expect("name error");
}
let ret = quote!(
pub fn init() -> crate::base::stdlib::Stdlib {
use crate::base::stdlib::Stdlib;
pub fn module_init(storage: &mut ModuleStorage) -> libcore::libbasic::Module {
use libcore::libbasic::Module;
use std::collections::hash_map::HashMap;
let mut functions = HashMap::new();
let mut classes = HashMap::new();
let mut submodules = HashMap::new();
let mut consts_info = HashMap::new();
#(
functions.insert(stringify!(#right_func).to_string(), #left_func());
classes.insert(stringify!(#right_class).to_string(), #left_class::init_info(Some(storage)));
)*
#(
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));
#left_class::gen_funcs_info(storage);
#left_class::gen_overrides_info(storage);
#left_class::modify_shadow_name(storage, stringify!(#right_class));
)*
#(
submodules.insert(stringify!(#submodules).to_string(), #submodules::init());
consts_info.insert(stringify!(#consts).to_string(), #consts.to_string());
)*
#(
consts_info.insert(stringify!(#consts).to_string(), #consts.to_string());
functions.insert(stringify!(#right_func).to_string(), #left_func(storage));
)*
#(
submodules.insert(stringify!(#submodules).to_string(), #submodules::module_init(storage));
)*
Stdlib::new(
Module::new(
stringify!(#module_ident),
submodules,
functions,
Expand Down
39 changes: 17 additions & 22 deletions derive/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ use syn::{
pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, syn::Expr) {
let output = sig.output.clone();
let output: syn::Expr = match output {
ReturnType::Default => parse_str("TypeAllowNull::None").unwrap(),
ReturnType::Default => parse_str("None").unwrap(),
ReturnType::Type(_, ty) => {
if let Type::Path(name) = (*ty).clone() {
let tyname = name.path.segments[0].ident.to_string();
if tyname == "void" {
parse_str("TypeAllowNull::None").unwrap()
parse_str("None").unwrap()
} else if tyname == "any" {
parse_str("ANY_TYPE").unwrap()
} else if tyname.ends_with("str") {
parse_str(r#"TypeAllowNull::Some(crate::tvm::types::TrcStr::export_info())"#)
.unwrap()
parse_str(r#"TypeAllowNull::Some(libcore::TrcStr::export_info())"#).unwrap()
} else if tyname.ends_with("int") {
parse_str("TypeAllowNull::Some(crate::tvm::types::TrcInt::export_info())")
.unwrap()
parse_str("TypeAllowNull::Some(libcore::TrcInt::export_info())").unwrap()
} else if tyname.ends_with("bool") {
parse_str("TypeAllowNull::Some(crate::tvm::types::TrcBool::export_info())")
.unwrap()
parse_str("TypeAllowNull::Some(libcore::TrcBool::export_info())").unwrap()
} else if tyname.ends_with("char") {
parse_str("TypeAllowNull::Some(crate::tvm::types::TrcChar::export_info())")
.unwrap()
parse_str("TypeAllowNull::Some(libcore::TrcChar::export_info())").unwrap()
} else if tyname.ends_with("float") {
parse_str("TypeAllowNull::Some(crate::tvm::types::TrcFloat::export_info())")
.unwrap()
parse_str("TypeAllowNull::Some(libcore::TrcFloat::export_info())").unwrap()
} else {
panic!("error");
}
Expand All @@ -49,7 +44,7 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, s
}
let mut args_type_required = vec![];

new_args.push(parse_str::<FnArg>("dydata: &mut crate::tvm::DynaData").unwrap());
new_args.push(parse_str::<FnArg>("dydata: &mut libcore::DynaData").unwrap());
sig.inputs = new_args;
for i in &input_args {
if let FnArg::Typed(PatType { pat, ty, .. }, ..) = i {
Expand All @@ -70,25 +65,25 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, s
.unwrap(),
);
} else if typename.ends_with("str") {
args_type_required.push(parse_str("crate::tvm::TrcStr").unwrap());
args_type_required.push(parse_str("libcore::TrcStr").unwrap());
new_stmts.push(
parse_str(&format!(
r#"let mut {} = dydata.pop_data::<crate::tvm::TrcStrInternal>();"#,
r#"let mut {} = dydata.pop_data::<libcore::TrcStrInternal>();"#,
arg_name
))
.unwrap(),
);
} else if typename.ends_with("int") {
args_type_required.push(parse_str("crate::tvm::TrcInt").unwrap());
args_type_required.push(parse_str("libcore::TrcInt").unwrap());
new_stmts.push(
parse_str(&format!(
r#"let mut {} = dydata.pop_data::<crate::tvm::TrcIntInternal>();"#,
r#"let mut {} = dydata.pop_data::<libcore::TrcIntInternal>();"#,
arg_name
))
.unwrap(),
);
} else if typename.ends_with("bool") {
args_type_required.push(parse_str("crate::tvm::TrcBool").unwrap());
args_type_required.push(parse_str("libcore::TrcBool").unwrap());
new_stmts.push(
parse_str(&format!(
r#"let mut {} = dydata.pop_data::<bool>();"#,
Expand All @@ -97,19 +92,19 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, s
.unwrap(),
);
} else if typename.ends_with("char") {
args_type_required.push(parse_str("crate::tvm::TrcChar").unwrap());
args_type_required.push(parse_str("libcore::TrcChar").unwrap());
new_stmts.push(
parse_str(&format!(
r#"let mut {} = dydata.pop_data::<crate::tvm::TrcCharInternal>();"#,
r#"let mut {} = dydata.pop_data::<libcore::TrcCharInternal>();"#,
arg_name
))
.unwrap(),
);
} else if typename.ends_with("float") {
args_type_required.push(parse_str("crate::tvm::TrcFloat").unwrap());
args_type_required.push(parse_str("libcore::TrcFloat").unwrap());
new_stmts.push(
parse_str(&format!(
r#"let mut {} = dydata.pop_data::<crate::tvm::TrcFloatInternal>();"#,
r#"let mut {} = dydata.pop_data::<libcore::TrcFloatInternal>();"#,
arg_name
))
.unwrap(),
Expand Down
Loading

0 comments on commit 7980f02

Please sign in to comment.