Commit | Line | Data |
---|---|---|
90f618cb | 1 | --- stepA_mal ------------------------------- |
f5223195 JM |
2 | import types, reader, printer, env, core |
3 | ||
4 | READ(str): return reader.read_str(str) | |
5 | ||
f5223195 JM |
6 | quasiquote(ast): return ... // quasiquote |
7 | ||
0f4ca9d1 | 8 | macro?(ast, env): return ... // true if macro call |
f5223195 JM |
9 | macroexpand(ast, env): return ... // recursive macro expansion |
10 | ||
11 | eval_ast(ast,env): | |
12 | switch type(ast): | |
13 | symbol: return env.get(ast) | |
14 | list,vector: return ast.map((x) -> EVAL(x,env)) | |
15 | hash: return ast.map((k,v) -> list(k, EVAL(v,env))) | |
16 | _default_: return ast | |
17 | ||
18 | EVAL(ast,env): | |
19 | while true: | |
20 | if not list?(ast): return eval_ast(ast, env) | |
899ff7fa JM |
21 | |
22 | ast = macroexpand(ast, env) | |
9b4cfe03 | 23 | if not list?(ast): return eval_ast(ast, env) |
1d100977 | 24 | if empty?(ast): return ast |
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 | |
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)))") | |
e6d41de4 | 54 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))") |
a9cd6543 | 55 | rep("(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)))))))"); |
f5223195 JM |
56 | |
57 | if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0 | |
58 | ||
59 | rep("(println (str \"Mal [\" *host-language* \"]\"))") | |
60 | main loop: | |
61 | try: println(rep(readline("user> "))) | |
62 | catch e: println("Error: ", e) | |
63 | ||
64 | --- env module ---------------------------------- | |
65 | class 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 --------------------------------- |
75 | ns = {'=: equal?, | |
76 | 'throw: throw, | |
77 | ||
78 | 'nil?: nil?, | |
79 | 'true?: true?, | |
80 | 'false?: false?, | |
396d869e | 81 | 'string?: string?, |
f5223195 JM |
82 | 'symbol: symbol, |
83 | 'symbol?: symbol?, | |
84 | 'keyword: keyword, | |
85 | 'keyword?: keyword?, | |
59436f1a DM |
86 | 'number?: number?, |
87 | 'fn?: fn?, | |
88 | 'macro?: macro?, | |
f5223195 JM |
89 | |
90 | 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")), | |
91 | 'str: (a) -> a.map(|s| pr_str(e,false)).join("")), | |
92 | 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")), | |
93 | 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")), | |
94 | 'read-string: read_str, | |
95 | 'readline: readline, | |
96 | 'slurp read-file, | |
97 | ||
98 | '<: lt, | |
99 | '<=: lte, | |
100 | '>: gt, | |
101 | '>=: gte, | |
102 | '+: add, | |
103 | '-: sub, | |
104 | '*: mult, | |
105 | '/: div, | |
0f4ca9d1 | 106 | 'time-ms cur-epoch-millis, |
f5223195 JM |
107 | |
108 | 'list: list, | |
109 | 'list?: list?, | |
110 | 'vector: vector, | |
111 | 'vector?: vector?, | |
112 | 'hash-map: hash_map, | |
113 | 'map?: hash_map?, | |
114 | 'assoc: assoc, | |
115 | 'dissoc: dissoc, | |
116 | 'get: get, | |
117 | 'contains?: contains?, | |
118 | 'keys: keys, | |
119 | 'vals: vals, | |
120 | ||
121 | 'sequential? sequential?, | |
122 | 'cons: (a) -> concat([a[0]], a[1]), | |
123 | 'concat: (a) -> reduce(concat, [], a), | |
fbfe6784 | 124 | 'vec: (l) -> l converted to vector, |
f5223195 JM |
125 | 'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range", |
126 | 'first: (a) -> a[0][0] OR nil, | |
127 | 'rest: (a) -> a[0][1..] OR list(), | |
128 | 'empty?: empty?, | |
129 | 'count: count, | |
130 | 'apply: apply, | |
131 | 'map: map, | |
dbac60df | 132 | |
f5223195 | 133 | 'conj: conj, |
396d869e | 134 | 'seq: seq, |
f5223195 JM |
135 | |
136 | 'meta: (a) -> a[0].meta, | |
137 | 'with-meta: (a) -> a[0].with_meta(a[1]), | |
138 | 'atom: (a) -> new Atom(a[0]), | |
139 | 'atom?: (a) -> type(a[0]) == "atom", | |
140 | 'deref: (a) -> a[0].val, | |
141 | 'reset!: (a) -> a[0].val = a[1], | |
142 | 'swap!: swap!} |