improve bytecode readability by reporting patterns

This commit is contained in:
Scott Richmond 2025-06-22 14:38:29 -04:00
parent 2f60de79a2
commit f58a5f14b5
2 changed files with 23 additions and 12 deletions

View File

@ -624,6 +624,10 @@ impl<'a> Compiler<'a> {
self.msg(format!("***{label} stack depth: {}", self.stack_depth)); self.msg(format!("***{label} stack depth: {}", self.stack_depth));
} }
fn report_ast(&mut self, label: String, node: &'static Spanned<Ast>) {
self.msg(format!("***{label}: {}", node.0.show()))
}
pub fn compile(&mut self) { pub fn compile(&mut self) {
use Ast::*; use Ast::*;
match self.ast { match self.ast {
@ -669,6 +673,7 @@ impl<'a> Compiler<'a> {
self.emit_op(Op::ResetMatch); self.emit_op(Op::ResetMatch);
self.visit(expr); self.visit(expr);
let expr_pos = self.stack_depth - 1; let expr_pos = self.stack_depth - 1;
self.report_ast("let binding: matching".to_string(), patt);
self.visit(patt); self.visit(patt);
self.emit_op(Op::PanicIfNoMatch); self.emit_op(Op::PanicIfNoMatch);
self.emit_op(Op::PushBinding); self.emit_op(Op::PushBinding);
@ -727,6 +732,7 @@ impl<'a> Compiler<'a> {
self.match_depth = 0; self.match_depth = 0;
self.emit_op(Op::ResetMatch); self.emit_op(Op::ResetMatch);
self.visit(expr); self.visit(expr);
self.report_ast("let binding: matching".to_string(), patt);
self.visit(patt); self.visit(patt);
self.emit_op(Op::PanicIfNoMatch); self.emit_op(Op::PanicIfNoMatch);
} }
@ -1208,6 +1214,7 @@ impl<'a> Compiler<'a> {
while let Some((MatchClause(pattern, guard, body), _)) = clauses.next() { while let Some((MatchClause(pattern, guard, body), _)) = clauses.next() {
self.tail_pos = false; self.tail_pos = false;
let mut no_match_jumps = vec![]; let mut no_match_jumps = vec![];
self.report_ast("match clause: ".to_string(), pattern);
self.scope_depth += 1; self.scope_depth += 1;
self.match_depth = 0; self.match_depth = 0;
self.visit(pattern); self.visit(pattern);
@ -1284,6 +1291,7 @@ impl<'a> Compiler<'a> {
let MatchClause(pattern, guard, clause_body) = &clause.0 else { let MatchClause(pattern, guard, clause_body) = &clause.0 else {
unreachable!() unreachable!()
}; };
let full_pattern = pattern;
let TuplePattern(pattern) = &pattern.0 else { let TuplePattern(pattern) = &pattern.0 else {
unreachable!() unreachable!()
}; };
@ -1319,6 +1327,7 @@ impl<'a> Compiler<'a> {
std::mem::swap(&mut upvalues, &mut compiler.upvalues); std::mem::swap(&mut upvalues, &mut compiler.upvalues);
let mut tup_jump_idxes = vec![]; let mut tup_jump_idxes = vec![];
compiler.report_ast("function clause matching: ".to_string(), full_pattern);
for member in pattern { for member in pattern {
compiler.match_depth -= 1; compiler.match_depth -= 1;
compiler.emit_op(Op::MatchDepth); compiler.emit_op(Op::MatchDepth);
@ -1505,6 +1514,7 @@ impl<'a> Compiler<'a> {
}; };
self.match_depth = arity; self.match_depth = arity;
let mut jnm_idxes = vec![]; let mut jnm_idxes = vec![];
self.report_ast("loop clause matching: ".to_string(), pattern);
for member in members { for member in members {
self.match_depth -= 1; self.match_depth -= 1;
self.emit_op(Op::MatchDepth); self.emit_op(Op::MatchDepth);

View File

@ -134,20 +134,20 @@ pub fn run(src: &'static str) {
println!("\n\n") println!("\n\n")
} }
if DEBUG_SCRIPT_RUN { // if DEBUG_SCRIPT_RUN {
println!("=== vm run: test ==="); // println!("=== vm run: test ===");
} // }
let vm_chunk = compiler.chunk; // let vm_chunk = compiler.chunk;
let mut vm = Vm::new(vm_chunk, DEBUG_SCRIPT_RUN); // let mut vm = Vm::new(vm_chunk, DEBUG_SCRIPT_RUN);
let result = vm.run(); // let result = vm.run();
let output = match result { // let output = match result {
Ok(val) => val.show(), // Ok(val) => val.show(),
Err(panic) => format!("Ludus panicked! {panic}"), // Err(panic) => format!("Ludus panicked! {panic}"),
}; // };
vm.print_stack(); // vm.print_stack();
println!("{output}"); // println!("{output}");
} }
pub fn ld_fmt(src: &'static str) -> Result<String, String> { pub fn ld_fmt(src: &'static str) -> Result<String, String> {
@ -181,4 +181,5 @@ pub fn main() {
Ok(src) => println!("{}", src), Ok(src) => println!("{}", src),
Err(msg) => println!("Could not format source with errors:\n{}", msg), Err(msg) => println!("Could not format source with errors:\n{}", msg),
} }
run(src);
} }