Skip to content

Commit

Permalink
fix(compiler):fix stdlib function order bug
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 11, 2024
1 parent e6c400e commit d59d74b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions derive/src/def_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions examples/print.trc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
print("{},{}!", "hello", "world")
print("{},{}!\n", "hello", "world")
a:="hello"
b:="world"
print("{},{}!", a, b)
print("{},{}!\n", a, b)
21 changes: 12 additions & 9 deletions libcore/src/libbasic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ impl RustFunction {
pub struct Module {
name: String,
sub_modules: HashMap<String, Module>,
functions: HashMap<String, RustFunction>,
functions: Vec<(String, RustFunction)>,
name_to_id: HashMap<String, usize>,
// class name 和class id
classes: HashMap<String, usize>,
consts: HashMap<String, String>,
Expand All @@ -318,7 +319,7 @@ impl Module {
pub fn new(
name: impl Into<String>,
sub_modules: HashMap<String, Module>,
functions: HashMap<String, RustFunction>,
functions: Vec<(String, RustFunction)>,
classes: HashMap<String, usize>,
consts: HashMap<String, String>,
) -> Module {
Expand All @@ -328,6 +329,7 @@ impl Module {
functions,
classes,
consts,
name_to_id: HashMap::new(),
}
}

Expand All @@ -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<usize> {
self.name_to_id.get(name).cloned()
}

pub fn get_module<T: Iterator<Item = impl Into<String>>>(&self, mut path: T) -> Option<Module> {
Expand Down Expand Up @@ -367,14 +374,10 @@ impl Module {
self.sub_modules = sub_modules;
}

pub fn functions(&self) -> &HashMap<String, RustFunction> {
pub fn functions(&self) -> &Vec<(String, RustFunction)> {
&self.functions
}

pub fn set_functions(&mut self, functions: HashMap<String, RustFunction>) {
self.functions = functions;
}

pub fn classes(&self) -> &HashMap<String, usize> {
&self.classes
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/tools/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ rust-i18n = "3"

[lib]
name = "stdlib"
crate-type = ["cdylib"]
crate-type = ["dylib"]
4 changes: 2 additions & 2 deletions stdlib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand All @@ -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() };
Expand Down

0 comments on commit d59d74b

Please sign in to comment.