Merge pull request #530 from mpritham/master
[jackhill/mal.git] / process / step8_macros.txt
1 --- step8_macros --------------------------------
2 import types, reader, printer, env, core
3
4 READ(str): return reader.read_str(str)
5
6 quasiquote(ast): return ... // quasiquote
7
8 macro?(ast, env): return ... // true if macro call
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)
21
22 ast = macroexpand(ast, env)
23 if not list?(ast): return eval_ast(ast, env)
24 if empty?(ast): return ast
25
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 'do: ast = eval_ast(ast[1..-1], env)[-1] // TCO
34 'if: EVAL(ast[1], env) ? ast = ast[2] : ast = ast[3] // TCO
35 'fn*: return new MalFunc(...)
36 _default_: f, args = eval_ast(ast, env)
37 if malfunc?(f): ast = f.fn; env = ... // TCO
38 else: return apply(f, args)
39
40 PRINT(exp): return printer.pr_str(exp)
41
42 repl_env = new Env()
43 rep(str): return PRINT(EVAL(READ(str),repl_env))
44
45 ;; core.EXT: defined using Racket
46 core.ns.map((k,v) -> (repl_env.set(k, v)))
47 repl_env.set('eval, (ast) -> EVAL(ast, repl-env))
48 repl_env.set('*ARGV*, cmdline_args[1..])
49
50 ;; core.mal: defined using the language itself
51 rep("(def! not (fn* (a) (if a false true)))")
52 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))")
53 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)))))))");
54
55 if cmdline_args: rep("(load-file \"" + args[0] + "\")"); exit 0
56
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
74 'pr-str: (a) -> a.map(|s| pr_str(e,true)).join(" ")),
75 'str: (a) -> a.map(|s| pr_str(e,false)).join("")),
76 'prn: (a) -> println(a.map(|s| pr_str(e,true)).join(" ")),
77 'println: (a) -> println(a.map(|s| pr_str(e,false)).join(" ")),
78 'read-string: read_str,
79 'slurp read-file,
80
81 '<: lt,
82 '<=: lte,
83 '>: gt,
84 '>=: gte,
85 '+: add,
86 '-: sub,
87 '*: mult,
88 '/: div,
89
90 'list: list,
91 'list?: list?,
92
93 'cons: (a) -> concat([a[0]], a[1]),
94 'concat: (a) -> reduce(concat, [], a),
95 'vec: (l) -> l converted to vector,
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?,
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!}