From 4f4494a9b1057e1807488ed44b05d9fb9ce97b48 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 21 Mar 2024 17:55:21 -0500 Subject: Implment set! --- eval.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/eval.lua b/eval.lua index f58c36e..9b1c315 100644 --- a/eval.lua +++ b/eval.lua @@ -11,6 +11,16 @@ function eval.Env (inner, outer) local mt = { __type = "Environment", __index = outer, + __newindex = + function (self, key, value) + if rawget(self, key) then + -- Set the current environment's value + rawset(self, key, value) + else + -- Set the outer value + getmetatable(self).__index[key] = value + end + end, } return setmetatable(inner, mt) end @@ -54,8 +64,10 @@ function eval.eval (x, env) if op == "quote" then return args.car elseif op == "define" or op == "def" then - env[args.car] = eval.eval(args.cdr.car, env) + 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 -- cgit 1.4.1-21-gabe81