From 38b3c056bc92bd667a66eb62c0f82932543531b8 Mon Sep 17 00:00:00 2001 From: limuy Date: Sat, 13 Apr 2024 20:26:04 +0800 Subject: [PATCH] feat(compiler):prepare for module import --- Cargo.lock | 63 ++++++++++++++++++++---------------- src/compiler.rs | 7 ++-- src/compiler/ast.rs | 28 ++++++++++++---- src/compiler/ast/ast_base.rs | 6 +++- src/compiler/manager.rs | 39 ++++++++++++++++++---- src/compiler/scope.rs | 44 +++++++------------------ tests/test_compiler.rs | 4 ++- 7 files changed, 115 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69b65e92..0eada1bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -525,9 +525,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" -version = "0.1.29" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f08474e32172238f2827bd160c67871cdb2801430f65c3979184dc362e3ca118" +checksum = "685a7d121ee3f65ae4fddd72b25a04bb36b6af81bc0828f7d5434c0fe60fa3a2" dependencies = [ "libc", ] @@ -584,7 +584,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -1414,7 +1414,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -1432,7 +1432,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -1452,17 +1452,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] [[package]] @@ -1473,9 +1474,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" @@ -1485,9 +1486,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" @@ -1497,9 +1498,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" @@ -1509,9 +1516,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" @@ -1521,9 +1528,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" @@ -1533,9 +1540,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" @@ -1545,9 +1552,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" diff --git a/src/compiler.rs b/src/compiler.rs index acefe407..98ad1fb8 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -5,16 +5,18 @@ pub mod manager; pub mod scope; pub mod token; -use self::{ast::AstBuilder, token::TokenLex}; +pub use self::{ast::AstBuilder, manager::ModuleManager, token::TokenLex}; use crate::cfg; use libcore::*; use rust_i18n::t; use std::{ + cell::RefCell, collections::HashMap, fmt::Display, fs, io::{self, BufRead}, process::exit, + rc::Rc, vec, }; @@ -385,7 +387,8 @@ impl Compiler { pub fn lex(&mut self) -> RuntimeResult { let token_lexer = TokenLex::new(self); - let mut ast_builder = AstBuilder::new(token_lexer); + let env_manager = Rc::new(RefCell::new(ModuleManager::new())); + let mut ast_builder = AstBuilder::new(token_lexer, env_manager.clone()); ast_builder.generate_code()?; Ok(ast_builder) } diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index 746b99d3..15687570 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -6,7 +6,10 @@ use super::{ token::{ConstPoolIndexTy, TokenType}, InputSource, TokenLex, }; -use crate::{base::dll::load_module_storage, compiler::token::TokenType::RightBigBrace}; +use crate::{ + base::dll::load_module_storage, + compiler::{manager::ModuleManager, token::TokenType::RightBigBrace}, +}; use collection_literals::collection; use libcore::*; use rust_i18n::t; @@ -39,6 +42,7 @@ pub struct AstBuilder<'a> { first_func: bool, modules_dll_dup: HashSet, modules_dll: Vec, + module_manager: Rc>>, } type AstError = RuntimeResult; @@ -113,7 +117,10 @@ macro_rules! expr_gen { } impl<'a> AstBuilder<'a> { - pub fn new(mut token_lexer: TokenLex<'a>) -> Self { + pub fn new( + mut token_lexer: TokenLex<'a>, + module_manager: Rc>>, + ) -> Self { let root_scope = Rc::new(RefCell::new(SymScope::new(SymScopePrev::Root))); let stdlib_dll_name = libloading::library_filename("stdlib"); let stdlib_dll = @@ -130,7 +137,9 @@ impl<'a> AstBuilder<'a> { let _optimize = token_lexer.compiler_data.option.optimize; root_scope .borrow_mut() - .import_native_module(prelude, stdstorage, &token_lexer.const_pool) + .import_native_module(prelude, stdstorage, &token_lexer.const_pool, || { + module_manager.borrow_mut().alloc_extern_function_id() + }) .expect("Import prelude but failed"); root_scope .borrow_mut() @@ -171,6 +180,7 @@ impl<'a> AstBuilder<'a> { first_func: false, modules_dll_dup: collection! {name_str.clone()}, modules_dll: vec![name_str.clone()], + module_manager, } } @@ -762,8 +772,10 @@ impl<'a> AstBuilder<'a> { } } // self.self_scope = tmp.clone(); + let function_id = self.module_manager.borrow_mut().alloc_custom_function_id(); self.self_scope.borrow_mut().add_custom_function( name_id, + function_id, CustomFunction::new( io, argname, @@ -948,9 +960,12 @@ impl<'a> AstBuilder<'a> { self.token_lexer.add_id_token(func_item.get_name()); // println!("{}", func_item.get_name()); let func_id = self.insert_sym_with_error(token_idx)?; - self.self_scope - .borrow_mut() - .add_extern_func(func_id, func_item.clone()); + let func_extern_id = self.alloc_extern_function_id(); + self.self_scope.borrow_mut().add_extern_func( + func_id, + func_extern_id, + func_item.clone(), + ); } } } @@ -968,6 +983,7 @@ impl<'a> AstBuilder<'a> { module, lib_storage, &self.token_lexer.const_pool, + || self.module_manager.borrow_mut().alloc_extern_function_id(), ) { return self.try_err(istry, e); }; diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index a55d6492..04ff1a12 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -1,7 +1,7 @@ use super::AstBuilder; use super::AstError; -use crate::compiler::token::Token; use crate::compiler::token::TokenType; +use crate::compiler::{scope::FuncIdxTy, token::Token}; use crate::compiler::{scope::SymScope, token::ConstPoolIndexTy}; use libcore::*; use rust_i18n::t; @@ -210,4 +210,8 @@ impl<'a> AstBuilder<'a> { self.modules_dll.push(module_name); } } + + pub fn alloc_extern_function_id(&mut self) -> FuncIdxTy { + self.module_manager.borrow_mut().alloc_extern_function_id() + } } diff --git a/src/compiler/manager.rs b/src/compiler/manager.rs index 59b4ffdf..455dc105 100644 --- a/src/compiler/manager.rs +++ b/src/compiler/manager.rs @@ -1,27 +1,54 @@ -use std::collections::HashMap; +//! ast的控制者,总管一个编译过程中的数据 use crate::compiler::ast::AstBuilder; - -/// ast的控制者,总管一个编译过程中的数据 +use std::collections::HashMap; /// ast manager -pub struct AstManager<'a> { +pub struct ModuleManager<'a> { // 储存模块和对应的中间文件的关系 modules: HashMap, // 有的模块是本次已经编译过的,在缓存中可以直接找到 cache: HashMap>, + global_custom_function_id: usize, + global_extern_function_id: usize, } -impl<'a> AstManager<'a> { +impl<'a> ModuleManager<'a> { pub fn new() -> Self { Self { modules: HashMap::new(), cache: HashMap::new(), + global_custom_function_id: 0, + global_extern_function_id: 0, } } + + pub fn add_module(&mut self, path: String, module: AstBuilder<'a>) { + self.cache.insert(path, module); + } + + pub fn add_specific_module(&mut self, name: String, path: String) { + self.modules.insert(name, path); + } + + pub fn get_module(&mut self, name: &str) -> Option<&AstBuilder<'a>> { + todo!() + } + + pub fn alloc_custom_function_id(&mut self) -> usize { + let ret = self.global_custom_function_id; + self.global_custom_function_id += 1; + ret + } + + pub fn alloc_extern_function_id(&mut self) -> usize { + let ret = self.global_extern_function_id; + self.global_extern_function_id += 1; + ret + } } -impl<'a> Default for AstManager<'a> { +impl<'a> Default for ModuleManager<'a> { fn default() -> Self { Self::new() } diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index c6d00d68..56a77689 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -137,7 +137,6 @@ impl VarInfo { #[derive(Clone, Debug, Default)] pub struct RootInfo { // 该id不会减少,在所有作用域共同使用 - funcs_extern_id: FuncIdxTy, } #[derive(Debug, Clone)] @@ -187,7 +186,6 @@ pub struct SymScope { // 当前作用域可以分配的下一个class id types_id: ClassIdxId, vars_id: ScopeAllocIdTy, - funcs_custom_id: FuncIdxTy, // 用户自定义的类型储存位置 types_custom_store: HashMap>, // 作用域暂时储存的函数token @@ -227,8 +225,6 @@ impl SymScope { } ret.scope_sym_id = prev_scope.borrow().scope_sym_id; ret.types_id = prev_scope.borrow().types_id; - ret.funcs_custom_id = prev_scope.borrow().funcs_custom_id; - // ret.root_scope.clone_from(&prev_scope.borrow().root_scope); ret } SymScopePrev::Root => Self { @@ -242,15 +238,13 @@ impl SymScope { pub fn add_custom_function( &mut self, id: ScopeAllocIdTy, + function_id: FuncIdxTy, mut f: CustomFunction, body: Vec<(Token, usize)>, - ) -> FuncIdxTy { - let ret = self.funcs_custom_id; + ) { self.funcs_temp_store.push((id, body)); - f.custom_id = ret; + f.custom_id = function_id; self.add_func(id, Rc::new(f)); - self.funcs_custom_id += 1; - ret } pub fn insert_sym_with_error( @@ -291,6 +285,7 @@ impl SymScope { stdlib: &Module, libstorage: &ModuleStorage, const_pool: &ValuePool, + mut alloc_extern_id_func: impl FnMut() -> usize, ) -> Result<(), ErrorInfo> { let types = stdlib.classes(); // println!("{:?}", types); @@ -322,7 +317,7 @@ impl SymScope { // 在将类型全部添加进去之后,需要重新改写函数和类的输入和输出参数 let mut fobj = i.1.clone(); self.fix_func(fobj.get_io_mut(), libstorage, const_pool); - self.add_extern_func(idx, fobj); + self.add_extern_func(idx, alloc_extern_id_func(), fobj); } // 改写类的重载方法 Ok(()) @@ -401,31 +396,16 @@ impl SymScope { self.funcs.insert(id, f); } - pub fn add_extern_func(&mut self, id: ScopeAllocIdTy, mut f: RustFunction) { - let funcid = self.alloc_extern_func_id(); - f.buildin_id = funcid; + pub fn add_extern_func( + &mut self, + id: ScopeAllocIdTy, + function_id: FuncIdxTy, + mut f: RustFunction, + ) { + f.buildin_id = function_id; self.add_func(id, Rc::new(f)) } - pub fn alloc_extern_func_id(&mut self) -> FuncIdxTy { - match self.root_scope { - Some(ref root_scope) => { - let tmp = root_scope.upgrade().unwrap(); - let mut tmp = tmp.borrow_mut(); - let refer = tmp.prev_scope.get_info(); - let ret = refer.funcs_extern_id; - refer.funcs_extern_id += 1; - ret - } - None => { - let refer = self.prev_scope.get_info(); - let ret = refer.funcs_extern_id; - refer.funcs_extern_id += 1; - ret - } - } - } - /// 返回变量的索引和内存地址 pub fn add_var(&mut self, id: ScopeAllocIdTy, ty: TyIdxTy, var_sz: usize) -> (VarIdxTy, usize) { let ret_addr = self.var_sz; diff --git a/tests/test_compiler.rs b/tests/test_compiler.rs index 886b3552..742c80d7 100644 --- a/tests/test_compiler.rs +++ b/tests/test_compiler.rs @@ -11,8 +11,10 @@ macro_rules! gen_test_env { CompileOption::new(false, InputSource::StringInternal), $test_code, ); + use std::{cell::RefCell, rc::Rc}; + let env_tmp = Rc::new(RefCell::new(trc::compiler::ModuleManager::new())); let token_lexer = trc::compiler::token::TokenLex::new(&mut compiler); - let mut $env_name = trc::compiler::ast::AstBuilder::new(token_lexer); + let mut $env_name = trc::compiler::ast::AstBuilder::new(token_lexer, env_tmp); }; }