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 if not a0
then return ast
end
88 local a0sym
= types
._symbol_Q(a0
) and a0
.val
or ""
89 if 'def!' == a0sym
then
90 return env
:set(a1
, EVAL(a2
, env
))
91 elseif 'let*' == a0sym
then
92 local let_env
= Env
:new(env
)
94 let_env
:set(a1
[i
], EVAL(a1
[i
+1], let_env
))
98 elseif 'quote' == a0sym
then
100 elseif 'quasiquote' == a0sym
then
101 ast
= quasiquote(a1
) -- TCO
102 elseif 'defmacro!' == a0sym
then
103 local mac
= EVAL(a2
, env
)
105 return env
:set(a1
, mac
)
106 elseif 'macroexpand' == a0sym
then
107 return macroexpand(a1
, env
)
108 elseif 'do' == a0sym
then
109 local el
= eval_ast(ast
:slice(2,#ast
-1), env
)
110 ast
= ast
[#ast
] -- TCO
111 elseif 'if' == a0sym
then
112 local cond
= EVAL(a1
, env
)
113 if cond
== types
.Nil
or cond
== false then
114 if a3
then ast
= a3
else return types
.Nil
end -- TCO
118 elseif 'fn*' == a0sym
then
119 return types
.MalFunc
:new(function(...)
120 return EVAL(a2
, Env
:new(env
, a1
, arg
))
123 local args
= eval_ast(ast
, env
)
124 local f
= table.remove(args
, 1)
125 if types
._malfunc_Q(f
) then
127 env
= Env
:new(f
.env
, f
.params
, args
) -- TCO
129 return f(unpack(args
))
137 return printer
._pr_str(exp, true)
141 local repl_env
= Env
:new()
143 return PRINT(EVAL(READ(str
),repl_env
))
146 -- core.lua: defined using Lua
147 for k
,v
in pairs(core
.ns
) do
148 repl_env
:set(types
.Symbol
:new(k
), v
)
150 repl_env
:set(types
.Symbol
:new('eval'),
151 function(ast
) return EVAL(ast
, repl_env
) end)
152 repl_env
:set(types
.Symbol
:new('*ARGV*'), types
.List
:new(types
.slice(arg
,2)))
154 -- core.mal: defined using mal
155 rep("(def! not (fn* (a) (if a false true)))")
156 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
157 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)))))))")
158 rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
160 if #arg
> 0 and arg
[1] == "--raw" then
166 rep("(load-file \""..arg
[1].."\")")
171 line
= readline
.readline("user> ")
172 if not line
then break end
177 if types
._malexception_Q(exc
) then
178 exc
= printer
._pr_str(exc
.val
, true)
180 print("Error: " .. exc
)
181 print(debug
.traceback())