fixed bugs

This commit is contained in:
nora 2021-09-17 16:12:39 +02:00
parent b6849d9238
commit 7506fc7478
3 changed files with 31 additions and 29 deletions

View file

@ -25,10 +25,10 @@ impl Vm {
Stmt::Dec(r) => self.registers[r] -= 1, Stmt::Dec(r) => self.registers[r] -= 1,
Stmt::IsZero(r, line) => { Stmt::IsZero(r, line) => {
if self.registers[r] == 0 { if self.registers[r] == 0 {
self.pc = line; self.pc = line - 1;
} }
} }
Stmt::Jump(line) => self.pc = line, Stmt::Jump(line) => self.pc = line - 1,
Stmt::Stop => return VmState::Stop, Stmt::Stop => return VmState::Stop,
} }
if self.breakpoints.contains(&self.pc) { if self.breakpoints.contains(&self.pc) {
@ -39,8 +39,12 @@ impl Vm {
} }
fn run(&mut self) -> VmState { fn run(&mut self) -> VmState {
while let VmState::Run = self.step() {} loop {
VmState::Break match self.step() {
state @ (VmState::Break | VmState::Stop) => return state,
_ => {}
}
}
} }
} }
@ -49,14 +53,15 @@ enum VmInstruction {
Step, Step,
Run, Run,
Break(usize), Break(usize),
Set(usize, usize),
} }
pub fn run(stmts: Vec<Stmt>) { pub fn run(stmts: Vec<Stmt>) {
let max_register = max_register(&stmts); let max_register_index = max_register(&stmts);
let mut vm = Vm { let mut vm = Vm {
stmts, stmts,
pc: 0, pc: 0,
registers: vec![0; max_register], registers: vec![0; max_register_index + 1],
breakpoints: vec![], breakpoints: vec![],
}; };
@ -80,6 +85,7 @@ pub fn run(stmts: Vec<Stmt>) {
} }
} }
} }
VmInstruction::Set(r, value) => vm.registers[r] = value,
} }
} }
} }
@ -104,6 +110,10 @@ fn debug_input(vm: &Vm) -> VmInstruction {
}, },
None => print_breakpoints(vm), None => print_breakpoints(vm),
}, },
"set" => match parse_set_command(&mut iter) {
Some((reg, value)) => return VmInstruction::Set(reg, value),
None => println!("Invalid arguments provided"),
},
"c" | "continue" => return VmInstruction::Run, "c" | "continue" => return VmInstruction::Run,
"s" | "step" => return VmInstruction::Step, "s" | "step" => return VmInstruction::Step,
_ => {} _ => {}
@ -113,6 +123,12 @@ fn debug_input(vm: &Vm) -> VmInstruction {
} }
} }
fn parse_set_command<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Option<(usize, usize)> {
let reg: usize = iter.next().and_then(|reg| reg.parse().ok())?;
let value: usize = iter.next().and_then(|value| value.parse().ok())?;
Some((reg, value))
}
fn max_register(stmts: &[Stmt]) -> usize { fn max_register(stmts: &[Stmt]) -> usize {
stmts stmts
.iter() .iter()
@ -135,11 +151,12 @@ fn print_registers(vm: &Vm) {
} }
fn print_program(vm: &Vm) { fn print_program(vm: &Vm) {
use std::cmp::{max, min}; use std::cmp::min;
println!("Program:"); println!("Program:");
let lower = max(vm.pc, 5) - 5; let lower = if vm.pc > 5 { vm.pc - 5 } else { 0 };
let higher = min(vm.pc, vm.stmts.len() - 6) + 5; let len = vm.stmts.len();
let higher = if len < 5 { len } else { min(vm.pc + 5, len) };
for i in lower..higher { for i in lower..higher {
let stmt = vm.stmts[i]; let stmt = vm.stmts[i];
@ -169,6 +186,7 @@ fn print_help() {
"List of commands and their aliases: "List of commands and their aliases:
step (s) -- Steps the program forward by one step step (s) -- Steps the program forward by one step
set <register> <value> -- Sets a register to a value
break <line> (b) -- Set a breakpoint to a line, use again to toggle break <line> (b) -- Set a breakpoint to a line, use again to toggle
continue (c) -- Run the program until the next breakpoint continue (c) -- Run the program until the next breakpoint
register (r) -- Shows the contents of the registers register (r) -- Shows the contents of the registers

View file

@ -26,4 +26,5 @@ Type 'help' for help
} }
}; };
db::run(statements); db::run(statements);
println!("Execution finished.");
} }

23
test.m8
View file

@ -1,22 +1,5 @@
INC 1 IS_ZERO 1 5
INC 1
INC 1
INC 2
INC 2
IS_ZERO 1 22
DEC 1
IS_ZERO 2 13
INC 4
INC 5
DEC 2
JUMP 8
IS_ZERO 4 17
INC 3 INC 3
DEC 4 DEC 1
JUMP 13 JUMP 1
IS_ZERO 5 21
INC 2
DEC 5
JUMP 17
JUMP 6
STOP STOP