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