be11008287b343b20492adf455ae71db9b07f7c9
3 local table = require('table')
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
16 return reader
.read_str(str
)
21 return types
._sequential_Q(x
) and #x
> 0
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
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"),
34 quasiquote(ast
:slice(2))})
36 return types
.List
:new({types
.Symbol
:new("cons"),
38 quasiquote(ast
:slice(2))})
42 function is_macro_call(ast
, env
)
43 if types
._list_Q(ast
) and
44 types
._symbol_Q(ast
[1]) and
46 local f
= env
:get(ast
[1])
47 return types
._malfunc_Q(f
) and f
.ismacro
51 function macroexpand(ast
, env
)
52 while is_macro_call(ast
, env
) do
53 local mac
= env
:get(ast
[1])
54 ast
= mac
.fn(unpack(ast
:slice(2)))
59 function eval_ast(ast
, env
)
60 if types
._symbol_Q(ast
) then
62 elseif types
._list_Q(ast
) then
63 return List
:new(utils
.map(function(x
) return EVAL(x
,env
) end,ast
))
64 elseif types
._vector_Q(ast
) then
65 return Vector
:new(utils
.map(function(x
) return EVAL(x
,env
) end,ast
))
66 elseif types
._hash_map_Q(ast
) then
68 for k
,v
in pairs(ast
) do
69 new_hm
[EVAL(k
, env
)] = EVAL(v
, env
)
71 return HashMap
:new(new_hm
)
77 function EVAL(ast
, env
)
79 --print("EVAL: "..printer._pr_str(ast,true))
80 if not types
._list_Q(ast
) then return eval_ast(ast
, env
) end
83 ast
= macroexpand(ast
, env
)
84 if not types
._list_Q(ast
) then return eval_ast(ast
, env
) end
86 local a0
,a1
,a2
,a3
= ast
[1], ast
[2],ast
[3],ast
[4]
87 local a0sym
= types
._symbol_Q(a0
) and a0
.val
or ""
88 if 'def!' == a0sym
then
89 return env
:set(a1
, EVAL(a2
, env
))
90 elseif 'let*' == a0sym
then
91 local let_env
= Env
:new(env
)
93 let_env
:set(a1
[i
], EVAL(a1
[i
+1], let_env
))
97 elseif 'quote' == a0sym
then
99 elseif 'quasiquote' == a0sym
then
100 ast
= quasiquote(a1
) -- TCO
101 elseif 'defmacro!' == a0sym
then
102 local mac
= EVAL(a2
, env
)
104 return env
:set(a1
, mac
)
105 elseif 'macroexpand' == a0sym
then
106 return macroexpand(a1
, env
)
107 elseif 'try*' == a0sym
then
108 local exc
, result
= nil, nil
110 result
= EVAL(a1
, env
)
115 if types
._malexception_Q(exc
) then
118 if a2
and a2
[1].val
== 'catch*' then
119 result
= EVAL(a2
[3], Env
:new(env
, {a2
[2]}, {exc
}))
125 elseif 'do' == a0sym
then
126 local el
= eval_ast(ast
:slice(2,#ast
-1), env
)
127 ast
= ast
[#ast
] -- TCO
128 elseif 'if' == a0sym
then
129 local cond
= EVAL(a1
, env
)
130 if cond
== types
.Nil
or cond
== false then
131 if a3
then ast
= a3
else return types
.Nil
end -- TCO
135 elseif 'fn*' == a0sym
then
136 return types
.MalFunc
:new(function(...)
137 return EVAL(a2
, Env
:new(env
, a1
, arg
))
140 local args
= eval_ast(ast
, env
)
141 local f
= table.remove(args
, 1)
142 if types
._malfunc_Q(f
) then
144 env
= Env
:new(f
.env
, f
.params
, args
) -- TCO
146 return f(unpack(args
))
154 return printer
._pr_str(exp, true)
158 local repl_env
= Env
:new()
160 return PRINT(EVAL(READ(str
),repl_env
))
163 -- core.lua: defined using Lua
164 for k
,v
in pairs(core
.ns
) do
165 repl_env
:set(types
.Symbol
:new(k
), v
)
167 repl_env
:set(types
.Symbol
:new('eval'),
168 function(ast
) return EVAL(ast
, repl_env
) end)
169 repl_env
:set(types
.Symbol
:new('*ARGV*'), types
.List
:new(types
.slice(arg
,2)))
171 -- core.mal: defined using mal
172 rep("(def! *host-language* \"lua\")")
173 rep("(def! not (fn* (a) (if a false true)))")
174 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
175 rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
176 rep("(def! *gensym-counter* (atom 0))")
177 rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
178 rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")
180 function print_exception(exc
)
182 if types
._malexception_Q(exc
) then
183 exc
= printer
._pr_str(exc
.val
, true)
185 print("Error: " .. exc
)
186 print(debug
.traceback())
190 if #arg
> 0 and arg
[1] == "--raw" then
196 xpcall(function() rep("(load-file \""..arg
[1].."\")") end,
201 rep("(println (str \"Mal [\" *host-language* \"]\"))")
203 line
= readline
.readline("user> ")
204 if not line
then break end
205 xpcall(function() print(rep(line
)) end,