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