From fa73489d0823ac5a072ac128dc0efb2d52d9484b Mon Sep 17 00:00:00 2001 From: tiye Date: Sat, 20 Jul 2024 20:29:02 +0800 Subject: [PATCH] switch to argh --- .github/workflows/test.yaml | 1 + .gitignore | 2 ++ Cargo.toml | 8 ++++---- src/bin/cli.rs | 29 +++++++++++++++-------------- src/parser.rs | 2 +- src/vm.rs | 2 +- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f31a2c6..f4b1acb 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -2,6 +2,7 @@ on: push: branches: - master + - main pull_request: {} name: Test diff --git a/.gitignore b/.gitignore index eb86e63..404cc81 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ Cargo.lock profile.json + +.DS_Store diff --git a/Cargo.toml b/Cargo.toml index 03024d5..97f4b94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,11 +18,11 @@ path = "src/bin/cli.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cirru_parser = "0.1.28" -regex = "1.10.2" -lazy_static = "1.4.0" -clap = { version = "4.4.18", features = ["derive"] } +cirru_parser = "0.1.31" +regex = "1.10.5" +lazy_static = "1.5.0" bincode = "2.0.0-rc.3" +argh = "0.1.12" # [target.'cfg(not(target_env = "msvc"))'.dependencies] # tikv-jemallocator = "0.5" diff --git a/src/bin/cli.rs b/src/bin/cli.rs index bb5c7a2..fe09047 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -2,8 +2,8 @@ use std::fs; use std::time::Instant; use std::{collections::hash_map::HashMap, rc::Rc}; +use argh::FromArgs; use cirru_parser::{parse, Cirru}; -use clap::{arg, Parser}; use calx_vm::{log_calx_value, parse_function, Calx, CalxFunc, CalxImportsDict, CalxVM}; @@ -23,27 +23,28 @@ pub struct CalxBinaryProgram { pub fns: Vec, } -/// Simple program to greet a person -#[derive(Parser, Debug)] -#[command(name = "Calx VM")] -#[command(author = "Jon Chen ")] -#[command(version = "0.1.6")] -#[command(about = "A toy VM", long_about = None)] -struct Args { - #[arg(short, long, value_name = "SHOW_CODE")] +#[derive(FromArgs)] +/// Calx VM args +struct TopLevel { + /// show code + #[argh(switch, short = 's')] show_code: bool, - #[arg(short, long, value_name = "EMIT_BINARY")] + /// emit binary + #[argh(option, short = 'b')] emit_binary: Option, - #[arg(short, long, value_name = "VERBOSE")] + /// verbose + #[argh(switch, short = 'v')] verbose: bool, - #[arg(long, value_name = "EVAL_BINARY")] + /// eval binary + #[argh(switch, short = 'e')] eval_binary: bool, - #[arg(value_name = "SOURCE")] + /// source + #[argh(positional)] source: String, } fn main() -> Result<(), String> { - let args = Args::parse(); + let args: TopLevel = argh::from_env(); let source = args.source; let show_code = args.show_code; diff --git a/src/parser.rs b/src/parser.rs index 7f17f1f..34d6a11 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -56,7 +56,7 @@ pub fn parse_function(nodes: &[Cirru]) -> Result { } Ok(CalxFunc { - name: Rc::from(name), + name, params_types: params_types.into(), ret_types: Rc::new(ret_types), local_names: Rc::new(locals_collector.locals), diff --git a/src/vm.rs b/src/vm.rs index d583782..4b17b59 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -196,7 +196,7 @@ impl CalxVM { if *idx >= self.top_frame.locals.len() { return Err(self.gen_err(format!("out of bound in local.tee {}", idx))); } else { - self.top_frame.locals[*idx] = v.to_owned() + v.clone_into(&mut self.top_frame.locals[*idx]) } self.stack_push(v); }