From 0290bb3bf2f86a5dbd1dac6f7369ad12f1d2042c Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Mon, 23 Jun 2025 21:22:28 -0400 Subject: [PATCH] update (fix?) partial function application stack discipline --- sandbox.ld | 76 ++++++++++++++++++++++++++----------------------- src/compiler.rs | 2 +- src/main.rs | 2 +- src/vm.rs | 4 +-- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/sandbox.ld b/sandbox.ld index b99e3d0..9e65145 100644 --- a/sandbox.ld +++ b/sandbox.ld @@ -1,37 +1,41 @@ -fn apply_command { - "Takes a turtle state and a command and calculates a new state." - (state, command) -> match command with { - (:goto, (x, y)) -> assoc (state, :position, (x, y)) - (:home) -> do state > - assoc (_, :position, (0, 0)) > - assoc (_, :heading, 0) - (:clear) -> do state > - assoc (state, :position, (0, 0)) > - assoc (_, :heading, 0) - (:right, turns) -> update (state, :heading, add (_, turns)) - (:left, turns) -> update (state, :heading, sub (_, turns)) - (:forward, steps) -> { - let #{heading, position, ...} = state - let unit = heading/vector (heading) - let vect = mult (steps, unit) - update (state, :position, add (vect, _)) - } - (:back, steps) -> { - let #{heading, position, ...} = state - let unit = heading/vector (heading) - let vect = mult (steps, unit) - update (state, :position, sub (_, vect)) - } - (:penup) -> assoc (state, :pendown?, false) - (:pendown) -> assoc (state, :pendown?, true) - (:penwidth, pixels) -> assoc (state, :penwidth, pixels) - (:pencolor, color) -> assoc (state, :pencolor, color) - (:setheading, heading) -> assoc (state, :heading, heading) - (:loadstate, position, heading, visible?, pendown?, penwidth, pencolor) -> #{position, heading, visible?, pendown?, penwidth, pencolor} - (:show) -> assoc (state, :visible?, true) - (:hide) -> assoc (state, :visible?, false) - (:background, _) -> state - } -} +& fn apply_command { +& "Takes a turtle state and a command and calculates a new state." +& (state, command) -> match command with { +& (:goto, (x, y)) -> assoc (state, :position, (x, y)) +& (:home) -> do state > +& assoc (_, :position, (0, 0)) > +& assoc (_, :heading, 0) +& (:clear) -> do state > +& assoc (state, :position, (0, 0)) > +& assoc (_, :heading, 0) +& (:right, turns) -> update (state, :heading, add (_, turns)) +& (:left, turns) -> update (state, :heading, sub (_, turns)) +& (:forward, steps) -> { +& let #{heading, position, ...} = state +& let unit = heading/vector (heading) +& let vect = mult (steps, unit) +& update (state, :position, add (vect, _)) +& } +& (:back, steps) -> { +& let #{heading, position, ...} = state +& let unit = heading/vector (heading) +& let vect = mult (steps, unit) +& update (state, :position, sub (_, vect)) +& } +& (:penup) -> assoc (state, :pendown?, false) +& (:pendown) -> assoc (state, :pendown?, true) +& (:penwidth, pixels) -> assoc (state, :penwidth, pixels) +& (:pencolor, color) -> assoc (state, :pencolor, color) +& (:setheading, heading) -> assoc (state, :heading, heading) +& (:loadstate, position, heading, visible?, pendown?, penwidth, pencolor) -> #{position, heading, visible?, pendown?, penwidth, pencolor} +& (:show) -> assoc (state, :visible?, true) +& (:hide) -> assoc (state, :visible?, false) +& (:background, _) -> state +& } +& } -apply_command (turtle_init, (:penup)) +fn many_args (_, _, x, _) -> x + +let fewer_args = many_args (:foo, :bar, _, :baz) + +fewer_args(:quux) diff --git a/src/compiler.rs b/src/compiler.rs index 5134627..7cd0595 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -917,7 +917,7 @@ impl Compiler { self.resolve_binding(fn_name); self.emit_op(Op::Partial); self.emit_byte(arity); - self.stack_depth -= 1; + self.stack_depth -= args.len() - 1; } else { match get_builtin(fn_name, args.len()) { Some(code) => { diff --git a/src/main.rs b/src/main.rs index 3b26244..b9674ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::env; use std::fs; const DEBUG_SCRIPT_COMPILE: bool = false; -const DEBUG_SCRIPT_RUN: bool = false; +const DEBUG_SCRIPT_RUN: bool = true; const DEBUG_PRELUDE_COMPILE: bool = false; const DEBUG_PRELUDE_RUN: bool = false; diff --git a/src/vm.rs b/src/vm.rs index badc4a0..589f32e 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -1000,7 +1000,7 @@ impl Vm { let mut frame = CallFrame { function: the_fn, arity: args.len() as u8, - stack_base: self.stack.len() - arity as usize - 1, + stack_base: self.stack.len() - args.len(), ip: 0, }; @@ -1090,7 +1090,7 @@ impl Vm { let mut frame = CallFrame { function: the_fn, arity: args.len() as u8, - stack_base: self.stack.len() - arity as usize - 1, + stack_base: self.stack.len() - args.len(), ip: 0, };