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

View File

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