1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-09-28 05:07:39 +00:00

fix: jump wrong pos, verbose arg

This commit is contained in:
Natapat Samutpong 2022-01-27 12:42:55 +07:00
parent 697012f0ac
commit 93f8f57db8
3 changed files with 21 additions and 9 deletions

View file

@ -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<PathBuf>, 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);
},

View file

@ -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(),
}
}

View file

@ -36,15 +36,17 @@ impl VM {
}
}
pub fn run(&mut self, instrs: Vec<Instr>) -> VMReturn {
pub fn run(&mut self, instrs: Vec<Instr>, 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;
},