about summary refs log tree commit diff stats
path: root/eval.lua
diff options
context:
space:
mode:
authorCase Duckworth2024-03-04 21:01:27 -0600
committerCase Duckworth2024-03-04 21:01:27 -0600
commitbbaff0e0c204c2fab216f6501dc8c11b4425b4bc (patch)
treeb2a06edc7b1c8e6f86839bff8c16e06297080674 /eval.lua
parentAdd copying (diff)
downloadlam-bbaff0e0c204c2fab216f6501dc8c11b4425b4bc.tar.gz
lam-bbaff0e0c204c2fab216f6501dc8c11b4425b4bc.zip
Ugghhhh totally not working first-try
Diffstat (limited to 'eval.lua')
-rw-r--r--eval.lua18
1 files changed, 10 insertions, 8 deletions
diff --git a/eval.lua b/eval.lua index 185268f..806148d 100644 --- a/eval.lua +++ b/eval.lua
@@ -5,12 +5,14 @@ local read = require "read"
5local util = require "util" 5local util = require "util"
6local pp = require "pp" 6local pp = require "pp"
7local global = require "global" 7local global = require "global"
8local types = require("types") 8local types = require "types"
9table.unpack = table.unpack or unpack
9 10
10if not table.unpack then table.unpack = unpack end 11--- Environments and Parameters
12-- these aren't in types.lua to avoid a circular dependency
11 13
12local function Env(inner, outer) 14local function Env(inner, outer)
13 return setmetatable(inner, { __type = "environment", __index = outer, }) 15 return setmetatable(inner, { __type = "Environment", __index = outer, })
14end 16end
15 17
16local function Proc(params, body, env) 18local function Proc(params, body, env)
@@ -20,7 +22,7 @@ local function Proc(params, body, env)
20 env = env, 22 env = env,
21 } 23 }
22 local mt = { 24 local mt = {
23 __type = "procedure", 25 __type = "Procedure",
24 __call = 26 __call =
25 function (self, ...) 27 function (self, ...)
26 local inner = {} 28 local inner = {}
@@ -37,23 +39,23 @@ end
37 39
38function eval.eval (x, e) 40function eval.eval (x, e)
39 e = e or global 41 e = e or global
40 if types.lamtype(x) == "symbol" then 42 if types.lamtype(x) == "Symbol" then
41 return e[x] 43 return e[x]
42 elseif types.luatype(x) ~= "table" then 44 elseif types.luatype(x) ~= "table" then
43 return x 45 return x
44 else 46 else
45 local op = util.car(x) 47 local op = util.car(x)
46 local args = util.cdr(x) 48 local args = util.cdr(x)
47 if op == "quote" then 49 if op == types.Symbol("quote") then
48 return args[1] 50 return args[1]
49 elseif op == "define" then 51 elseif op == types.Symbol("define") then
50 local sym, exp = table.unpack(args) 52 local sym, exp = table.unpack(args)
51 e[sym] = eval(exp, e) 53 e[sym] = eval(exp, e)
52 --[[ 54 --[[
53 elseif op == "set!" then 55 elseif op == "set!" then
54 local sym, exp = table.unpack(args) 56 local sym, exp = table.unpack(args)
55 e[sym] = eval(exp, e) --]] 57 e[sym] = eval(exp, e) --]]
56 elseif op == "lambda" then 58 elseif op == types.Symbol("lambda") then
57 local params = util.car(args) 59 local params = util.car(args)
58 local body = util.cdr(args) 60 local body = util.cdr(args)
59 table.insert(body, 1, "begin") 61 table.insert(body, 1, "begin")