1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
--- stepA_mal -------------------------------
import types, reader, printer, env, core
READ(str): return reader.read_str(str)
pair?(ast): return ... // true if non-empty sequence
quasiquote(ast): return ... // quasiquote
macro?(ast, env): return ... // true if macro call
macroexpand(ast, env): return ... // recursive macro expansion
eval_ast(ast,env):
switch type(ast):
symbol: return env.get(ast)
list,vector: return ast.map((x) -> EVAL(x,env))
hash: return ast.map((k,v) -> list(k, EVAL(v,env)))
_default_: return ast
EVAL(ast,env):
while true:
if not list?(ast): return eval_ast(ast, env)
ast = macroexpand(ast, env)
if not list?(ast): return ast
switch ast[0]:
'def!: return env.set(ast[1], EVAL(ast[2], env))
'let*: env = ...; ast = ast[2] // TCO
'quote: return ast[1]
'quasiquote: ast = quasiquote(ast[1]) // TCO
'defmacro!: return ... // like def!, but set macro property
'macroexpand: return macroexpand(ast[1], env)
'try*: return ... // try/catch native and malval exceptions
'do: ast = eval_ast(ast[1..-1], env)[-1] // TCO
'if: EVAL(ast[1], env) ? ast = ast[2] : ast = ast[3] // TCO
'fn*: return new MalFunc(...)
_default_: f, args = eval_ast(ast, env)
if malfunc?(f): ast = f.fn; env = ... // TCO
else: return apply(f, args)
PRINT(exp): return printer.pr_str(exp)
repl_env = new Env()
rep(str): return PRINT(EVAL(READ(str),repl_env))
;; core.EXT: defined using Racket
core.ns.map((k,v) -> (repl_env.set(k, v)))
repl_env.set('eval, (ast) -> EVAL(ast, repl-env))
repl_env.set('*ARGV*, cmdline_args[1..])
;; core.mal: defined using the language itself
rep("(def! *host-language* \"racket\")")
rep("(def! not (fn* (a) (if a false true)))")
rep("(def! load-file (fn* (f) ...))")
rep("(defmacro! cond (fn* (& xs) ...))")
rep("(defmacro! or (fn* (& xs) ...))")
if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0
rep("(println (str \"Mal [\" *host-language* \"]\"))")
main loop:
try: println(rep(readline("user> ")))
catch e: println("Error: ", e)
--- env module ----------------------------------
class Env (outer=null,binds=[],exprs=[])
data = hash_map()
foreach b, i in binds:
if binds[i] == '&: data[binds[i+1]] = exprs.drop(i); break
else: data[binds[i]] = exprs[i]
set(k,v): return data.set(k,v)
find(k): return data.has(k) ? this : (if outer ? find(outer) : null)
get(k): return data.find(k).get(k) OR raise "'" + k + "' not found"
--- core module ---------------------------------
ns = {'=: equal?,
'throw: throw,
'nil?: nil?,
'true?: true?,
'false?: false?,
'symbol: symbol,
'symbol?: symbol?,
'keyword: keyword,
'keyword?: keyword?,
'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")),
'str: (a) -> a.map(|s| pr_str(e,false)).join("")),
'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")),
'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")),
'read-string: read_str,
'readline: readline,
'slurp read-file,
'<: lt,
'<=: lte,
'>: gt,
'>=: gte,
'+: add,
'-: sub,
'*: mult,
'/: div,
'time-ms cur-epoch-millis,
'list: list,
'list?: list?,
'vector: vector,
'vector?: vector?,
'hash-map: hash_map,
'map?: hash_map?,
'assoc: assoc,
'dissoc: dissoc,
'get: get,
'contains?: contains?,
'keys: keys,
'vals: vals,
'sequential? sequential?,
'cons: (a) -> concat([a[0]], a[1]),
'concat: (a) -> reduce(concat, [], a),
'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range",
'first: (a) -> a[0][0] OR nil,
'rest: (a) -> a[0][1..] OR list(),
'empty?: empty?,
'count: count,
'apply: apply,
'map: map,
'conj: conj,
'meta: (a) -> a[0].meta,
'with-meta: (a) -> a[0].with_meta(a[1]),
'atom: (a) -> new Atom(a[0]),
'atom?: (a) -> type(a[0]) == "atom",
'deref: (a) -> a[0].val,
'reset!: (a) -> a[0].val = a[1],
'swap!: swap!}
|