From 4b8e5e065ef2503564e048998c96260298533e8c Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 2 Apr 2024 20:18:21 -0500 Subject: Handle dotted lambda-lists and symbols --- eval.lua | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/eval.lua b/eval.lua index c2945bf..53292d0 100644 --- a/eval.lua +++ b/eval.lua @@ -20,6 +20,28 @@ function m.environ (inner, outer) return setmetatable(inner, mt) end +local function call_proc (proc, r) + local function doargs (p, r, e) + if p == type.null and r == type.null then return e end + if type.isa(p, "symbol") then + e[p] = r + return e + end + if p[1] == nil then error("Too many arguments") end + if r[1] == nil then error("Too few arguments") end + e[p[1]] = r[1] + doargs(p[2], r[2], e) + end + + local e = doargs(proc.params, r, m.environ({}, proc.env)) + local b = proc.body + while b[2] ~= type.null do + m.eval(b[1], e) + b = b[2] + end + return m.eval(b[1], e) +end + function m.procedure (params, body, env) local t = { params = params, @@ -28,22 +50,7 @@ function m.procedure (params, body, env) } local mt = { __type = "procedure", - __call = - function (self, args) - local inner = {} - local p, a = self.params, args - while p[2] and a[2] do - inner[p[1]] = a[1] - p, a = p[2], a[2] - end - local b = self.body - local e = m.environ(inner, self.env) - while not b[2] == type.null do - m.eval(b[1], e) - b = b[2] - end - return m.eval(b[1], e) - end, + __call = call_proc, } return setmetatable(t, mt) end -- cgit 1.4.1-21-gabe81