diff --git a/examples/skip_test b/examples/skip_test index 36d58470..c199a19f 100644 --- a/examples/skip_test +++ b/examples/skip_test @@ -1 +1 @@ -import_user_defined \ No newline at end of file +import_user_defined diff --git a/libcore/src/libbasic.rs b/libcore/src/libbasic.rs index 06dce6a7..51dfe00b 100644 --- a/libcore/src/libbasic.rs +++ b/libcore/src/libbasic.rs @@ -16,8 +16,7 @@ pub type TypeAllowNull = Option; 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 { @@ -54,7 +53,7 @@ pub enum OverrideOperations { Power, } -pub type ArgsNameTy = Vec; +pub type ArgsNameTy = Vec; pub type Argvs = Vec; #[derive(Clone, Debug)] @@ -128,7 +127,7 @@ impl_downcast!(FunctionInterface); pub trait ClassInterface: Downcast + Sync + Send + Debug + Display { fn has_func(&self, funcname: &str) -> Option>; - fn has_attr(&self, attrname: usize) -> bool; + fn has_attr(&self, attrname: ConstPoolIndex) -> bool; fn get_id(&self) -> ClassIdxId; @@ -185,7 +184,7 @@ pub struct RustClass { pub id: ClassIdxId, /// 类名 pub name: &'static str, - pub id_to_var: HashMap, + pub id_to_var: HashMap, } /// 约定,0号id是any类型 @@ -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) } diff --git a/src/compiler.rs b/src/compiler.rs index 252dfe88..690b51ee 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -107,7 +107,7 @@ impl CompileOption { } } -type Pool = HashMap; +type Pool = HashMap; #[derive(Default)] /// 管理常量池添加删除 @@ -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 [](&mut self, val: $type) -> usize { + pub fn [](&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 [](&self, val: &$type) -> Option { + pub fn [](&self, val: &$type) -> Option { self.$const_pool.get(val).copied() } } }; ($func_name:ident => ($const_pool:ident, $id_pool:ident, $type:ty, $convert_func: ident)) => { paste::paste! { - pub fn [](&mut self, val: $type) -> usize { + pub fn [](&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 [](&self, val: &$type) -> Option { + pub fn [](&self, val: &$type) -> Option { self.$const_pool.get(val).copied() } } @@ -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"); } diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index ee3aa4e8..3c737b33 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -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::*; @@ -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) { @@ -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), ), @@ -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), ), @@ -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), ), @@ -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); } _ => { @@ -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); @@ -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)?; @@ -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)?); @@ -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, ); @@ -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; @@ -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)?; @@ -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 @@ -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()); @@ -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 @@ -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, @@ -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, @@ -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 => { diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index b09c49e8..ab0cf894 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -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}; @@ -80,14 +81,14 @@ impl<'a> ModuleUnit<'a> { /// 解析一个类型名为id pub fn lex_ty(&mut self, istry: bool) -> AstError { - 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), ), @@ -111,7 +112,7 @@ impl<'a> ModuleUnit<'a> { } /// 获取一个token并检查该token是否正确,仅仅检查带有值的token类型 - pub fn get_token_checked_with_val(&mut self, ty: Token) -> AstError { + pub fn get_token_checked_with_val(&mut self, ty: Token) -> AstError { let t = self.token_lexer.borrow_mut().next_token()?; match (ty, t) { (Token::ID(_), Token::ID(data)) => Ok(data), @@ -224,13 +225,13 @@ impl<'a> ModuleUnit<'a> { } /// 添加一个符号,在符号冲突的时候报出错误 - pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError { + pub fn insert_sym_with_error(&mut self, name: ConstPoolIndex) -> AstError { 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), ))?, diff --git a/src/compiler/linker.rs b/src/compiler/linker.rs index 3be042f7..a237a709 100644 --- a/src/compiler/linker.rs +++ b/src/compiler/linker.rs @@ -29,13 +29,13 @@ pub fn link<'a>(data_iter: impl Iterator) -> StaticDa let mut added = j.clone(); match j.opcode { Opcode::LoadString => { - added.operand.0 = value_pool + added.operand.0 = *value_pool .get_string(&i.constpool.stringpool[added.operand.0 as usize]) .unwrap() as Opidx } Opcode::LoadFloat => { // TODO:improve the performance - added.operand.0 = value_pool + added.operand.0 = *value_pool .get_float(&i.constpool.floatpool[added.operand.0 as usize].to_string()) .unwrap() as Opidx } diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index b2eae02c..c4a52118 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -1,7 +1,4 @@ -use super::{ - token::{ConstPoolIndexTy, Token}, - ValuePool, -}; +use super::{token::Token, ValuePool}; use libcore::*; use std::{ cell::RefCell, @@ -62,7 +59,7 @@ pub struct CustomType { funcs: Vec, pub name: ClassIdxId, pub origin_name: String, - pub id_to_attr: HashMap, + pub id_to_attr: HashMap, } impl Display for CustomType { @@ -80,7 +77,7 @@ impl CustomType { } } - pub fn add_attr(&mut self, attrname: ScopeAllocId, attrty: ClassIdxId) -> Option { + pub fn add_attr(&mut self, attrname: ConstPoolIndex, attrty: ClassIdxId) -> Option { self.id_to_attr.insert(attrname, attrty) } @@ -99,7 +96,7 @@ impl ClassInterface for CustomType { None } - fn has_attr(&self, attrname: usize) -> bool { + fn has_attr(&self, attrname: ConstPoolIndex) -> bool { self.id_to_attr.contains_key(&attrname) } @@ -197,7 +194,7 @@ pub struct SymScope { // 父作用域 pub prev_scope: RootOnlyInfo>>, // 管理符号之间的映射,由token在name pool中的id映射到符号表中的id - sym_map: HashMap, + sym_map: HashMap, // 当前作用域要分配的下一个ID,也就是当前作用域的最大id+1 scope_sym_id: ScopeAllocId, // ID到class id的映射 @@ -268,7 +265,7 @@ impl SymScope { pub fn insert_sym_with_error( &mut self, - sym_name: ConstPoolIndexTy, + sym_name: ConstPoolIndex, str_name: &str, ) -> Result { match self.insert_sym(sym_name) { @@ -365,7 +362,7 @@ impl SymScope { } } - pub fn has_sym(&self, id: usize) -> bool { + pub fn has_sym(&self, id: ConstPoolIndex) -> bool { if self.sym_map.contains_key(&id) { return true; } @@ -381,7 +378,7 @@ impl SymScope { ret } - pub fn insert_sym(&mut self, id: usize) -> Option { + pub fn insert_sym(&mut self, id: ConstPoolIndex) -> Option { let t = self.sym_map.insert(id, self.scope_sym_id); self.scope_sym_id += 1; // 先前不能存在 @@ -402,7 +399,7 @@ impl SymScope { } } - pub fn get_sym(&self, id: usize) -> Option { + pub fn get_sym(&self, id: ConstPoolIndex) -> Option { let t = self.sym_map.get(&id); match t { None => match self.prev_scope { @@ -425,7 +422,7 @@ impl SymScope { pub fn import_extern_func( &mut self, mut f: RustFunction, - tokenid: usize, + tokenid: ConstPoolIndex, fname: &str, extern_function_id: FuncIdx, storage: &ModuleStorage, @@ -521,7 +518,7 @@ impl SymScope { Ok(ret) } - pub fn get_type_id_by_token(&self, ty_name: ConstPoolIndexTy) -> Option { + pub fn get_type_id_by_token(&self, ty_name: ConstPoolIndex) -> Option { let ty_idx_id = self.get_sym(ty_name).unwrap(); self.get_type_id(ty_idx_id) } @@ -559,11 +556,11 @@ mod tests { #[test] fn test_scope() { let root_scope = Rc::new(RefCell::new(SymScope::new(SymScopePrev::Root))); - root_scope.borrow_mut().insert_sym(1); + root_scope.borrow_mut().insert_sym(ConstPoolIndex(1)); let mut son_scope = SymScope::new(SymScopePrev::Prev(root_scope.clone())); - son_scope.insert_sym(2); - assert_eq!(son_scope.get_sym(2), Some(1)); + son_scope.insert_sym(ConstPoolIndex(2)); + assert_eq!(son_scope.get_sym(ConstPoolIndex(2)), Some(1)); drop(son_scope); - assert_eq!(root_scope.borrow().get_sym(1), Some(0)); + assert_eq!(root_scope.borrow().get_sym(ConstPoolIndex(1)), Some(0)); } } diff --git a/src/compiler/token.rs b/src/compiler/token.rs index 9d33a650..997b81e1 100644 --- a/src/compiler/token.rs +++ b/src/compiler/token.rs @@ -5,12 +5,14 @@ use logos::{Lexer, Logos, Source}; use rust_i18n::t; use std::{cell::RefCell, fmt::Display, rc::Rc}; -fn add_float(lex: &mut Lexer) -> usize { +fn add_float(lex: &mut Lexer) -> ConstPoolIndex { // println!("{}", lex.slice()); lex.extras.add_float(lex.slice().to_owned()) } -fn add_string(lex: &mut Lexer) -> usize { +pub const CONST_IDX_PLACEHOLDER: ConstPoolIndex = ConstPoolIndex(0); + +fn add_string(lex: &mut Lexer) -> ConstPoolIndex { // 此处还需要进行字符串 let origin_str = lex.slice(); let mut target_string = String::new(); @@ -50,7 +52,7 @@ fn add_string(lex: &mut Lexer) -> usize { lex.extras.add_string(target_string) } -fn add_id(lex: &mut Lexer) -> usize { +fn add_id(lex: &mut Lexer) -> ConstPoolIndex { lex.extras.add_id(lex.slice().to_owned()) } @@ -77,10 +79,10 @@ fn lex_muli_comment(lex: &mut Lexer) -> Result { Ok(lines) } -fn convert_int(lex: &mut Lexer) -> usize { +fn convert_int(lex: &mut Lexer) -> ConstPoolIndex { // println!("{}", lex.slice()); let str_tmp = lex.slice().replace("_", ""); - if let Some(s) = str_tmp.strip_prefix("0x") { + ConstPoolIndex(if let Some(s) = str_tmp.strip_prefix("0x") { let val: i64 = i64::from_str_radix(s, 16).unwrap(); convert_int_constval_to_usize(val) } else if let Some(s) = str_tmp.strip_prefix("0b") { @@ -92,17 +94,17 @@ fn convert_int(lex: &mut Lexer) -> usize { } else { let val: i64 = str_tmp.parse().unwrap(); convert_int_constval_to_usize(val) - } + }) } -fn convert_char(lex: &mut Lexer) -> Result { +fn convert_char(lex: &mut Lexer) -> Result { let len = lex.slice().len(); if len != 3 { return Err(ErrorInfo::new(t!(CHAR_FORMAT), t!(SYNTAX_ERROR))); } let mut iter = lex.slice().chars(); iter.next(); - Ok(iter.next().unwrap() as usize) + Ok(ConstPoolIndex(iter.next().unwrap() as usize)) } #[derive(PartialEq, Debug, Clone, Hash, Eq, Copy, Logos)] @@ -187,19 +189,19 @@ pub enum Token { r#"(0[bB][01_]+|0[oO][0-7_]+|0[xX][0-9a-fA-F_]+|[0-9][0-9_]*)"#, convert_int )] - IntValue(usize), + IntValue(ConstPoolIndex), #[regex(r#""[^"]*""#, add_string)] // #[regex(r#""""(.)*""""#, add_string)] // TODO:MULT - StringValue(usize), + StringValue(ConstPoolIndex), #[regex(r#"((\d+)\.(\d+))|(((\d+)|(\d+\.\d+))[eE][-+]?\d+)?"#, add_float)] - FloatValue(usize), + FloatValue(ConstPoolIndex), LongIntValue, #[regex(r#"'[^']*'"#, convert_char)] - CharValue(usize), - #[token("false", |_| false as usize)] - #[token("true", |_| true as usize)] - BoolValue(usize), + CharValue(ConstPoolIndex), + #[token("false", |_| ConstPoolIndex(false as usize))] + #[token("true", |_| ConstPoolIndex(true as usize))] + BoolValue(ConstPoolIndex), #[token("||=")] SelfOr, #[token("&&=")] @@ -231,7 +233,7 @@ pub enum Token { #[token(";")] Semicolon, #[regex(r#"[\p{XID_Start}_]\p{XID_Continue}*"#, add_id)] - ID(usize), + ID(ConstPoolIndex), #[token("while")] While, #[token("for")] @@ -314,8 +316,6 @@ impl Token { } } -pub type ConstPoolIndexTy = usize; - impl Display for Token { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let res = match self { @@ -582,7 +582,7 @@ impl<'a> TokenLex<'a> { Ok(()) } - pub fn add_id(&mut self, id: String) -> usize { + pub fn add_id(&mut self, id: String) -> ConstPoolIndex { self.internal_lexer.extras.add_id(id) } @@ -697,20 +697,20 @@ mod tests { Token::Comma, Token::Dot, Token::Comma, - Token::IntValue(convert_int_constval_to_usize(100)), - Token::FloatValue(0), - Token::IntValue(convert_int_constval_to_usize(232_304904)), - Token::IntValue(convert_int_constval_to_usize(0b011)), - Token::IntValue(convert_int_constval_to_usize(0x2AA4)), - Token::IntValue(convert_int_constval_to_usize(0o2434)), - Token::IntValue(convert_int_constval_to_usize(0)), - Token::IntValue(convert_int_constval_to_usize(0)), - Token::FloatValue(1), - Token::FloatValue(2), - Token::FloatValue(3), - Token::FloatValue(4), - Token::FloatValue(5), - Token::FloatValue(6), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(100))), + Token::FloatValue(ConstPoolIndex(0)), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(232_304904))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0b011))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0x2AA4))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0o2434))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0))), + Token::FloatValue(ConstPoolIndex(1)), + Token::FloatValue(ConstPoolIndex(2)), + Token::FloatValue(ConstPoolIndex(3)), + Token::FloatValue(ConstPoolIndex(4)), + Token::FloatValue(ConstPoolIndex(5)), + Token::FloatValue(ConstPoolIndex(6)), ], ); check_pool( @@ -763,13 +763,13 @@ mod tests { check( &mut t, vec![ - Token::StringValue(0), - Token::CharValue('s' as usize), - Token::StringValue(1), - Token::StringValue(2), - Token::StringValue(3), - Token::StringValue(4), - Token::StringValue(5), + Token::StringValue(ConstPoolIndex(0)), + Token::CharValue(ConstPoolIndex('s' as usize)), + Token::StringValue(ConstPoolIndex(1)), + Token::StringValue(ConstPoolIndex(2)), + Token::StringValue(ConstPoolIndex(3)), + Token::StringValue(ConstPoolIndex(4)), + Token::StringValue(ConstPoolIndex(5)), ], ); check_pool( @@ -820,52 +820,52 @@ mod tests { &mut t, vec![ Token::Import, - Token::StringValue(0), + Token::StringValue(ConstPoolIndex(0)), Token::Func, - Token::ID(0), + Token::ID(ConstPoolIndex(0)), Token::LeftSmallBrace, - Token::ID(1), - Token::ID(2), + Token::ID(ConstPoolIndex(1)), + Token::ID(ConstPoolIndex(2)), Token::RightSmallBrace, Token::Arrow, - Token::ID(3), + Token::ID(ConstPoolIndex(3)), Token::LeftBigBrace, Token::If, - Token::ID(2), + Token::ID(ConstPoolIndex(2)), Token::Mod, - Token::IntValue(2), + Token::IntValue(ConstPoolIndex(2)), Token::Equal, - Token::IntValue(convert_int_constval_to_usize(0)), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0))), Token::LeftBigBrace, Token::Return, - Token::StringValue(1), + Token::StringValue(ConstPoolIndex(1)), Token::RightBigBrace, Token::Else, Token::LeftBigBrace, Token::Return, - Token::StringValue(2), + Token::StringValue(ConstPoolIndex(2)), Token::RightBigBrace, Token::RightBigBrace, Token::Func, - Token::ID(4), + Token::ID(ConstPoolIndex(4)), Token::LeftSmallBrace, Token::RightSmallBrace, Token::LeftBigBrace, - Token::ID(5), + Token::ID(ConstPoolIndex(5)), Token::LeftSmallBrace, - Token::StringValue(3), + Token::StringValue(ConstPoolIndex(3)), Token::RightSmallBrace, - Token::ID(6), + Token::ID(ConstPoolIndex(6)), Token::Store, - Token::ID(0), + Token::ID(ConstPoolIndex(0)), Token::LeftSmallBrace, - Token::ID(7), + Token::ID(ConstPoolIndex(7)), Token::LeftSmallBrace, Token::RightSmallBrace, Token::RightSmallBrace, - Token::ID(5), + Token::ID(ConstPoolIndex(5)), Token::LeftSmallBrace, - Token::ID(6), + Token::ID(ConstPoolIndex(6)), Token::RightSmallBrace, Token::RightBigBrace, ], @@ -878,12 +878,12 @@ mod tests { check( &mut t, vec![ - Token::ID(0), - Token::ID(1), - Token::ID(2), - Token::ID(3), - Token::ID(4), - Token::ID(5), + Token::ID(ConstPoolIndex(0)), + Token::ID(ConstPoolIndex(1)), + Token::ID(ConstPoolIndex(2)), + Token::ID(ConstPoolIndex(3)), + Token::ID(ConstPoolIndex(4)), + Token::ID(ConstPoolIndex(5)), ], ); check_pool( @@ -905,8 +905,8 @@ mod tests { check( &mut t, vec![ - Token::IntValue(convert_int_constval_to_usize(1)), - Token::IntValue(convert_int_constval_to_usize(23)), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(1))), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(23))), ], ); } @@ -917,8 +917,8 @@ mod tests { check( &mut t, vec![ - Token::IntValue(convert_int_constval_to_usize(0xabc)), - Token::ID(0), + Token::IntValue(ConstPoolIndex(convert_int_constval_to_usize(0xabc))), + Token::ID(ConstPoolIndex(0)), ], ); check_pool(vec!["hds".to_string()], &t.internal_lexer.extras.name_pool); @@ -960,7 +960,11 @@ mod tests { gen_test_token_env!(r#"true tru false"#, t); check( &mut t, - vec![Token::BoolValue(1), Token::ID(0), Token::BoolValue(0)], + vec![ + Token::BoolValue(ConstPoolIndex(1)), + Token::ID(ConstPoolIndex(0)), + Token::BoolValue(ConstPoolIndex(0)), + ], ) } diff --git a/tests/test_all_examples.rs b/tests/test_all_examples.rs index 9da8af81..875e13c2 100644 --- a/tests/test_all_examples.rs +++ b/tests/test_all_examples.rs @@ -1,7 +1,9 @@ //! this test all examples files and check them output use assert_cmd::Command; +use core::panic; use libcore::utils::get_next_check_char; +use logos::Source; use std::fs::read_to_string; /// 检查迭代器是否剩下的所有字符都满足某个条件 @@ -52,11 +54,16 @@ pub fn test_run_examples() { Ok(s) => { let mut ret = vec![]; for i in s.split("\n") { - ret.push(i.to_owned()); + let tmp = i.trim(); + if tmp.is_empty() { + continue; + } + ret.push(tmp.to_owned()); } ret } }; + // panic!("x{}x", skip_list[0]); for entry in std::fs::read_dir("examples").unwrap() { let path = entry.unwrap().path(); if path.is_file()