Skip to content

Commit

Permalink
feat(compiler):prepare for module import
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 13, 2024
1 parent 06bac51 commit 38b3c05
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 76 deletions.
63 changes: 35 additions & 28 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -385,7 +387,8 @@ impl Compiler {

pub fn lex(&mut self) -> RuntimeResult<AstBuilder> {
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)
}
Expand Down
28 changes: 22 additions & 6 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +42,7 @@ pub struct AstBuilder<'a> {
first_func: bool,
modules_dll_dup: HashSet<String>,
modules_dll: Vec<String>,
module_manager: Rc<RefCell<ModuleManager<'a>>>,
}

type AstError<T> = RuntimeResult<T>;
Expand Down Expand Up @@ -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<RefCell<ModuleManager<'a>>>,
) -> Self {
let root_scope = Rc::new(RefCell::new(SymScope::new(SymScopePrev::Root)));
let stdlib_dll_name = libloading::library_filename("stdlib");
let stdlib_dll =
Expand All @@ -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()
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
);
}
}
}
Expand All @@ -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);
};
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/ast/ast_base.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
}
}
39 changes: 33 additions & 6 deletions src/compiler/manager.rs
Original file line number Diff line number Diff line change
@@ -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<String, String>,
// 有的模块是本次已经编译过的,在缓存中可以直接找到
cache: HashMap<String, AstBuilder<'a>>,
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()
}
Expand Down
Loading

0 comments on commit 38b3c05

Please sign in to comment.