Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed May 25, 2024
1 parent 163de5e commit dd839cd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 62 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

32 changes: 18 additions & 14 deletions libcore/src/libbasic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ use super::{codegen::Opcode, error::*};

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

pub type ScopeAllocIdTy = usize;
pub type ScopeAllocId = usize;
pub type TypeAllowNull = Option<ClassIdxId>;
// 定义函数的索引类型
crate::impl_newtype_int!(FuncIdxTy, usize);
crate::impl_newtype_int!(FuncIdx, usize);
crate::impl_newtype_int!(ClassIdxId, usize);
crate::impl_newtype_int!(VarIdxTy, usize);
crate::impl_newtype_int!(VarIdx, usize);

pub type TyIdxTy = ScopeAllocIdTy;
type ConstPoolIndexTy = usize;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -61,7 +60,7 @@ pub type Argvs = Vec<ClassIdxId>;
#[derive(Clone, Debug)]
pub struct RustFunction {
pub name: String,
pub buildin_id: FuncIdxTy,
pub buildin_id: FuncIdx,
pub ptr: RustlibFunc,
pub io: IOType,
}
Expand Down Expand Up @@ -121,7 +120,7 @@ pub trait FunctionInterface: Downcast + Debug {
fn get_io(&self) -> &IOType;
fn get_name(&self) -> &str;
fn get_io_mut(&mut self) -> &mut IOType;
fn get_func_id(&self) -> FuncIdxTy;
fn get_func_id(&self) -> FuncIdx;
}

impl_downcast!(FunctionInterface);
Expand Down Expand Up @@ -157,7 +156,7 @@ impl FunctionInterface for RustFunction {
&mut self.io
}

fn get_func_id(&self) -> FuncIdxTy {
fn get_func_id(&self) -> FuncIdx {
self.buildin_id
}
}
Expand All @@ -176,12 +175,17 @@ impl OverrideWrapper {

#[derive(Debug, Clone, Default)]
pub struct RustClass {
pub members: HashMap<String, String>,
/// 成员变量
pub attribute_members: HashMap<String, String>,
/// 成员函数
pub functions: HashMap<String, RustFunction>,
/// 重载函数
pub overrides: HashMap<OverrideOperations, OverrideWrapper>,
/// 类ID
pub id: ClassIdxId,
/// 类名
pub name: &'static str,
pub id_to_var: HashMap<ConstPoolIndexTy, TyIdxTy>,
pub id_to_var: HashMap<ConstPoolIndexTy, ClassIdxId>,
}

/// 约定,0号id是any类型
Expand All @@ -194,7 +198,7 @@ impl RustClass {
storage: &mut ModuleStorage,
) -> ClassIdxId {
let ret = RustClass {
members,
attribute_members: members,
functions: functions.unwrap_or_default(),
overrides: overrides.unwrap_or_default(),
id: ClassIdxId(storage.class_table.len()),
Expand All @@ -209,7 +213,7 @@ impl RustClass {
}

pub fn add_attr(&mut self, name: impl Into<String>, ty: String) {
self.members.insert(name.into(), ty);
self.attribute_members.insert(name.into(), ty);
}
}

Expand Down Expand Up @@ -265,9 +269,9 @@ impl ModuleStorage {
}
}

pub fn add_func(&mut self, f: RustlibFunc) -> FuncIdxTy {
pub fn add_func(&mut self, f: RustlibFunc) -> FuncIdx {
self.func_table.push(f);
FuncIdxTy(self.func_table.len() - 1)
FuncIdx(self.func_table.len() - 1)
}

/// 获取类的个数
Expand All @@ -281,7 +285,7 @@ impl ModuleStorage {
ClassIdxId(self.class_table.len() - 1)
}

pub fn access_func(&self, id: FuncIdxTy) -> RustlibFunc {
pub fn access_func(&self, id: FuncIdx) -> RustlibFunc {
self.func_table[*id]
}

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl<'a> ModuleUnit<'a> {

fn load_var(
&mut self,
idx: ScopeAllocIdTy,
idx: ScopeAllocId,
name_token: ConstPoolIndexTy,
istry: bool,
) -> AstError<()> {
Expand Down Expand Up @@ -1019,7 +1019,7 @@ impl<'a> ModuleUnit<'a> {
.token_lexer
.borrow_mut()
.add_id(func_item.get_name().to_owned());
let func_extern_id = FuncIdxTy(
let func_extern_id = FuncIdx(
*func_item.buildin_id + self.modules_info[&import_file_path],
);
// println!("{}", func_extern_id);
Expand All @@ -1039,7 +1039,7 @@ impl<'a> ModuleUnit<'a> {
}
Some(module) => {
let tmp = self.token_lexer.borrow_mut().add_id(import_item_name);
let module_sym_idx: ScopeAllocIdTy = self.insert_sym_with_error(tmp)?;
let module_sym_idx: ScopeAllocId = self.insert_sym_with_error(tmp)?;
self.import_module_sym(module);
let sub_module = Rc::new(RefCell::new(SymScope::new(SymScopePrev::Prev(
self.self_scope.clone(),
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 @@ -195,7 +195,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 通过Scope中的ID获取类型名
pub fn get_ty_name(&mut self, type_name: ScopeAllocIdTy) -> String {
pub fn get_ty_name(&mut self, type_name: ScopeAllocId) -> String {
self.self_scope
.borrow()
.get_class(type_name)
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 添加一个符号,在符号冲突的时候报出错误
pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError<ScopeAllocIdTy> {
pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError<ScopeAllocId> {
match self.self_scope.borrow_mut().insert_sym(name) {
Some(v) => Ok(v),
None => self.gen_error(ErrorInfo::new(
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
compiler::{ast::ModuleUnit, linker::link, optimizer::optimize_module, Compiler, CompilerImpl},
};
use libcore::StaticData;
use libcore::{utils, FuncIdxTy};
use libcore::{utils, FuncIdx};
use std::{
cell::RefCell,
collections::{hash_map::IterMut, HashMap},
Expand Down Expand Up @@ -112,16 +112,16 @@ impl<'a> ModuleManager<'a> {
// 对应模块不存在,返回None
}

pub fn alloc_custom_function_id(&mut self) -> FuncIdxTy {
pub fn alloc_custom_function_id(&mut self) -> FuncIdx {
let ret = self.global_custom_function_id;
self.global_custom_function_id += 1;
FuncIdxTy(ret)
FuncIdx(ret)
}

pub fn alloc_extern_function_id(&mut self) -> FuncIdxTy {
pub fn alloc_extern_function_id(&mut self) -> FuncIdx {
let ret = self.global_extern_function_id;
self.global_extern_function_id += 1;
FuncIdxTy(ret)
FuncIdx(ret)
}

/// 执行优化
Expand Down
Loading

0 comments on commit dd839cd

Please sign in to comment.