Merge pull request #306 from kanaka/add-predicates
[jackhill/mal.git] / process / stepA_mal.txt
CommitLineData
90f618cb 1--- stepA_mal -------------------------------
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)
34 'try*: return ... // try/catch native and malval exceptions
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! *host-language* \"racket\")")
54rep("(def! not (fn* (a) (if a false true)))")
a9cd6543
JM
55rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
56rep("(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)))))))");
e7ea3031
DM
57rep("(def! *gensym-counter* (atom 0))")
58rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
59rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")
f5223195
JM
60
61if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0
62
63rep("(println (str \"Mal [\" *host-language* \"]\"))")
64main loop:
65 try: println(rep(readline("user> ")))
66 catch e: println("Error: ", e)
67
68--- env module ----------------------------------
69class Env (outer=null,binds=[],exprs=[])
70 data = hash_map()
71 foreach b, i in binds:
72 if binds[i] == '&: data[binds[i+1]] = exprs.drop(i); break
73 else: data[binds[i]] = exprs[i]
74 set(k,v): return data.set(k,v)
75 find(k): return data.has(k) ? this : (if outer ? find(outer) : null)
76 get(k): return data.find(k).get(k) OR raise "'" + k + "' not found"
fd888612 77
f5223195
JM
78--- core module ---------------------------------
79ns = {'=: equal?,
80 'throw: throw,
81
82 'nil?: nil?,
83 'true?: true?,
84 'false?: false?,
396d869e 85 'string?: string?,
f5223195
JM
86 'symbol: symbol,
87 'symbol?: symbol?,
88 'keyword: keyword,
89 'keyword?: keyword?,
59436f1a
DM
90 'number?: number?,
91 'fn?: fn?,
92 'macro?: macro?,
f5223195
JM
93
94 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")),
95 'str: (a) -> a.map(|s| pr_str(e,false)).join("")),
96 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")),
97 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")),
98 'read-string: read_str,
99 'readline: readline,
100 'slurp read-file,
101
102 '<: lt,
103 '<=: lte,
104 '>: gt,
105 '>=: gte,
106 '+: add,
107 '-: sub,
108 '*: mult,
109 '/: div,
0f4ca9d1 110 'time-ms cur-epoch-millis,
f5223195
JM
111
112 'list: list,
113 'list?: list?,
114 'vector: vector,
115 'vector?: vector?,
116 'hash-map: hash_map,
117 'map?: hash_map?,
118 'assoc: assoc,
119 'dissoc: dissoc,
120 'get: get,
121 'contains?: contains?,
122 'keys: keys,
123 'vals: vals,
124
125 'sequential? sequential?,
126 'cons: (a) -> concat([a[0]], a[1]),
127 'concat: (a) -> reduce(concat, [], a),
128 'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range",
129 'first: (a) -> a[0][0] OR nil,
130 'rest: (a) -> a[0][1..] OR list(),
131 'empty?: empty?,
132 'count: count,
133 'apply: apply,
134 'map: map,
dbac60df 135
f5223195 136 'conj: conj,
396d869e 137 'seq: seq,
f5223195
JM
138
139 'meta: (a) -> a[0].meta,
140 'with-meta: (a) -> a[0].with_meta(a[1]),
141 'atom: (a) -> new Atom(a[0]),
142 'atom?: (a) -> type(a[0]) == "atom",
143 'deref: (a) -> a[0].val,
144 'reset!: (a) -> a[0].val = a[1],
145 'swap!: swap!}