From 93f8f57db8594a88dc957185c7f4ffe611817e83 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 12:42:55 +0700 Subject: [PATCH] fix: jump wrong pos, verbose arg --- blspc/src/main.rs | 15 +++++++++++---- blspc/src/vm/instr.rs | 5 ++++- blspc/src/vm/vm.rs | 10 ++++++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/blspc/src/main.rs b/blspc/src/main.rs index fc75fac..9c78776 100644 --- a/blspc/src/main.rs +++ b/blspc/src/main.rs @@ -17,6 +17,13 @@ use vm::{vm::VM, parser::parse_instr}; fn main() { let start = Instant::now(); let args = Args::from_args(); + + let debug = match args.verbose { + 0 => false, + 1 => true, + 2 => true, + _ => true, + }; match (args.compile, args.run) { (true, true) => { @@ -31,7 +38,7 @@ fn main() { // Run (false, true) => { let src = read_to_string(&args.file).unwrap(); - run_src(src); + run_src(src, debug); }, (false, false) => { if args.file.extension() == Some("blsp".as_ref()) { @@ -39,7 +46,7 @@ fn main() { compile_src(src, args.output, args.file, start); } else if args.file.extension() == Some("bsm".as_ref()) { let src = read_to_string(&args.file).unwrap(); - run_src(src); + run_src(src, debug); } else { panic!("No mode specified"); } @@ -84,10 +91,10 @@ fn compile_src(src: String, path: Option, file: PathBuf, start: Instant } } -fn run_src(src: String) { +fn run_src(src: String, debug: bool) { let instrs = parse_instr(&src); let mut vm = VM::new(); - match vm.run(instrs) { + match vm.run(instrs, debug) { Ok(()) => { exit(0); }, diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index 1d7ddb9..a6ed1f2 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -34,7 +34,10 @@ impl Type { match self { Type::Int(i) => i.to_string(), Type::Float(f) => f.to_string(), - Type::Boolean(b) => b.to_string(), + Type::Boolean(b) => match b { + true => "true".to_string(), + false => "false".to_string(), + }, Type::String(s) => s.clone(), } } diff --git a/blspc/src/vm/vm.rs b/blspc/src/vm/vm.rs index f459187..ecb39a2 100644 --- a/blspc/src/vm/vm.rs +++ b/blspc/src/vm/vm.rs @@ -36,15 +36,17 @@ impl VM { } } - pub fn run(&mut self, instrs: Vec) -> VMReturn { + pub fn run(&mut self, instrs: Vec, debug: bool) -> VMReturn { 'tco: loop { self.instr_pointer += 1; if self.instr_pointer - 1 == instrs.len() as isize { - dbg!("VM: HALT"); return Ok(()); } let instr = &instrs[self.instr_pointer as usize - 1]; + if debug { + println!("ptr: {} | stack: {:?} | curr: {}", self.instr_pointer - 1, &self.stack, &instr); + } match instr { Store { address, value, .. } => { self.store(&address, &value)?; @@ -90,13 +92,13 @@ impl VM { continue 'tco; }, Jump { to, .. } => { - self.instr_pointer = *to as isize; + self.instr_pointer = *to as isize - 1; continue 'tco; }, PopJumpIfFalse { to, .. } => { let value = self.stack.pop().unwrap(); if !value.as_bool() { - self.instr_pointer = *to as isize; + self.instr_pointer = *to as isize - 1; } continue 'tco; },