From 3aa9bd57c4a504eb35515bbbf2f346c4214b4cb3 Mon Sep 17 00:00:00 2001 From: limuy Date: Mon, 5 Feb 2024 17:44:34 +0800 Subject: [PATCH] fix grammar lex --- rust/derive/src/def_module.rs | 14 +++++++++----- rust/src/compiler/ast.rs | 12 +++++++++++- rust/src/tvm/stdlib/prelude.rs | 6 +++++- rust/src/tvm/types.rs | 5 +++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/rust/derive/src/def_module.rs b/rust/derive/src/def_module.rs index 4d6651a5..dce9ff78 100644 --- a/rust/derive/src/def_module.rs +++ b/rust/derive/src/def_module.rs @@ -31,10 +31,8 @@ pub fn lex_arrow( left_name = TokenStream::new(); right_name = TokenStream::new(); control_which_put = false; - } else if x == "]" { - left_push.push(syn::parse(left_name).expect("left push break")); - right_push.push(syn::parse(right_name).expect("right push break")); - break; + } else { + panic!("{}", errormsg); } } else { if !control_which_put { @@ -44,6 +42,8 @@ pub fn lex_arrow( } } } + left_push.push(syn::parse(left_name).expect("left push break")); + right_push.push(syn::parse(right_name).expect("right push break")); } _ => { panic!("{}", errormsg); @@ -147,15 +147,19 @@ pub fn def_impl(content: TokenStream) -> TokenStream { use std::collections::hash_map::HashMap; let mut functions = HashMap::new(); let mut classes = HashMap::new(); + let mut submodules = HashMap::new(); #( functions.insert(stringify!(#right_func).to_string(), #left_func()); )* #( classes.insert(stringify!(#right_class).to_string(), #left_class::export_info()); )* + #( + submodules.insert(stringify!(#submodules).to_string(), #submodules::init()); + )* Stdlib::new( stringify!(#module_ident), - HashMap::new(), + submodules, functions, classes ) diff --git a/rust/src/compiler/ast.rs b/rust/src/compiler/ast.rs index cf1d8efe..2e413757 100644 --- a/rust/src/compiler/ast.rs +++ b/rust/src/compiler/ast.rs @@ -638,7 +638,17 @@ mod tests { t.staticdata.inst, vec![ Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::CallNative, 0), + Inst::new( + Opcode::CallNative, + STDLIB_ROOT + .sub_modules + .get("prelude") + .unwrap() + .functions + .get("print") + .unwrap() + .buildin_id + ), ] ) } diff --git a/rust/src/tvm/stdlib/prelude.rs b/rust/src/tvm/stdlib/prelude.rs index 8c28c1b5..6dbfed25 100644 --- a/rust/src/tvm/stdlib/prelude.rs +++ b/rust/src/tvm/stdlib/prelude.rs @@ -1,3 +1,4 @@ +use super::super::types::*; use crate::base::stdlib::*; use crate::{base::error::*, tvm::DynaData}; use derive::{def_module, trc_function}; @@ -13,5 +14,8 @@ pub fn println(obj: any) -> void { } def_module!(module_name = prelude, functions = [print => print, println => print], classes = [ - Trcint => int + TrcInt => int, + TrcStr => str, + TrcBool => bool, + TrcFloat => float ]); diff --git a/rust/src/tvm/types.rs b/rust/src/tvm/types.rs index 312a2cf3..c7636edb 100644 --- a/rust/src/tvm/types.rs +++ b/rust/src/tvm/types.rs @@ -10,6 +10,11 @@ pub mod trcfloat; pub mod trcint; pub mod trcstr; +pub use trcbool::TrcBool; +pub use trcfloat::TrcFloat; +pub use trcint::TrcInt; +pub use trcstr::TrcStr; + /// help to generate the same error reporter functions macro_rules! batch_unsupported_operators { ($($traie_name:ident => $oper_name:expr),*) => {