Merge pull request #383 from asarhaddon/ada2tco-do
[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)))")
a9cd6543
JM
54rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
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)))))))");
56rep("(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))))))))");
f5223195
JM
57
58if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0
59
60main loop:
61 try: println(rep(readline("user> ")))
62 catch e: println("Error: ", e)
63
64--- env module ----------------------------------
65class Env (outer=null,binds=[],exprs=[])
66 data = hash_map()
67 foreach b, i in binds:
68 if binds[i] == '&: data[binds[i+1]] = exprs.drop(i); break
69 else: data[binds[i]] = exprs[i]
70 set(k,v): return data.set(k,v)
71 find(k): return data.has(k) ? this : (if outer ? find(outer) : null)
72 get(k): return data.find(k).get(k) OR raise "'" + k + "' not found"
fd888612 73
f5223195
JM
74--- core module ---------------------------------
75ns = {'=: equal?,
0f4ca9d1 76 'throw: throw,
f5223195 77
dbac60df
JM
78 'nil?: nil?,
79 'true?: true?,
80 'false?: false?,
81 'symbol: symbol,
82 'symbol?: symbol?,
83 'keyword: keyword,
84 'keyword?: keyword?,
85
f5223195
JM
86 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")),
87 'str: (a) -> a.map(|s| pr_str(e,false)).join("")),
88 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")),
89 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")),
90 'read-string: read_str,
91 'slurp read-file,
92
93 '<: lt,
94 '<=: lte,
95 '>: gt,
96 '>=: gte,
97 '+: add,
98 '-: sub,
99 '*: mult,
100 '/: div,
101
102 'list: list,
103 'list?: list?,
dbac60df
JM
104 'vector: vector,
105 'vector?: vector?,
106 'hash-map: hash_map,
107 'map?: hash_map?,
108 'assoc: assoc,
109 'dissoc: dissoc,
110 'get: get,
111 'contains?: contains?,
112 'keys: keys,
113 'vals: vals,
114
115 'sequential? sequential?,
f5223195
JM
116 'cons: (a) -> concat([a[0]], a[1]),
117 'concat: (a) -> reduce(concat, [], a),
118 'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range",
119 'first: (a) -> a[0][0] OR nil,
120 'rest: (a) -> a[0][1..] OR list(),
121 'empty?: empty?,
dbac60df
JM
122 'count: count,
123 'apply: apply,
627bd6f7
DM
124 'map: map,
125
126 'atom: (a) -> new Atom(a[0]),
127 'atom?: (a) -> type(a[0]) == "atom",
128 'deref: (a) -> a[0].val,
129 'reset!: (a) -> a[0].val = a[1],
130 'swap!: swap!}