optional display execution time

This commit is contained in:
nora 2021-09-18 21:56:55 +02:00
parent 16fbd8555f
commit 15743535ac

View file

@ -42,19 +42,29 @@ impl Vm<'_> {
} }
} }
fn run(&mut self) -> VmState { fn run(&mut self, time_kind: VmRunKind) -> VmState {
let now = std::time::Instant::now();
loop { loop {
if let state @ (VmState::Break | VmState::Stop | VmState::OutOfBounds) = self.step() { if let state @ (VmState::Break | VmState::Stop | VmState::OutOfBounds) = self.step() {
if let VmRunKind::WithTime = time_kind {
println!("Vm ran for {}ms.", now.elapsed().as_millis());
}
return state; return state;
} }
} }
} }
} }
#[derive(Debug, Copy, Clone)]
enum VmRunKind {
WithTime,
WithoutTime,
}
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
enum VmInstruction { enum VmInstruction {
Step, Step,
Run, Run(VmRunKind),
Break(usize), Break(usize),
Set(usize, usize), Set(usize, usize),
} }
@ -72,7 +82,7 @@ pub fn run(code: Code) {
loop { loop {
match debug_input(&vm) { match debug_input(&vm) {
VmInstruction::Run => match vm.run() { VmInstruction::Run(time_kind) => match vm.run(time_kind) {
VmState::Stop => break, VmState::Stop => break,
VmState::OutOfBounds => { VmState::OutOfBounds => {
print_program(&vm); print_program(&vm);
@ -122,7 +132,12 @@ fn debug_input(vm: &Vm) -> VmInstruction {
Some((reg, value)) => return VmInstruction::Set(reg, value), Some((reg, value)) => return VmInstruction::Set(reg, value),
None => println!("Invalid arguments provided"), None => println!("Invalid arguments provided"),
}, },
"c" | "continue" => return VmInstruction::Run, "c" | "continue" => {
if let Some("time") = iter.next() {
return VmInstruction::Run(VmRunKind::WithTime);
}
return VmInstruction::Run(VmRunKind::WithoutTime);
}
"s" | "step" => return VmInstruction::Step, "s" | "step" => return VmInstruction::Step,
_ => {} _ => {}
} }
@ -198,7 +213,7 @@ fn print_help() {
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 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) (time) -- Run the program until the next breakpoint, add 'time' to display execution time
register (r) -- Shows the contents of the registers register (r) -- Shows the contents of the registers
program (p) -- Shows where the program currently is program (p) -- Shows where the program currently is
help (h, ?) -- Shows this help page help (h, ?) -- Shows this help page