From 7f3c27e3275e1515ecb84db162b86134a45ea99c Mon Sep 17 00:00:00 2001 From: limuy Date: Thu, 1 Feb 2024 10:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E4=BA=8E=E4=BF=9D=E5=91=BD=E7=9A=84co?= =?UTF-8?q?mmit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/locales/zh_CN/LC_MESSAGES/trans.po | 9 +++ rust/src/base/error.rs | 3 + rust/src/base/stdlib.rs | 100 +++++++++++++++++++++++- rust/src/compiler/scope.rs | 31 +++++--- rust/src/tvm.rs | 2 +- rust/src/tvm/rustlib.rs | 1 - rust/src/tvm/stdlib.rs | 1 + rust/src/tvm/stdlib/prelude.rs | 6 ++ 8 files changed, 138 insertions(+), 15 deletions(-) delete mode 100644 rust/src/tvm/rustlib.rs create mode 100644 rust/src/tvm/stdlib.rs create mode 100644 rust/src/tvm/stdlib/prelude.rs diff --git a/rust/locales/zh_CN/LC_MESSAGES/trans.po b/rust/locales/zh_CN/LC_MESSAGES/trans.po index 588ea0b3..e3173c49 100644 --- a/rust/locales/zh_CN/LC_MESSAGES/trans.po +++ b/rust/locales/zh_CN/LC_MESSAGES/trans.po @@ -71,3 +71,12 @@ msgstr "类型错误" msgid "Type {} and {} are not the same" msgstr "类型{}和{}不同" +msgid "ArgumentError" +msgstr "参数错误" + +msgid "expect {}.But given {}" +msgstr "期望{}.但是输入{}" + +msgid "Expect type {}.But given type {}" +msgstr "期望类型{}.但是传入类型是{}" + diff --git a/rust/src/base/error.rs b/rust/src/base/error.rs index 0b46b3a4..1e3c9e82 100644 --- a/rust/src/base/error.rs +++ b/rust/src/base/error.rs @@ -9,6 +9,7 @@ pub const ZERO_DIVSION_ERROR: &str = "ZeroDivisionError"; pub const NUMBER_OVER_FLOW: &str = "NumberOverFlowError"; pub const SYMBOL_ERROR: &str = "SymbolError"; pub const TYPE_ERROR: &str = "TypeError"; +pub const ARGUMENT_ERROR: &str = "ArgumentError"; pub const STRING_WITHOUT_END: &str = "this string should be ended with {}"; pub const UNMATCHED_BRACE: &str = "{} is unmatched"; @@ -25,6 +26,8 @@ pub const IN_MODULE: &str = "In module {}"; pub const SYMBOL_NOT_FOUND: &str = "Symbol {} not found"; pub const SYMBOL_REDEFINED: &str = "Symbol {} redefined"; pub const TYPE_NOT_THE_SAME: &str = "Type {} and {} are not the same"; +pub const ARGU_NUMBER: &str = "expect {}.But given {}"; +pub const EXPECT_TYPE: &str = "Expect type {}.But given type {}"; #[derive(Debug)] pub struct ErrorInfo { diff --git a/rust/src/base/stdlib.rs b/rust/src/base/stdlib.rs index 8474a5bd..c347498f 100644 --- a/rust/src/base/stdlib.rs +++ b/rust/src/base/stdlib.rs @@ -1 +1,99 @@ -pub const STDLIB_LIST: Vec = vec![]; +use std::{collections::{HashMap, HashSet}, sync::Mutex}; +use lazy_static::lazy_static; +use crate::{base::error::{ARGUMENT_ERROR, ARGU_NUMBER, EXPECT_TYPE, SYNTAX_ERROR}, compiler::scope::{Type, TypeAllowNull}, tvm::DynaData}; + +use super::{codegen::Opcode, error::{ErrorInfo, RunResult}}; + +type StdlibFunc = fn(DynaData) -> RunResult<()>; + +#[derive(Hash, PartialEq, Eq)] +pub struct RustFunction { + pub name: String, + pub buildin_id: usize, + pub ptr : StdlibFunc, + pub argvs_type: Vec, + pub return_type: TypeAllowNull +} + +trait Function { + fn gen_code(&self) -> Opcode; + fn check_argvs(&self, argvs:Vec) -> Result<(), ErrorInfo> ; + fn get_return_type(&self) -> &TypeAllowNull; +} + +impl Function for RustFunction { + fn gen_code(&self) -> Opcode { + return Opcode::CallNative; + } + + fn check_argvs(&self, argvs:Vec) -> Result<(), ErrorInfo> { + if argvs.len() != self.argvs_type.len() { + return Err(ErrorInfo::new( + gettextrs::gettext!(ARGU_NUMBER, self.argvs_type.len(), argvs.len()), + gettextrs::gettext(ARGUMENT_ERROR) + )) + } + for i in 0..self.argvs_type.len() { + if argvs[i] != self.argvs_type[i] { + return Err(ErrorInfo::new( + gettextrs::gettext!(EXPECT_TYPE, self.argvs_type[i].origin_name, argvs[i].origin_name), + gettextrs::gettext(ARGUMENT_ERROR) + )) + } + } + Ok(()) + } + + fn get_return_type(&self) -> &TypeAllowNull { + &self.return_type + } +} + +pub struct RustClass { + pub name: String, + pub members: HashMap, + pub functions: HashSet +} + +lazy_static!{ + static ref STD_FUNC_ID: Mutex = Mutex::new(0); +} + +impl RustFunction { + pub fn new(name: String, ptr: StdlibFunc, argvs_type: Vec, return_type: TypeAllowNull) -> RustFunction { + let mut lock = STD_FUNC_ID.lock().unwrap(); + let tmp = *lock; + *lock += 1; + RustFunction { name, buildin_id:tmp, ptr , return_type, argvs_type} + } +} + +pub struct StdlibNode { + pub name: String, + pub sons: HashMap, + pub functions: HashSet +} + +impl StdlibNode { + pub fn new(name: String) -> StdlibNode { + StdlibNode { name, sons: HashMap::new() , functions: HashSet::new()} + } + + pub fn add_module(&mut self, name: String) { + self.sons.insert(name.clone(), StdlibNode::new(name)); + } + + pub fn add_function(&mut self, name: RustFunction) { + self.functions.insert(name); + } +} + +pub fn init() -> StdlibNode { + // init stdlib + let mut stdlib = StdlibNode::new("std".to_string()); + stdlib +} + +lazy_static! { +pub static ref STDLIB_LIST:StdlibNode = init(); +} diff --git a/rust/src/compiler/scope.rs b/rust/src/compiler/scope.rs index 8273720f..348485ea 100644 --- a/rust/src/compiler/scope.rs +++ b/rust/src/compiler/scope.rs @@ -1,6 +1,6 @@ use lazy_static::lazy_static; use std::{cell::RefCell, collections::HashMap, fmt::Display, rc::Rc}; - +use crate::base::stdlib::{StdlibNode, STDLIB_LIST}; use super::{BOOL_ID_POS, FLOAT_ID_POS, INT_ID_POS, STR_ID_POS}; lazy_static! { @@ -13,14 +13,14 @@ lazy_static! { ]; } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum TypeAllowNull { Yes(Type), No, } /// Manager of function -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Function { args_type: Vec, return_type: TypeAllowNull, @@ -37,31 +37,33 @@ impl Function { pub fn add_argv() {} } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Var { - ty: Function, + ty: Type, } impl Var { - pub fn new(ty: Function) -> Self { + pub fn new(ty: Type) -> Self { Self { ty } } } /// Manager of type -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Type { attr: Vec, funcs: Vec, pub name: usize, + pub origin_name: String } impl Type { - pub fn new(name: usize) -> Self { + pub fn new(name: usize, origin_name: String) -> Self { Self { attr: vec![], funcs: vec![], name, + origin_name } } @@ -74,10 +76,10 @@ impl Type { } } lazy_static! { - pub static ref INT_TYPE: Type = Type::new(INT_ID_POS); - pub static ref FLOAT_TYPE: Type = Type::new(INT_ID_POS); - pub static ref STR_TYPE: Type = Type::new(INT_ID_POS); - pub static ref BOOL_TYPE: Type = Type::new(INT_ID_POS); + pub static ref INT_TYPE: Type = Type::new(INT_ID_POS, "int".to_string()); + pub static ref FLOAT_TYPE: Type = Type::new(INT_ID_POS, "float".to_string()); + pub static ref STR_TYPE: Type = Type::new(INT_ID_POS, "str".to_string()); + pub static ref BOOL_TYPE: Type = Type::new(INT_ID_POS, "bool".to_string()); } pub struct SymScope { @@ -114,6 +116,11 @@ impl SymScope { } ret } + + /// import the module defined in rust + pub fn import_native_module(&mut self, stdlib:StdlibNode) { + + } pub fn has_sym(&self, id: usize) -> bool { if self.sym_map.contains_key(&id) { diff --git a/rust/src/tvm.rs b/rust/src/tvm.rs index 4271443c..bd899a1c 100644 --- a/rust/src/tvm.rs +++ b/rust/src/tvm.rs @@ -2,7 +2,7 @@ mod algo; mod def; mod function; mod gc; -mod rustlib; +mod stdlib; mod types; use self::types::trcfloat::TrcFloat; diff --git a/rust/src/tvm/rustlib.rs b/rust/src/tvm/rustlib.rs deleted file mode 100644 index 8b137891..00000000 --- a/rust/src/tvm/rustlib.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/rust/src/tvm/stdlib.rs b/rust/src/tvm/stdlib.rs new file mode 100644 index 00000000..883b169c --- /dev/null +++ b/rust/src/tvm/stdlib.rs @@ -0,0 +1 @@ +mod prelude; diff --git a/rust/src/tvm/stdlib/prelude.rs b/rust/src/tvm/stdlib/prelude.rs new file mode 100644 index 00000000..41121fe1 --- /dev/null +++ b/rust/src/tvm/stdlib/prelude.rs @@ -0,0 +1,6 @@ +use crate::{base::error::RunResult, tvm::DynaData}; + +pub fn tvm_print(dydata:DynaData) -> RunResult<()> { + Ok(()) +} +