Commit | Line | Data |
---|---|---|
9d42904e JM |
1 | #!/usr/bin/env lua |
2 | ||
3 | local table = require('table') | |
4 | ||
5 | local readline = require('readline') | |
6 | local utils = require('utils') | |
7 | local types = require('types') | |
8 | local reader = require('reader') | |
9 | local printer = require('printer') | |
10 | local Env = require('env') | |
11 | local core = require('core') | |
12 | local List, Vector, HashMap = types.List, types.Vector, types.HashMap | |
13 | ||
14 | -- read | |
15 | function READ(str) | |
16 | return reader.read_str(str) | |
17 | end | |
18 | ||
19 | -- eval | |
20 | function is_pair(x) | |
21 | return types._sequential_Q(x) and #x > 0 | |
22 | end | |
23 | ||
24 | function quasiquote(ast) | |
25 | if not is_pair(ast) then | |
26 | return types.List:new({types.Symbol:new("quote"), ast}) | |
27 | elseif types._symbol_Q(ast[1]) and ast[1].val == 'unquote' then | |
28 | return ast[2] | |
29 | elseif is_pair(ast[1]) and | |
30 | types._symbol_Q(ast[1][1]) and | |
31 | ast[1][1].val == 'splice-unquote' then | |
32 | return types.List:new({types.Symbol:new("concat"), | |
33 | ast[1][2], | |
34 | quasiquote(ast:slice(2))}) | |
35 | else | |
36 | return types.List:new({types.Symbol:new("cons"), | |
37 | quasiquote(ast[1]), | |
38 | quasiquote(ast:slice(2))}) | |
39 | end | |
40 | end | |
41 | ||
42 | function eval_ast(ast, env) | |
43 | if types._symbol_Q(ast) then | |
44 | return env:get(ast) | |
45 | elseif types._list_Q(ast) then | |
46 | return List:new(utils.map(function(x) return EVAL(x,env) end,ast)) | |
47 | elseif types._vector_Q(ast) then | |
48 | return Vector:new(utils.map(function(x) return EVAL(x,env) end,ast)) | |
49 | elseif types._hash_map_Q(ast) then | |
50 | local new_hm = {} | |
51 | for k,v in pairs(ast) do | |
52 | new_hm[EVAL(k, env)] = EVAL(v, env) | |
53 | end | |
54 | return HashMap:new(new_hm) | |
55 | else | |
56 | return ast | |
57 | end | |
58 | end | |
59 | ||
60 | function EVAL(ast, env) | |
61 | while true do | |
62 | --print("EVAL: "..printer._pr_str(ast,true)) | |
63 | if not types._list_Q(ast) then return eval_ast(ast, env) end | |
64 | ||
65 | local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4] | |
3c59a767 | 66 | if not a0 then return ast end |
9d42904e JM |
67 | local a0sym = types._symbol_Q(a0) and a0.val or "" |
68 | if 'def!' == a0sym then | |
69 | return env:set(a1, EVAL(a2, env)) | |
70 | elseif 'let*' == a0sym then | |
71 | local let_env = Env:new(env) | |
72 | for i = 1,#a1,2 do | |
73 | let_env:set(a1[i], EVAL(a1[i+1], let_env)) | |
74 | end | |
75 | env = let_env | |
76 | ast = a2 -- TCO | |
77 | elseif 'quote' == a0sym then | |
78 | return a1 | |
79 | elseif 'quasiquote' == a0sym then | |
80 | ast = quasiquote(a1) -- TCO | |
81 | elseif 'do' == a0sym then | |
82 | local el = eval_ast(ast:slice(2,#ast-1), env) | |
83 | ast = ast[#ast] -- TCO | |
84 | elseif 'if' == a0sym then | |
85 | local cond = EVAL(a1, env) | |
86 | if cond == types.Nil or cond == false then | |
87 | if a3 then ast = a3 else return types.Nil end -- TCO | |
88 | else | |
89 | ast = a2 -- TCO | |
90 | end | |
91 | elseif 'fn*' == a0sym then | |
92 | return types.MalFunc:new(function(...) | |
93 | return EVAL(a2, Env:new(env, a1, arg)) | |
94 | end, a2, env, a1) | |
95 | else | |
96 | local args = eval_ast(ast, env) | |
97 | local f = table.remove(args, 1) | |
98 | if types._malfunc_Q(f) then | |
99 | ast = f.ast | |
100 | env = Env:new(f.env, f.params, args) -- TCO | |
101 | else | |
102 | return f(unpack(args)) | |
103 | end | |
104 | end | |
105 | end | |
106 | end | |
107 | ||
108 | ||
109 | function PRINT(exp) | |
110 | return printer._pr_str(exp, true) | |
111 | end | |
112 | ||
113 | -- repl | |
114 | local repl_env = Env:new() | |
115 | function rep(str) | |
116 | return PRINT(EVAL(READ(str),repl_env)) | |
117 | end | |
118 | ||
119 | -- core.lua: defined using Lua | |
120 | for k,v in pairs(core.ns) do | |
121 | repl_env:set(types.Symbol:new(k), v) | |
122 | end | |
123 | repl_env:set(types.Symbol:new('eval'), | |
124 | function(ast) return EVAL(ast, repl_env) end) | |
125 | repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2))) | |
126 | ||
127 | -- core.mal: defined using mal | |
128 | rep("(def! not (fn* (a) (if a false true)))") | |
129 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") | |
130 | ||
3e0b36dc JM |
131 | if #arg > 0 and arg[1] == "--raw" then |
132 | readline.raw = true | |
133 | table.remove(arg,1) | |
134 | end | |
135 | ||
9d42904e JM |
136 | if #arg > 0 then |
137 | rep("(load-file \""..arg[1].."\")") | |
138 | os.exit(0) | |
139 | end | |
140 | ||
141 | while true do | |
142 | line = readline.readline("user> ") | |
143 | if not line then break end | |
144 | xpcall(function() | |
145 | print(rep(line)) | |
146 | end, function(exc) | |
147 | if exc then | |
148 | if types._malexception_Q(exc) then | |
149 | exc = printer._pr_str(exc.val, true) | |
150 | end | |
151 | print("Error: " .. exc) | |
152 | print(debug.traceback()) | |
153 | end | |
154 | end) | |
155 | end |