From f58a5f14b5e6a91f108f6cc36c47e7245c1002d2 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 22 Jun 2025 14:38:29 -0400 Subject: [PATCH] improve bytecode readability by reporting patterns --- src/compiler.rs | 10 ++++++++++ src/main.rs | 25 +++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index b77d62a..493c140 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -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) { + 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); diff --git a/src/main.rs b/src/main.rs index 60bfbf4..58addc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { @@ -181,4 +181,5 @@ pub fn main() { Ok(src) => println!("{}", src), Err(msg) => println!("Could not format source with errors:\n{}", msg), } + run(src); }