diff --git a/Cargo.lock b/Cargo.lock index d712b455..ff6732a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -723,9 +723,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -1054,9 +1054,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -1077,9 +1077,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", diff --git a/derive/src/def_module.rs b/derive/src/def_module.rs index 715e4f07..3145e88f 100644 --- a/derive/src/def_module.rs +++ b/derive/src/def_module.rs @@ -157,7 +157,7 @@ pub fn def_impl(context: TokenStream) -> TokenStream { 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 functions = vec![]; let mut classes = HashMap::new(); let mut submodules = HashMap::new(); let mut consts_info = HashMap::new(); @@ -173,7 +173,7 @@ pub fn def_impl(context: TokenStream) -> TokenStream { consts_info.insert(stringify!(#consts).to_string(), #consts.to_string()); )* #( - functions.insert(stringify!(#right_func).to_string(), #left_func(storage)); + functions.push((stringify!(#right_func).to_string(), #left_func(storage))); )* #( submodules.insert(stringify!(#submodules).to_string(), #submodules::module_init(storage)); diff --git a/examples/print.trc b/examples/print.trc index b85d2d05..4955f950 100644 --- a/examples/print.trc +++ b/examples/print.trc @@ -1,4 +1,4 @@ -print("{},{}!", "hello", "world") +print("{},{}!\n", "hello", "world") a:="hello" b:="world" -print("{},{}!", a, b) +print("{},{}!\n", a, b) diff --git a/libcore/src/libbasic.rs b/libcore/src/libbasic.rs index 5bd5a213..027d534f 100644 --- a/libcore/src/libbasic.rs +++ b/libcore/src/libbasic.rs @@ -308,7 +308,8 @@ impl RustFunction { pub struct Module { name: String, sub_modules: HashMap, - functions: HashMap, + functions: Vec<(String, RustFunction)>, + name_to_id: HashMap, // class name 和class id classes: HashMap, consts: HashMap, @@ -318,7 +319,7 @@ impl Module { pub fn new( name: impl Into, sub_modules: HashMap, - functions: HashMap, + functions: Vec<(String, RustFunction)>, classes: HashMap, consts: HashMap, ) -> Module { @@ -328,6 +329,7 @@ impl Module { functions, classes, consts, + name_to_id: HashMap::new(), } } @@ -337,8 +339,13 @@ impl Module { } pub fn add_function(&mut self, name: String, func: RustFunction) { - let ret = self.functions.insert(name, func); - debug_assert!(ret.is_none()); + self.functions.push((name.clone(), func)); + let tmp = self.name_to_id.insert(name, self.functions.len() - 1); + debug_assert!(tmp.is_none()); + } + + pub fn get_func_id_by_name(&self, name: &str) -> Option { + self.name_to_id.get(name).cloned() } pub fn get_module>>(&self, mut path: T) -> Option { @@ -367,14 +374,10 @@ impl Module { self.sub_modules = sub_modules; } - pub fn functions(&self) -> &HashMap { + pub fn functions(&self) -> &Vec<(String, RustFunction)> { &self.functions } - pub fn set_functions(&mut self, functions: HashMap) { - self.functions = functions; - } - pub fn classes(&self) -> &HashMap { &self.classes } diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index 2dff0f69..1fc88447 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -945,7 +945,7 @@ impl<'a> AstBuilder<'a> { match now.sub_modules().get(&import_item_name) { None => { // 不是模块 - match now.functions().get(&import_item_name) { + match now.get_func_id_by_name(&import_item_name) { None => { return self.try_err( istry, @@ -956,6 +956,7 @@ impl<'a> AstBuilder<'a> { ); } Some(func_item) => { + let func_item = now.functions()[func_item].1.clone(); let token_idx: ConstPoolIndexTy = self.token_lexer.add_id_token(func_item.get_name()); // println!("{}", func_item.get_name()); diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index 412ae19a..b029e98a 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -321,7 +321,7 @@ impl SymScope { } let funcs = stdlib.functions(); for i in funcs { - let idx = self.insert_sym_with_error(const_pool.name_pool[i.0], i.0)?; + let idx = self.insert_sym_with_error(const_pool.name_pool[&i.0], &i.0)?; // 在将类型全部添加进去之后,需要重新改写函数和类的输入和输出参数 let mut fobj = i.1.clone(); self.fix_func(fobj.get_io_mut(), libstorage, const_pool); diff --git a/src/tools/dis.rs b/src/tools/dis.rs index 6ba05913..7f892159 100644 --- a/src/tools/dis.rs +++ b/src/tools/dis.rs @@ -4,6 +4,11 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> RuntimeResult let mut compiler = crate::compiler::Compiler::new(opt); let mut ast = compiler.lex()?; let static_data = ast.prepare_get_static(); + println!("deps modules:"); + for i in &static_data.dll_module_should_loaded { + println!("{}", i); + } + println!(); for i in static_data.inst.iter().enumerate() { if rustcode { println!("Inst::new(Opcode::{}, {}),", i.1.opcode, i.1.operand); diff --git a/stdlib/Cargo.toml b/stdlib/Cargo.toml index 035bdf89..fd5d0bf7 100644 --- a/stdlib/Cargo.toml +++ b/stdlib/Cargo.toml @@ -12,4 +12,4 @@ rust-i18n = "3" [lib] name = "stdlib" -crate-type = ["cdylib"] +crate-type = ["dylib"] diff --git a/stdlib/src/prelude.rs b/stdlib/src/prelude.rs index 966f2076..088ae660 100644 --- a/stdlib/src/prelude.rs +++ b/stdlib/src/prelude.rs @@ -4,7 +4,7 @@ use rust_i18n::t; use std::io::{self, Write}; #[trc_function(var_params = true)] -#[no_mangle] +// #[no_mangle] pub fn print(fmt_string: str) -> void { let mut iter = va_list.iter(); let mut output_iter = unsafe { (*fmt_string).chars() }; @@ -27,7 +27,7 @@ pub fn print(fmt_string: str) -> void { } #[trc_function(var_params = true)] -#[no_mangle] +// #[no_mangle] pub fn println(fmt_string: str) -> void { let mut iter = va_list.iter(); let mut output_iter = unsafe { (*fmt_string).chars() };