diff options
Diffstat (limited to 'eval.lua')
-rw-r--r-- | eval.lua | 18 |
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" | |||
5 | local util = require "util" | 5 | local util = require "util" |
6 | local pp = require "pp" | 6 | local pp = require "pp" |
7 | local global = require "global" | 7 | local global = require "global" |
8 | local types = require("types") | 8 | local types = require "types" |
9 | table.unpack = table.unpack or unpack | ||
9 | 10 | ||
10 | if 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 | ||
12 | local function Env(inner, outer) | 14 | local function Env(inner, outer) |
13 | return setmetatable(inner, { __type = "environment", __index = outer, }) | 15 | return setmetatable(inner, { __type = "Environment", __index = outer, }) |
14 | end | 16 | end |
15 | 17 | ||
16 | local function Proc(params, body, env) | 18 | local 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 | ||
38 | function eval.eval (x, e) | 40 | function 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") |