From 7cbf804459125ae25302c25ba4b7f53b4c700182 Mon Sep 17 00:00:00 2001 From: limuy Date: Sun, 26 May 2024 17:05:23 +0800 Subject: [PATCH] change ScopeAllocId into newtype --- libcore/src/libbasic.rs | 2 +- src/compiler/ast.rs | 10 +++++++--- src/compiler/ast/ast_base.rs | 4 ++-- src/compiler/scope.rs | 32 ++++++++++++++++++-------------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/libcore/src/libbasic.rs b/libcore/src/libbasic.rs index 0e4f0d5e..640687d2 100644 --- a/libcore/src/libbasic.rs +++ b/libcore/src/libbasic.rs @@ -10,13 +10,13 @@ use super::{codegen::Opcode, error::*}; pub type RustlibFunc = fn(&mut DynaData) -> ErrorInfoResult<()>; -pub type ScopeAllocId = usize; pub type TypeAllowNull = Option; // 定义函数的索引类型 crate::impl_newtype_int!(FuncIdx, usize); crate::impl_newtype_int!(ClassIdxId, usize); crate::impl_newtype_int!(VarIdx, usize); crate::impl_newtype_int!(ConstPoolData, usize); +crate::impl_newtype_int!(ScopeAllocId, usize); #[derive(Clone, Debug)] pub struct IOType { diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index 9898679a..1d0c87a5 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -891,7 +891,7 @@ impl<'a> ModuleUnit<'a> { let name_id = self.insert_sym_with_error(name)?; // FIXME:BUG!CLASS ID SHOULD BE ALLOCATED let mut class_obj = CustomType::new( - ClassIdxId(name_id), + ClassIdxId(*name_id), self.token_lexer.borrow_mut().get_constpool().id_name[*name].clone(), ); self.get_token_checked(Token::LeftBigBrace)?; @@ -1114,7 +1114,7 @@ impl<'a> ModuleUnit<'a> { return self.gen_error(ErrorInfo::new( t!( SYMBOL_NOT_FOUND, - "0" = self.token_lexer.borrow_mut().get_constpool().id_name[name] + "0" = self.token_lexer.borrow_mut().get_constpool().id_name[*name] ), t!(SYMBOL_ERROR), )) @@ -1335,7 +1335,11 @@ impl<'a> ModuleUnit<'a> { /// # Return /// 返回函数的首地址 - fn lex_function(&mut self, funcid: usize, body: &FuncBodyTy) -> RuntimeResult<(usize, usize)> { + fn lex_function( + &mut self, + funcid: ScopeAllocId, + body: &FuncBodyTy, + ) -> RuntimeResult<(usize, usize)> { if !self.first_func { // 如果不是第一个函数,在末尾加上结束主程序的指令 // println!("run here.Inst num is {}", self.staticdata.inst.len()); diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index fff05686..3b662d21 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -183,8 +183,8 @@ impl<'a> ModuleUnit<'a> { ArguError::TypeNotMatch(ArgumentError { expected, actual }) => ErrorInfo::new( t!( EXPECT_TYPE, - "0" = self.get_ty_name(expected), - "1" = self.get_ty_name(actual) + "0" = self.get_ty_name(ScopeAllocId(expected)), + "1" = self.get_ty_name(ScopeAllocId(actual)) ), t!(ARGUMENT_ERROR), ), diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index 08c1f0b1..f9f3ce44 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -118,12 +118,13 @@ pub type FuncBodyTy = Vec<(Token, usize)>; #[derive(Clone, Debug, Copy)] pub struct VarInfo { pub ty: ClassIdxId, - pub var_idx: usize, + // FIXME:using ScopeAllocId,BUT IN ast.rs using it access id_name,should use token type + pub var_idx: ScopeAllocId, pub addr: usize, } impl VarInfo { - pub fn new(ty: ClassIdxId, var_idx: usize, addr: usize) -> Self { + pub fn new(ty: ClassIdxId, var_idx: ScopeAllocId, addr: usize) -> Self { Self { ty, var_idx, addr } } } @@ -372,18 +373,18 @@ impl SymScope { } } - pub fn new_id(&mut self) -> usize { + pub fn new_id(&mut self) -> ScopeAllocId { let ret = self.scope_sym_id; - self.scope_sym_id += 1; + *self.scope_sym_id += 1; ret } - pub fn insert_sym(&mut self, id: ConstPoolData) -> Option { + pub fn insert_sym(&mut self, id: ConstPoolData) -> Option { let t = self.sym_map.insert(id, self.scope_sym_id); - self.scope_sym_id += 1; + *self.scope_sym_id += 1; // 先前不能存在 match t { - None => Some(self.scope_sym_id - 1), + None => Some(ScopeAllocId(*self.scope_sym_id - 1)), Some(_) => None, } } @@ -399,7 +400,7 @@ impl SymScope { } } - pub fn get_sym(&self, id: ConstPoolData) -> Option { + pub fn get_sym(&self, id: ConstPoolData) -> Option { let t = self.sym_map.get(&id); match t { None => match self.prev_scope { @@ -441,9 +442,9 @@ impl SymScope { self.vars .insert(id, VarInfo::new(ty, self.vars_id, ret_addr)); let ret = self.vars_id; - self.vars_id += 1; + *self.vars_id += 1; self.var_sz += var_sz; - (VarIdx(ret), ret_addr) + (VarIdx(*ret), ret_addr) } pub fn get_var_table_sz(&self) -> usize { @@ -460,12 +461,12 @@ impl SymScope { } } - pub fn get_scope_last_idx(&self) -> usize { + pub fn get_scope_last_idx(&self) -> ScopeAllocId { self.scope_sym_id } pub fn get_var_table_len(&self) -> usize { - self.vars_id + *self.vars_id } pub fn get_class_by_class_id(&self, classid: ClassIdxId) -> Option> { @@ -559,8 +560,11 @@ mod tests { root_scope.borrow_mut().insert_sym(ConstPoolData(1)); let mut son_scope = SymScope::new(SymScopePrev::Prev(root_scope.clone())); son_scope.insert_sym(ConstPoolData(2)); - assert_eq!(son_scope.get_sym(ConstPoolData(2)), Some(1)); + assert_eq!(son_scope.get_sym(ConstPoolData(2)), Some(ScopeAllocId(1))); drop(son_scope); - assert_eq!(root_scope.borrow().get_sym(ConstPoolData(1)), Some(0)); + assert_eq!( + root_scope.borrow().get_sym(ConstPoolData(1)), + Some(ScopeAllocId(0)) + ); } }