From e499d27dfcbf991a776e4a09c2415f81df9403db Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 29 Sep 2023 16:55:02 -0500 Subject: [PATCH] :construction: wip: improving debugging --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/state.rs | 17 +++++++++-------- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index f1083c0..09169d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,9 +60,56 @@ fn main () { let upvalues = State::upvalues_from_args ([exe_name].into_iter ().chain (args)); let mut vm = State::new (&chunk, &upvalues); - if std::env::var("LUA_DEBUG").is_ok() { + if std::env::var("LWVM_DEBUG").is_ok() { vm.debug_print = true; } - vm.execute_chunk (&breakpoints); + let max_iters = 2000; + let mut in_break = false; + let mut last_input = String::new (); + + for _ in 0..max_iters { + if in_break || breakpoints.iter ().any (|bp| vm.at_breakpoint (bp)) { + in_break = true; + dbg! (&vm.stack); + + let mut input = Default::default (); + std::io::stdin ().read_line (&mut input).unwrap (); + + let input = if input == "" { + &last_input + } + else { + last_input = input; + &last_input + }; + + match input.as_str ().trim_end () { + "c" => in_break = false, + "q" => return, + "s" => { + match vm.step () { + None => (), + Some (state::StepOutput::ChunkReturned (x)) => { + dbg! (x); + break; + }, + } + continue; + }, + x => { dbg! (x); }, + } + } + + match vm.step () { + None => (), + Some (state::StepOutput::ChunkReturned (x)) => { + dbg! (x); + break; + }, + } + } + + dbg! (vm); + panic! ("Hit max iterations before block returned"); } diff --git a/src/state.rs b/src/state.rs index 1b4d656..a125725 100644 --- a/src/state.rs +++ b/src/state.rs @@ -28,7 +28,7 @@ pub struct Chunk { } #[derive (Clone, Debug)] -struct StackFrame { +pub struct StackFrame { // i32 makes it a little easier to implement jumps // Starts at 0 right after OP_CALL @@ -51,7 +51,7 @@ pub struct State <'a> { registers: Vec , // Currently only used for native function calls top: usize, - stack: Vec , + pub stack: Vec , pub debug_print: bool, step_count: u32, @@ -98,6 +98,11 @@ impl <'a> State <'a> { } } + pub fn at_breakpoint (&self, bp: &Breakpoint) -> bool { + let frame = self.stack.last ().unwrap (); + frame.block_idx == bp.block_idx && frame.program_counter == bp.program_counter + } + pub fn upvalues_from_args > (args: I) -> Vec { let arg = args.map (|s| Value::from (s)).enumerate (); @@ -646,12 +651,8 @@ impl <'a> State <'a> { let max_iters = 2000; for _ in 0..max_iters { - let frame = self.stack.last ().unwrap (); - for bp in breakpoints { - if frame.block_idx == bp.block_idx && frame.program_counter == bp.program_counter - { - dbg! (&self); - } + if breakpoints.iter ().any (|bp| self.at_breakpoint (bp)) { + dbg! (&self); } match self.step () {