Merged master into ada branch + fix Makefile
[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)
899ff7fa 25
f5223195
JM
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)
899ff7fa
JM
38 if malfunc?(f): ast = f.fn; env = ... // TCO
39 else: return apply(f, args)
f5223195
JM
40
41PRINT(exp): return printer.pr_str(exp)
42
43repl_env = new Env()
44rep(str): return PRINT(EVAL(READ(str),repl_env))
45
46;; core.EXT: defined using Racket
47core.ns.map((k,v) -> (repl_env.set(k, v)))
48repl_env.set('eval, (ast) -> EVAL(ast, repl-env))
49repl_env.set('*ARGV*, cmdline_args[1..])
50
51;; core.mal: defined using the language itself
52rep("(def! *host-language* \"racket\")")
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)))))))");
e7ea3031
DM
56rep("(def! *gensym-counter* (atom 0))")
57rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
58rep("(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
59
60if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0
61
62rep("(println (str \"Mal [\" *host-language* \"]\"))")
63main loop:
64 try: println(rep(readline("user> ")))
65 catch e: println("Error: ", e)
66
67--- env module ----------------------------------
68class Env (outer=null,binds=[],exprs=[])
69 data = hash_map()
70 foreach b, i in binds:
71 if binds[i] == '&: data[binds[i+1]] = exprs.drop(i); break
72 else: data[binds[i]] = exprs[i]
73 set(k,v): return data.set(k,v)
74 find(k): return data.has(k) ? this : (if outer ? find(outer) : null)
75 get(k): return data.find(k).get(k) OR raise "'" + k + "' not found"
fd888612 76
f5223195
JM
77--- core module ---------------------------------
78ns = {'=: equal?,
79 'throw: throw,
80
81 'nil?: nil?,
82 'true?: true?,
83 'false?: false?,
84 'symbol: symbol,
85 'symbol?: symbol?,
86 'keyword: keyword,
87 'keyword?: keyword?,
88
89 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")),
90 'str: (a) -> a.map(|s| pr_str(e,false)).join("")),
91 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")),
92 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")),
93 'read-string: read_str,
94 'readline: readline,
95 'slurp read-file,
96
97 '<: lt,
98 '<=: lte,
99 '>: gt,
100 '>=: gte,
101 '+: add,
102 '-: sub,
103 '*: mult,
104 '/: div,
0f4ca9d1 105 'time-ms cur-epoch-millis,
f5223195
JM
106
107 'list: list,
108 'list?: list?,
109 'vector: vector,
110 'vector?: vector?,
111 'hash-map: hash_map,
112 'map?: hash_map?,
113 'assoc: assoc,
114 'dissoc: dissoc,
115 'get: get,
116 'contains?: contains?,
117 'keys: keys,
118 'vals: vals,
119
120 'sequential? sequential?,
121 'cons: (a) -> concat([a[0]], a[1]),
122 'concat: (a) -> reduce(concat, [], a),
123 'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range",
124 'first: (a) -> a[0][0] OR nil,
125 'rest: (a) -> a[0][1..] OR list(),
126 'empty?: empty?,
127 'count: count,
128 'apply: apply,
129 'map: map,
dbac60df 130
f5223195
JM
131 'conj: conj,
132
133 'meta: (a) -> a[0].meta,
134 'with-meta: (a) -> a[0].with_meta(a[1]),
135 'atom: (a) -> new Atom(a[0]),
136 'atom?: (a) -> type(a[0]) == "atom",
137 'deref: (a) -> a[0].val,
138 'reset!: (a) -> a[0].val = a[1],
139 'swap!: swap!}