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