From b30f3e84bfe6bd5103994fe1180df32ca117363d Mon Sep 17 00:00:00 2001 From: limuy Date: Thu, 1 Feb 2024 22:20:10 +0800 Subject: [PATCH] =?UTF-8?q?hello=20world=E6=88=90=E5=8A=9F=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/compiler.rs | 12 ++++++++++++ rust/src/compiler/ast.rs | 4 ++-- rust/src/lib.rs | 35 ++++++++++++++++++++++++---------- rust/src/tvm/stdlib/prelude.rs | 3 ++- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/rust/src/compiler.rs b/rust/src/compiler.rs index 956dc171..6b492b73 100644 --- a/rust/src/compiler.rs +++ b/rust/src/compiler.rs @@ -107,6 +107,9 @@ impl Float { for _ in 0..len { float_part /= 10.0; } + for _ in 0..self.zero { + float_part /= 10.0; + } self.front as f64 + float_part } } @@ -188,6 +191,15 @@ impl ValuePool { for i in &self.const_ints { ret.intpool[*i.1] = *i.0; } + ret.floatpool.resize(self.const_floats.len(), 0.0); + for i in &self.const_floats { + ret.floatpool[*i.1] = i.0.to_float(); + } + ret.stringpool + .resize(self.const_strings.len(), "".to_string()); + for i in &self.const_strings { + ret.stringpool[*i.1] = (i.0).clone(); + } ret } } diff --git a/rust/src/compiler/ast.rs b/rust/src/compiler/ast.rs index 6e9e192f..bdc229bd 100644 --- a/rust/src/compiler/ast.rs +++ b/rust/src/compiler/ast.rs @@ -128,8 +128,8 @@ impl<'a> AstBuilder<'a> { ExprGen!(expr1, expr1_, expr2, TokenType::And => Opcode::And); ExprGen!(expr, expr_, expr1, TokenType::Or => Opcode::Or); - pub fn return_static_data(self) -> StaticData { - self.token_lexer.compiler_data.const_pool.store_val_to_vm(); + pub fn return_static_data(mut self) -> StaticData { + self.staticdata.constpool = self.token_lexer.compiler_data.const_pool.store_val_to_vm(); self.staticdata } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 93f10144..cb66488d 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -18,24 +18,35 @@ use std::error::Error; struct Args { #[command(subcommand)] mode: Commands, - #[arg()] - files: Vec, } #[derive(Debug, Subcommand)] enum Commands { - Build { optimize: bool }, + Build { + #[arg(short, long, default_value_t = false)] + optimize: bool, + #[arg()] + files: Vec, + }, Tshell {}, Update {}, - Run { optimize: bool }, + Run { + #[arg(short, long, default_value_t = false)] + optimize: bool, + #[arg()] + files: Vec, + }, } pub fn run() -> Result<(), Box> { let cli = Args::parse(); match cli.mode { - Commands::Build { optimize: opt } => { - for i in cli.files { - tools::compile(compiler::Option::new(opt, compiler::InputSource::File(i))); + Commands::Build { + optimize: opt, + files: files, + } => { + for i in files { + tools::compile(compiler::Option::new(false, compiler::InputSource::File(i))); } } Commands::Tshell {} => { @@ -47,9 +58,13 @@ pub fn run() -> Result<(), Box> { } Ok(_) => {} }, - Commands::Run { optimize: opt } => { - for i in cli.files { - match tools::run::run(compiler::Option::new(opt, compiler::InputSource::File(i))) { + Commands::Run { + optimize: opt, + files: files, + } => { + for i in files { + match tools::run::run(compiler::Option::new(false, compiler::InputSource::File(i))) + { Ok(_) => {} Err(c) => { eprintln!("{}", c); diff --git a/rust/src/tvm/stdlib/prelude.rs b/rust/src/tvm/stdlib/prelude.rs index 9ee7648e..b7179e75 100644 --- a/rust/src/tvm/stdlib/prelude.rs +++ b/rust/src/tvm/stdlib/prelude.rs @@ -1,6 +1,7 @@ use crate::{base::error::RunResult, tvm::DynaData}; pub fn tvm_print(dydata: &mut DynaData) -> RunResult<()> { - print!("win!"); + let obj = dydata.obj_stack.pop(); + print!("{}", obj.unwrap()); Ok(()) }