🚧 wip: improving debugging

main
_ 2023-09-29 16:55:02 -05:00
parent a460b5a932
commit e499d27dfc
2 changed files with 58 additions and 10 deletions

View File

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

View File

@ -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 <Value>,
// Currently only used for native function calls
top: usize,
stack: Vec <StackFrame>,
pub stack: Vec <StackFrame>,
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 <I: Iterator <Item = String>> (args: I) -> Vec <Value>
{
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 () {