mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
fix: jump wrong pos, verbose arg
This commit is contained in:
parent
697012f0ac
commit
93f8f57db8
|
@ -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);
|
||||
},
|
||||
|
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue