From b8b720b877c017864020419deb3c6361cd90f7ce Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 5 Jun 2025 12:15:49 -0400 Subject: [PATCH] add do forms --- src/compiler.rs | 25 +++++++++++++++++-------- src/main.rs | 32 ++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 20f2ec1..81ef617 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -851,8 +851,8 @@ impl<'a> Compiler<'a> { self.emit_op(Op::PushBinding); self.stack_depth += 1; - let biter = self.bindings.iter().rev(); - for binding in biter { + // let biter = self.bindings.iter().rev(); + for binding in self.bindings.iter() { if binding.name == *fn_name { self.emit_byte(binding.stack_pos); break; @@ -1261,12 +1261,21 @@ impl<'a> Compiler<'a> { } } } - Arguments(..) - | Placeholder - | Do(..) - | Splat(..) - | InterpolatedPattern(..) - | Splattern(..) => todo!(), + Do(terms) => { + let mut terms = terms.iter(); + let first = terms.next().unwrap(); + // put the first value on the stack + self.visit(first); + for term in terms { + self.visit(term); + self.emit_op(Op::Call); + self.emit_byte(1); + self.stack_depth -= 1; + } + } + Arguments(..) | Placeholder | Splat(..) | InterpolatedPattern(..) | Splattern(..) => { + todo!() + } And | Or => unreachable!(), } } diff --git a/src/main.rs b/src/main.rs index dfcc9bd..6366dc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,28 +57,32 @@ pub fn run(src: &'static str) { println!("\n\n") } - // if DEBUG_RUN { - // println!("=== vm run: test ==="); - // } + if DEBUG_RUN { + println!("=== vm run: test ==="); + } - // let vm_chunk = compiler.chunk; + let vm_chunk = compiler.chunk; - // let mut vm = Vm::new(vm_chunk); - // 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); + 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 main() { env::set_var("RUST_BACKTRACE", "1"); let src = " -fn foo () -> :foo +fn increase (x) -> add (x, 1) -foo () +fn double (x) -> mult (x, 2) + +fn halve (x) -> div (x, 2) + +do 1 > increase > double > increase > halve ";