Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed May 22, 2024
1 parent cada299 commit 163de5e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions libcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub use types::*;

pub use mimalloc::MiMalloc;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
// #[global_allocator]
// static GLOBAL: MiMalloc = MiMalloc;

pub const GET_LIB_FUNC_NAME: &str = "get_lib";
pub const GET_STORAGE_FUNC_NAME: &str = "get_storage";
Expand Down
15 changes: 8 additions & 7 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rust_i18n::t;
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
iter::Cloned,
mem::swap,
path::PathBuf,
rc::Rc,
Expand Down Expand Up @@ -49,6 +48,7 @@ pub struct ModuleUnit<'a> {
first_func: bool,
/// 对哈希表去重,并且记录每个dll的函数起始索引
modules_dll_dup: HashSet<String>,
/// 每个模块的函数起始点
modules_info: HashMap<String, usize>,
/// 引用的dll
modules_dll: Vec<String>,
Expand Down Expand Up @@ -1019,8 +1019,9 @@ impl<'a> ModuleUnit<'a> {
.token_lexer
.borrow_mut()
.add_id(func_item.get_name().to_owned());
let func_extern_id =
func_item.buildin_id + self.modules_info[&import_file_path];
let func_extern_id = FuncIdxTy(
*func_item.buildin_id + self.modules_info[&import_file_path],
);
// println!("{}", func_extern_id);
let name = func_item.get_name().to_owned();
if let Err(e) = self.self_scope.borrow_mut().import_extern_func(
Expand Down Expand Up @@ -1063,7 +1064,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 生成修改变量的指令
fn modify_var(&mut self, varty: TyIdxTy, var_addr: usize, is_global: bool) {
fn modify_var(&mut self, varty: ClassIdxId, var_addr: usize, is_global: bool) {
let objsz = self.get_ty_sz(varty);
if !is_global {
self.add_double_bycode(Opcode::StoreLocal, var_addr as Opidx, objsz as Opidx);
Expand Down Expand Up @@ -1236,8 +1237,8 @@ impl<'a> ModuleUnit<'a> {
self.expr(true)?;
let actual_ty = self.process_info.pop_last_ty().unwrap();
if ty != actual_ty {
let s1 = self.get_ty_name(ty);
let s2 = self.get_ty_name(actual_ty);
let s1 = self.get_ty_name_by_class_id(ty);
let s2 = self.get_ty_name_by_class_id(actual_ty);
return self.gen_error(ErrorInfo::new(
t!(RETURN_TYPE_ERROR, "0" = s1, "1" = s2),
t!(TYPE_ERROR),
Expand All @@ -1247,7 +1248,7 @@ impl<'a> ModuleUnit<'a> {
None => {
if self.expr(true).is_ok() {
let actual_ty = self.process_info.pop_last_ty().unwrap();
let name = self.get_ty_name(actual_ty);
let name = self.get_ty_name_by_class_id(actual_ty);
return self.gen_error(ErrorInfo::new(
t!(RETURN_TYPE_ERROR, "0" = "void", "1" = name),
t!(TYPE_ERROR),
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/ast/ast_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 通过Scope中的ID获取类型名
pub fn get_ty_name(&mut self, type_name: TyIdxTy) -> String {
pub fn get_ty_name(&mut self, type_name: ScopeAllocIdTy) -> String {
self.self_scope
.borrow()
.get_class(type_name)
Expand All @@ -204,6 +204,15 @@ impl<'a> ModuleUnit<'a> {
.to_string()
}

pub fn get_ty_name_by_class_id(&mut self, type_name: ClassIdxId) -> String {
self.self_scope
.borrow()
.get_class_by_class_id(type_name)
.unwrap()
.get_name()
.to_string()
}

/// 导入模块中的符号
pub fn import_module_sym(&mut self, lib: &Module) {
for i in lib.functions() {
Expand Down
12 changes: 8 additions & 4 deletions src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::{
ValuePool,
};
use libcore::*;
use num_enum::Default;
use std::{
cell::RefCell,
collections::HashMap,
Expand Down Expand Up @@ -127,7 +126,7 @@ pub struct VarInfo {
}

impl VarInfo {
pub fn new(ty: TyIdxTy, var_idx: usize, addr: usize) -> Self {
pub fn new(ty: ClassIdxId, var_idx: usize, addr: usize) -> Self {
Self { ty, var_idx, addr }
}
}
Expand Down Expand Up @@ -440,14 +439,19 @@ impl SymScope {
}

/// 返回变量的索引和内存地址
pub fn add_var(&mut self, id: ScopeAllocIdTy, ty: TyIdxTy, var_sz: usize) -> (VarIdxTy, usize) {
pub fn add_var(
&mut self,
id: ScopeAllocIdTy,
ty: ClassIdxId,
var_sz: usize,
) -> (VarIdxTy, usize) {
let ret_addr = self.var_sz;
self.vars
.insert(id, VarInfo::new(ty, self.vars_id, ret_addr));
let ret = self.vars_id;
self.vars_id += 1;
self.var_sz += var_sz;
(ret, ret_addr)
(VarIdxTy(ret), ret_addr)
}

pub fn get_var_table_sz(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_all_examples.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! this test all examples files and check them output
use assert_cmd::Command;
use libcore::utils::get_next_check_char;
use std::fs::read_to_string;
use trc::base::utils::get_next_check_char;

/// 检查迭代器是否剩下的所有字符都满足某个条件
fn check_whether_end(iter: &mut impl Iterator<Item = char>, condit: impl Fn(char) -> bool) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn get_func_id(scope: &mut ModuleUnit, name: &str) -> Opidx {
.borrow()
.get_sym(idstr)
.expect("sym not found");
scope
*scope
.get_scope()
.borrow()
.get_function(symid)
Expand Down

0 comments on commit 163de5e

Please sign in to comment.