Commit | Line | Data |
---|---|---|
f5223195 JM |
1 | --- step8_macros -------------------------------- |
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 | ||
0f4ca9d1 | 9 | macro?(ast, env): return ... // true if macro call |
f5223195 JM |
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) | |
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 | '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! not (fn* (a) (if a false true)))") | |
e6d41de4 | 53 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))") |
a9cd6543 | 54 | 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 |
55 | |
56 | if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0 | |
57 | ||
58 | main loop: | |
59 | try: println(rep(readline("user> "))) | |
60 | catch e: println("Error: ", e) | |
61 | ||
62 | --- env module ---------------------------------- | |
63 | class Env (outer=null,binds=[],exprs=[]) | |
64 | data = hash_map() | |
65 | foreach b, i in binds: | |
66 | if binds[i] == '&: data[binds[i+1]] = exprs.drop(i); break | |
67 | else: data[binds[i]] = exprs[i] | |
68 | set(k,v): return data.set(k,v) | |
69 | find(k): return data.has(k) ? this : (if outer ? find(outer) : null) | |
70 | get(k): return data.find(k).get(k) OR raise "'" + k + "' not found" | |
fd888612 | 71 | |
f5223195 JM |
72 | --- core module --------------------------------- |
73 | ns = {'=: equal?, | |
f5223195 JM |
74 | |
75 | 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")), | |
76 | 'str: (a) -> a.map(|s| pr_str(e,false)).join("")), | |
77 | 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")), | |
78 | 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")), | |
79 | 'read-string: read_str, | |
80 | 'slurp read-file, | |
81 | ||
82 | '<: lt, | |
83 | '<=: lte, | |
84 | '>: gt, | |
85 | '>=: gte, | |
86 | '+: add, | |
87 | '-: sub, | |
88 | '*: mult, | |
89 | '/: div, | |
90 | ||
91 | 'list: list, | |
92 | 'list?: list?, | |
f5223195 JM |
93 | |
94 | 'cons: (a) -> concat([a[0]], a[1]), | |
95 | 'concat: (a) -> reduce(concat, [], a), | |
96 | 'nth: (a) -> a[0][a[1]] OR raise "nth: index out of range", | |
97 | 'first: (a) -> a[0][0] OR nil, | |
98 | 'rest: (a) -> a[0][1..] OR list(), | |
99 | 'empty?: empty?, | |
627bd6f7 DM |
100 | 'count: count, |
101 | ||
102 | 'atom: (a) -> new Atom(a[0]), | |
103 | 'atom?: (a) -> type(a[0]) == "atom", | |
104 | 'deref: (a) -> a[0].val, | |
105 | 'reset!: (a) -> a[0].val = a[1], | |
106 | 'swap!: swap!} |