From cf430149b8e7317eceb8ac0f36e7b5f7d32a8bdb Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 21 Mar 2024 22:42:13 -0500 Subject: Revert "Refactor eval" This reverts commit 1d5041ff7bf92458d6d07ac6a03f2747dce9b9b1. --- eval.lua | 75 +++++++++++++++++++++++----------------------------------------- 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/eval.lua b/eval.lua index 39a5bbe..9b1c315 100644 --- a/eval.lua +++ b/eval.lua @@ -1,12 +1,13 @@ --- lam.eval +local eval = {} local base = require "base" local type = require "type" local isNull, isList, isa, List, Cons = type.isNull, type.isList, type.isa, type.List, type.Cons local unpack = table.unpack or unpack -local function Env (inner, outer) +function eval.Env (inner, outer) local mt = { __type = "Environment", __index = outer, @@ -24,7 +25,7 @@ local function Env (inner, outer) return setmetatable(inner, mt) end -local function Proc (params, body, env) +function eval.Proc (params, body, env) local v = { params = params, body = body, @@ -41,51 +42,18 @@ local function Proc (params, body, env) p, a = p.cdr, a.cdr end local b = self.body - local e = Env(inner, self.env) + local e = eval.Env(inner, self.env) while not isNull(b.cdr) do - eval(b.car, e) + eval.eval(b.car, e) b = b.cdr end - return eval(b.car, e) + return eval.eval(b.car, e) end, } return setmetatable(v, mt) end -local specials = { - quote = - function (args, env) - return args.car - end, - define = - function (args, env) - rawset(env, args.car, eval(args.cdr.car, env)) - return nil - end, - lambda = - function (args, env) - return Proc(args.car, args.cdr, env) - end, - ["set!"] = - function (args, env) - env[args.car] = eval(args.cdr.car, env) - return nil - end, - ["if"] = - function (args, env) - local test, conseq, alt = - args.car, args.cdr.car, args.cdr.cdr.car - if eval(test) - then return eval(conseq) - else return eval(alt) - end - end, -} --- Aliases -specials.lam = specials.lambda -specials.def = specials.define - -local function eval (x, env) +function eval.eval (x, env) env = env or base.env if isa(x, "Symbol") then return env[x] @@ -93,14 +61,29 @@ local function eval (x, env) return x else local op, args = x.car, x.cdr - if specials[op] then - return specials[op](args, env) + if op == "quote" then + return args.car + elseif op == "define" or op == "def" then + rawset(env, args.car, eval.eval(args.cdr.car, env)) + return nil + elseif op == "set!" then + env[args.car] = eval.eval(args.cdr.car, env) + elseif op == "lambda" or op == "lam" then + return eval.Proc(args.car, args.cdr, env) + elseif op == "if" then + assert(not isNull(args.cdr), "Malformed 'if'") + local test, conseq, alt = + args.car, args.cdr.car, args.cdr.cdr.car + if eval.eval(test) + then return eval.eval(conseq) + else return eval.eval(alt) + end else -- procedure - local proc = eval(op, env) + local proc = eval.eval(op, env) local params = {} local a = args while a.cdr do - table.insert(params, eval(a.car, env)) + table.insert(params, eval.eval(a.car, env)) a = a.cdr end return proc(List(params)) @@ -109,8 +92,4 @@ local function eval (x, env) end --- -return { - Env = Env, - Proc = Proc, - eval = eval, -} +return eval -- cgit 1.4.1-21-gabe81