Skip to content

Commit

Permalink
change ConstPoolIndex into newtype
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed May 26, 2024
1 parent 315a5a5 commit 27d3487
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 148 deletions.
2 changes: 1 addition & 1 deletion examples/skip_test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import_user_defined
import_user_defined
11 changes: 5 additions & 6 deletions libcore/src/libbasic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ pub type TypeAllowNull = Option<ClassIdxId>;
crate::impl_newtype_int!(FuncIdx, usize);
crate::impl_newtype_int!(ClassIdxId, usize);
crate::impl_newtype_int!(VarIdx, usize);

type ConstPoolIndexTy = usize;
crate::impl_newtype_int!(ConstPoolIndex, usize);

#[derive(Clone, Debug)]
pub struct IOType {
Expand Down Expand Up @@ -54,7 +53,7 @@ pub enum OverrideOperations {
Power,
}

pub type ArgsNameTy = Vec<ConstPoolIndexTy>;
pub type ArgsNameTy = Vec<ConstPoolIndex>;
pub type Argvs = Vec<ClassIdxId>;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -128,7 +127,7 @@ impl_downcast!(FunctionInterface);
pub trait ClassInterface: Downcast + Sync + Send + Debug + Display {
fn has_func(&self, funcname: &str) -> Option<Box<dyn FunctionInterface>>;

fn has_attr(&self, attrname: usize) -> bool;
fn has_attr(&self, attrname: ConstPoolIndex) -> bool;

fn get_id(&self) -> ClassIdxId;

Expand Down Expand Up @@ -185,7 +184,7 @@ pub struct RustClass {
pub id: ClassIdxId,
/// 类名
pub name: &'static str,
pub id_to_var: HashMap<ConstPoolIndexTy, ClassIdxId>,
pub id_to_var: HashMap<ConstPoolIndex, ClassIdxId>,
}

/// 约定,0号id是any类型
Expand Down Expand Up @@ -227,7 +226,7 @@ impl ClassInterface for RustClass {
None
}

fn has_attr(&self, attrname: usize) -> bool {
fn has_attr(&self, attrname: ConstPoolIndex) -> bool {
self.id_to_var.contains_key(&attrname)
}

Expand Down
28 changes: 14 additions & 14 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl CompileOption {
}
}

type Pool<T> = HashMap<T, usize>;
type Pool<T> = HashMap<T, ConstPoolIndex>;

#[derive(Default)]
/// 管理常量池添加删除
Expand All @@ -124,30 +124,30 @@ pub struct ValuePool {
macro_rules! gen_single_getter_setter {
($func_name:ident => ($const_pool:ident, $id_pool:ident, $type:ty)) => {
paste::paste! {
pub fn [<add_ $func_name>](&mut self, val: $type) -> usize {
pub fn [<add_ $func_name>](&mut self, val: $type) -> ConstPoolIndex {
let len_tmp = self.$const_pool.len();
let ret = *self.$const_pool.entry(val.clone()).or_insert(len_tmp);
let ret = *self.$const_pool.entry(val.clone()).or_insert(ConstPoolIndex(len_tmp));
if len_tmp != self.$const_pool.len() {
self.$id_pool.push(val);
}
ret
}
pub fn [<get_ $func_name>](&self, val: &$type) -> Option<usize> {
pub fn [<get_ $func_name>](&self, val: &$type) -> Option<ConstPoolIndex> {
self.$const_pool.get(val).copied()
}
}
};
($func_name:ident => ($const_pool:ident, $id_pool:ident, $type:ty, $convert_func: ident)) => {
paste::paste! {
pub fn [<add_ $func_name>](&mut self, val: $type) -> usize {
pub fn [<add_ $func_name>](&mut self, val: $type) -> ConstPoolIndex {
let len_tmp = self.$const_pool.len();
let ret = *self.$const_pool.entry(val.clone()).or_insert(len_tmp);
let ret = *self.$const_pool.entry(val.clone()).or_insert(ConstPoolIndex(len_tmp));
if len_tmp != self.$const_pool.len() {
self.$id_pool.push($convert_func(val));
}
ret
}
pub fn [<get_ $func_name>](&self, val: &$type) -> Option<usize> {
pub fn [<get_ $func_name>](&self, val: &$type) -> Option<ConstPoolIndex> {
self.$const_pool.get(val).copied()
}
}
Expand Down Expand Up @@ -300,17 +300,17 @@ impl Compiler {

#[cfg(test)]
mod tests {
use super::ValuePool;
use super::*;

#[test]
fn test_value_pool() {
let mut pool = ValuePool::new();
assert_eq!(pool.add_float("9.0".to_owned()), 0);
assert_eq!(pool.add_float("9.0".to_owned()), 0);
assert_eq!(pool.add_float("9.5".to_owned()), 1);
assert_eq!(pool.add_string(String::from("value")), 0);
assert_eq!(pool.add_string(String::from("value")), 0);
assert_eq!(pool.add_string(String::from("vale")), 1);
assert_eq!(pool.add_float("9.0".to_owned()), ConstPoolIndex(0));
assert_eq!(pool.add_float("9.0".to_owned()), ConstPoolIndex(0));
assert_eq!(pool.add_float("9.5".to_owned()), ConstPoolIndex(1));
assert_eq!(pool.add_string(String::from("value")), ConstPoolIndex(0));
assert_eq!(pool.add_string(String::from("value")), ConstPoolIndex(0));
assert_eq!(pool.add_string(String::from("vale")), ConstPoolIndex(1));
assert!((pool.id_float[0] - 9.0).abs() < 0.00001);
assert_eq!(pool.id_str[1], "vale");
}
Expand Down
77 changes: 46 additions & 31 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod ast_base;
mod lexprocess;

use super::{
scope::*,
token::{ConstPoolIndexTy, Token},
InputSource, TokenLex,
};
use super::{scope::*, token::Token, InputSource, TokenLex};
use crate::{
base::dll::load_module_storage,
compiler::{manager::ModuleManager, token::Token::RightBigBrace, CompilerImpl},
compiler::{
manager::ModuleManager,
token::{Token::RightBigBrace, CONST_IDX_PLACEHOLDER},
CompilerImpl,
},
};
use collection_literals::collection;
use libcore::*;
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'a> ModuleUnit<'a> {
fn load_var(
&mut self,
idx: ScopeAllocId,
name_token: ConstPoolIndexTy,
name_token: ConstPoolIndex,
istry: bool,
) -> AstError<()> {
let var = match self.self_scope.borrow().get_var(idx) {
Expand All @@ -449,7 +449,7 @@ impl<'a> ModuleUnit<'a> {
ErrorInfo::new(
t!(
SYMBOL_NOT_FOUND,
"0" = self.token_lexer.borrow_mut().get_constpool().id_name[name_token]
"0" = self.token_lexer.borrow_mut().get_constpool().id_name[*name_token]
),
t!(SYMBOL_ERROR),
),
Expand All @@ -473,7 +473,8 @@ impl<'a> ModuleUnit<'a> {
ErrorInfo::new(
t!(
SYMBOL_NOT_FOUND,
"0" = self.token_lexer.borrow_mut().get_constpool().id_name[token_data]
"0" =
self.token_lexer.borrow_mut().get_constpool().id_name[*token_data]
),
t!(SYMBOL_ERROR),
),
Expand Down Expand Up @@ -527,7 +528,7 @@ impl<'a> ModuleUnit<'a> {
t!(
SYMBOL_NOT_FOUND,
"0" = self.token_lexer.borrow_mut().get_constpool().id_name
[token_data]
[*token_data]
),
t!(SYMBOL_ERROR),
),
Expand Down Expand Up @@ -568,23 +569,23 @@ impl<'a> ModuleUnit<'a> {
let t = self.next_token()?;
match t {
Token::IntValue(data) => {
self.add_bycode(Opcode::LoadInt, data as Opidx);
self.add_bycode(Opcode::LoadInt, *data as Opidx);
self.process_info.new_type(self.cache.intty_id);
}
Token::FloatValue(data) => {
self.add_bycode(Opcode::LoadFloat, data as Opidx);
self.add_bycode(Opcode::LoadFloat, *data as Opidx);
self.process_info.new_type(self.cache.floatty_id);
}
Token::StringValue(data) => {
self.add_bycode(Opcode::LoadString, data as Opidx);
self.add_bycode(Opcode::LoadString, *data as Opidx);
self.process_info.new_type(self.cache.strty_id);
}
Token::CharValue(data) => {
self.add_bycode(Opcode::LoadChar, data as Opidx);
self.add_bycode(Opcode::LoadChar, *data as Opidx);
self.process_info.new_type(self.cache.charty_id);
}
Token::BoolValue(data) => {
self.add_bycode(Opcode::LoadBool, data as Opidx);
self.add_bycode(Opcode::LoadBool, *data as Opidx);
self.process_info.new_type(self.cache.boolty_id);
}
_ => {
Expand Down Expand Up @@ -695,7 +696,7 @@ impl<'a> ModuleUnit<'a> {
// 处理通配符
let t = self.next_token()?;
if let Token::ID(data) = t
&& self.token_lexer.borrow_mut().get_constpool().id_name[data] == "_"
&& self.token_lexer.borrow_mut().get_constpool().id_name[*data] == "_"
{
is_end = true;
self.add_bycode(Opcode::Jump, ARG_WRONG);
Expand Down Expand Up @@ -781,7 +782,7 @@ impl<'a> ModuleUnit<'a> {
}

fn def_func(&mut self) -> AstError<()> {
let funcname = self.get_token_checked_with_val(Token::ID(0))?;
let funcname = self.get_token_checked_with_val(Token::ID(CONST_IDX_PLACEHOLDER))?;
let name_id = self.insert_sym_with_error(funcname)?;
// lex args
self.get_token_checked(Token::LeftSmallBrace)?;
Expand All @@ -795,7 +796,7 @@ impl<'a> ModuleUnit<'a> {
if t != Token::Comma {
self.token_lexer.borrow_mut().next_back(t);
}
let name_id = self.get_token_checked_with_val(Token::ID(0))?;
let name_id = self.get_token_checked_with_val(Token::ID(CONST_IDX_PLACEHOLDER))?;
argname.push(name_id);
self.get_token_checked(Token::Colon)?;
ty_list.push(self.lex_ty(false)?);
Expand Down Expand Up @@ -828,7 +829,7 @@ impl<'a> ModuleUnit<'a> {
CustomFunction::new(
io,
argname,
self.token_lexer.borrow_mut().get_constpool().id_name[funcname].clone(),
self.token_lexer.borrow_mut().get_constpool().id_name[*funcname].clone(),
),
function_body,
);
Expand All @@ -852,11 +853,12 @@ impl<'a> ModuleUnit<'a> {
match t {
Token::Var => {
// 声明属性
let attr_name_tok = self.get_token_checked_with_val(Token::ID(0))?;
let attr_id = self.insert_sym_with_error(attr_name_tok)?;
let attr_name_tok =
self.get_token_checked_with_val(Token::ID(CONST_IDX_PLACEHOLDER))?;
let _attr_id = self.insert_sym_with_error(attr_name_tok)?;
self.get_token_checked(Token::Colon)?;
let ty = self.lex_ty(false)?;
class_obj.add_attr(attr_id, ty);
class_obj.add_attr(attr_name_tok, ty);
}
Token::Pub => {
let mut is_in_pub = true;
Expand Down Expand Up @@ -885,12 +887,12 @@ impl<'a> ModuleUnit<'a> {
self.self_scope.clone(),
))));
self.self_scope.borrow_mut().in_class = true;
let name = self.get_token_checked_with_val(Token::ID(0))?;
let name = self.get_token_checked_with_val(Token::ID(CONST_IDX_PLACEHOLDER))?;
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),
self.token_lexer.borrow_mut().get_constpool().id_name[name].clone(),
self.token_lexer.borrow_mut().get_constpool().id_name[*name].clone(),
);
self.get_token_checked(Token::LeftBigBrace)?;
self.lex_class_item_loop(&mut class_obj)?;
Expand All @@ -905,9 +907,9 @@ impl<'a> ModuleUnit<'a> {
}

fn lex_import_module(&mut self, istry: bool) -> AstError<()> {
let tok = self.get_token_checked_with_val(Token::StringValue(0))?;
let tok = self.get_token_checked_with_val(Token::StringValue(CONST_IDX_PLACEHOLDER))?;
// import的路径
let mut path_with_dot = self.token_lexer.borrow_mut().get_constpool().id_str[tok].clone();
let mut path_with_dot = self.token_lexer.borrow_mut().get_constpool().id_str[*tok].clone();
// 具体文件的路径
let mut import_file_path = String::new();
// 是不是dll
Expand Down Expand Up @@ -1015,7 +1017,7 @@ impl<'a> ModuleUnit<'a> {
// println!("{}", import_item_name);
let func_item = now.functions()[func_item].1.clone();
// println!("{}", func_item.get_name());
let token_idx: ConstPoolIndexTy = self
let token_idx: ConstPoolIndex = self
.token_lexer
.borrow_mut()
.add_id(func_item.get_name().to_owned());
Expand Down Expand Up @@ -1074,7 +1076,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 生成新建变量的指令
fn new_var(&mut self, name: ConstPoolIndexTy, varty: ClassIdxId) -> AstError<()> {
fn new_var(&mut self, name: ConstPoolIndex, varty: ClassIdxId) -> AstError<()> {
let sym_idx = self.insert_sym_with_error(name)?;
let (_var_sym, var_addr) =
self.self_scope
Expand All @@ -1086,7 +1088,7 @@ impl<'a> ModuleUnit<'a> {
Ok(())
}

fn store_var(&mut self, name: usize) -> RuntimeResult<()> {
fn store_var(&mut self, name: ConstPoolIndex) -> RuntimeResult<()> {
self.expr(false)?;
let var_type = match self.process_info.pop_last_ty() {
Some(v) => v,
Expand All @@ -1098,7 +1100,7 @@ impl<'a> ModuleUnit<'a> {
Ok(())
}

fn assign_var(&mut self, name: usize) -> RuntimeResult<()> {
fn assign_var(&mut self, name: ScopeAllocId) -> RuntimeResult<()> {
self.expr(false)?;
let var_type = match self.process_info.pop_last_ty() {
Some(v) => v,
Expand Down Expand Up @@ -1291,7 +1293,20 @@ impl<'a> ModuleUnit<'a> {
let tt = self.next_token()?;
match tt {
Token::Assign => {
self.assign_var(name)?;
let var_id = match self.self_scope.borrow().get_sym(name) {
Some(v) => v,
None => {
return self.gen_error(ErrorInfo::new(
t!(
SYMBOL_NOT_FOUND,
"0" = self.token_lexer.borrow_mut().get_constpool().id_name
[*name]
),
t!(SYMBOL_ERROR),
))
}
};
self.assign_var(var_id)?;
return Ok(());
}
Token::Store => {
Expand Down
13 changes: 7 additions & 6 deletions src/compiler/ast/ast_base.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::AstError;
use super::ClassIdxId;
use super::ModuleUnit;
use crate::compiler::scope::SymScope;
use crate::compiler::token::Token;
use crate::compiler::{scope::SymScope, token::ConstPoolIndexTy};
use crate::compiler::token::CONST_IDX_PLACEHOLDER;
use libcore::*;
use rust_i18n::t;
use std::{cell::RefCell, rc::Rc};
Expand Down Expand Up @@ -80,14 +81,14 @@ impl<'a> ModuleUnit<'a> {

/// 解析一个类型名为id
pub fn lex_ty(&mut self, istry: bool) -> AstError<ClassIdxId> {
let t = self.get_token_checked_with_val(Token::ID(0))?;
let t = self.get_token_checked_with_val(Token::ID(CONST_IDX_PLACEHOLDER))?;
let ty = match self.self_scope.borrow().get_type_id_by_token(t) {
None => self.try_err(
istry,
ErrorInfo::new(
t!(
SYMBOL_NOT_FOUND,
"0" = self.token_lexer.borrow_mut().get_constpool().id_name[t]
"0" = self.token_lexer.borrow_mut().get_constpool().id_name[*t]
),
t!(TYPE_ERROR),
),
Expand All @@ -111,7 +112,7 @@ impl<'a> ModuleUnit<'a> {
}

/// 获取一个token并检查该token是否正确,仅仅检查带有值的token类型
pub fn get_token_checked_with_val(&mut self, ty: Token) -> AstError<usize> {
pub fn get_token_checked_with_val(&mut self, ty: Token) -> AstError<ConstPoolIndex> {
let t = self.token_lexer.borrow_mut().next_token()?;
match (ty, t) {
(Token::ID(_), Token::ID(data)) => Ok(data),
Expand Down Expand Up @@ -224,13 +225,13 @@ impl<'a> ModuleUnit<'a> {
}

/// 添加一个符号,在符号冲突的时候报出错误
pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError<ScopeAllocId> {
pub fn insert_sym_with_error(&mut self, name: ConstPoolIndex) -> AstError<ScopeAllocId> {
match self.self_scope.borrow_mut().insert_sym(name) {
Some(v) => Ok(v),
None => self.gen_error(ErrorInfo::new(
t!(
SYMBOL_REDEFINED,
"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
Loading

0 comments on commit 27d3487

Please sign in to comment.