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 eval_ast(ast, env) | |
21 | if types._symbol_Q(ast) then | |
22 | return env:get(ast) | |
23 | elseif types._list_Q(ast) then | |
24 | return List:new(utils.map(function(x) return EVAL(x,env) end,ast)) | |
25 | elseif types._vector_Q(ast) then | |
26 | return Vector:new(utils.map(function(x) return EVAL(x,env) end,ast)) | |
27 | elseif types._hash_map_Q(ast) then | |
28 | local new_hm = {} | |
29 | for k,v in pairs(ast) do | |
30 | new_hm[EVAL(k, env)] = EVAL(v, env) | |
31 | end | |
32 | return HashMap:new(new_hm) | |
33 | else | |
34 | return ast | |
35 | end | |
36 | end | |
37 | ||
38 | function EVAL(ast, env) | |
39 | while true do | |
40 | --print("EVAL: "..printer._pr_str(ast,true)) | |
41 | if not types._list_Q(ast) then return eval_ast(ast, env) end | |
42 | ||
43 | local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4] | |
44 | local a0sym = types._symbol_Q(a0) and a0.val or "" | |
45 | if 'def!' == a0sym then | |
46 | return env:set(a1, EVAL(a2, env)) | |
47 | elseif 'let*' == a0sym then | |
48 | local let_env = Env:new(env) | |
49 | for i = 1,#a1,2 do | |
50 | let_env:set(a1[i], EVAL(a1[i+1], let_env)) | |
51 | end | |
52 | env = let_env | |
53 | ast = a2 -- TCO | |
54 | elseif 'do' == a0sym then | |
55 | local el = eval_ast(ast:slice(2,#ast-1), env) | |
56 | ast = ast[#ast] -- TCO | |
57 | elseif 'if' == a0sym then | |
58 | local cond = EVAL(a1, env) | |
59 | if cond == types.Nil or cond == false then | |
60 | if a3 then ast = a3 else return types.Nil end -- TCO | |
61 | else | |
62 | ast = a2 -- TCO | |
63 | end | |
64 | elseif 'fn*' == a0sym then | |
65 | return types.MalFunc:new(function(...) | |
66 | return EVAL(a2, Env:new(env, a1, arg)) | |
67 | end, a2, env, a1) | |
68 | else | |
69 | local args = eval_ast(ast, env) | |
70 | local f = table.remove(args, 1) | |
71 | if types._malfunc_Q(f) then | |
72 | ast = f.ast | |
73 | env = Env:new(f.env, f.params, args) -- TCO | |
74 | else | |
75 | return f(unpack(args)) | |
76 | end | |
77 | end | |
78 | end | |
79 | end | |
80 | ||
81 | ||
82 | function PRINT(exp) | |
83 | return printer._pr_str(exp, true) | |
84 | end | |
85 | ||
86 | -- repl | |
87 | local repl_env = Env:new() | |
88 | function rep(str) | |
89 | return PRINT(EVAL(READ(str),repl_env)) | |
90 | end | |
91 | ||
92 | -- core.lua: defined using Lua | |
93 | for k,v in pairs(core.ns) do | |
94 | repl_env:set(types.Symbol:new(k), v) | |
95 | end | |
96 | repl_env:set(types.Symbol:new('eval'), | |
97 | function(ast) return EVAL(ast, repl_env) end) | |
98 | repl_env:set(types.Symbol:new('*ARGV*'), types.List:new(types.slice(arg,2))) | |
99 | ||
100 | -- core.mal: defined using mal | |
101 | rep("(def! not (fn* (a) (if a false true)))") | |
102 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") | |
103 | ||
3e0b36dc JM |
104 | if #arg > 0 and arg[1] == "--raw" then |
105 | readline.raw = true | |
106 | table.remove(arg,1) | |
107 | end | |
108 | ||
9d42904e JM |
109 | if #arg > 0 then |
110 | rep("(load-file \""..arg[1].."\")") | |
111 | os.exit(0) | |
112 | end | |
113 | ||
114 | while true do | |
115 | line = readline.readline("user> ") | |
116 | if not line then break end | |
117 | xpcall(function() | |
118 | print(rep(line)) | |
119 | end, function(exc) | |
120 | if exc then | |
121 | if types._malexception_Q(exc) then | |
122 | exc = printer._pr_str(exc.val, true) | |
123 | end | |
124 | print("Error: " .. exc) | |
125 | print(debug.traceback()) | |
126 | end | |
127 | end) | |
128 | end |