Skip to content

Commit

Permalink
change ScopeAllocId into newtype
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed May 26, 2024
1 parent 5a25c35 commit 7cbf804
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion libcore/src/libbasic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use super::{codegen::Opcode, error::*};

pub type RustlibFunc = fn(&mut DynaData) -> ErrorInfoResult<()>;

pub type ScopeAllocId = usize;
pub type TypeAllowNull = Option<ClassIdxId>;
// 定义函数的索引类型
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 {
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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),
))
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast/ast_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
Expand Down
32 changes: 18 additions & 14 deletions src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -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<usize> {
pub fn insert_sym(&mut self, id: ConstPoolData) -> Option<ScopeAllocId> {
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,
}
}
Expand All @@ -399,7 +400,7 @@ impl SymScope {
}
}

pub fn get_sym(&self, id: ConstPoolData) -> Option<usize> {
pub fn get_sym(&self, id: ConstPoolData) -> Option<ScopeAllocId> {
let t = self.sym_map.get(&id);
match t {
None => match self.prev_scope {
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Rc<dyn ClassInterface>> {
Expand Down Expand Up @@ -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))
);
}
}

0 comments on commit 7cbf804

Please sign in to comment.