Change quasiquote algorithm
[jackhill/mal.git] / impls / nim / step8_macros.nim
CommitLineData
f5cf5237 1import rdstdin, tables, sequtils, os, types, reader, printer, env, core
2
3proc read(str: string): MalType = str.read_str
4
fbfe6784
NB
5proc quasiquote(ast: MalType): MalType
6
7proc quasiquote_loop(xs: seq[MalType]): MalType =
8 result = list()
9 for i in countdown(xs.high, 0):
10 var elt = xs[i]
11 if elt.kind == List and 0 < elt.list.len and elt.list[0] == symbol "splice-unquote":
12 result = list(symbol "concat", elt.list[1], result)
13 else:
14 result = list(symbol "cons", quasiquote(elt), result)
f5cf5237 15
16proc quasiquote(ast: MalType): MalType =
fbfe6784
NB
17 case ast.kind
18 of List:
19 if ast.list.len == 2 and ast.list[0] == symbol "unquote":
20 result = ast.list[1]
21 else:
22 result = quasiquote_loop(ast.list)
23 of Vector:
24 result = list(symbol "vec", quasiquote_loop(ast.list))
25 of Symbol:
26 result = list(symbol "quote", ast)
27 of HashMap:
28 result = list(symbol "quote", ast)
f5cf5237 29 else:
fbfe6784 30 result = ast
f5cf5237 31
32proc is_macro_call(ast: MalType, env: Env): bool =
fc7f8a4b 33 ast.kind == List and ast.list.len > 0 and ast.list[0].kind == Symbol and
c1709fad 34 env.find(ast.list[0].str) != nil and env.get(ast.list[0].str).fun_is_macro
f5cf5237 35
36proc macroexpand(ast: MalType, env: Env): MalType =
37 result = ast
38 while result.is_macro_call(env):
39 let mac = env.get(result.list[0].str)
3f429bf4 40 result = mac.malfun.fn(result.list[1 .. ^1]).macroexpand(env)
f5cf5237 41
2800f318 42proc eval(ast: MalType, env: Env): MalType
f5cf5237 43
44proc eval_ast(ast: MalType, env: var Env): MalType =
45 case ast.kind
46 of Symbol:
47 result = env.get(ast.str)
48 of List:
41558f01 49 result = list ast.list.mapIt(it.eval(env))
f5cf5237 50 of Vector:
41558f01 51 result = vector ast.list.mapIt(it.eval(env))
f5cf5237 52 of HashMap:
53 result = hash_map()
54 for k, v in ast.hash_map.pairs:
55 result.hash_map[k] = v.eval(env)
56 else:
57 result = ast
58
2800f318 59proc eval(ast: MalType, env: Env): MalType =
60 var ast = ast
61 var env = env
62
f5cf5237 63 template defaultApply =
64 let el = ast.eval_ast(env)
65 let f = el.list[0]
66 case f.kind
67 of MalFun:
68 ast = f.malfun.ast
3f429bf4 69 env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. ^1]))
f5cf5237 70 else:
3f429bf4 71 return f.fun(el.list[1 .. ^1])
f5cf5237 72
f5cf5237 73 while true:
74 if ast.kind != List: return ast.eval_ast(env)
75
76 ast = ast.macroexpand(env)
6c94cd3e 77 if ast.kind != List: return ast.eval_ast(env)
f5cf5237 78 if ast.list.len == 0: return ast
79
80 let a0 = ast.list[0]
81 case a0.kind
82 of Symbol:
83 case a0.str
84 of "def!":
85 let
86 a1 = ast.list[1]
87 a2 = ast.list[2]
88 return env.set(a1.str, a2.eval(env))
89
90 of "let*":
91 let
92 a1 = ast.list[1]
93 a2 = ast.list[2]
fc7f8a4b 94 var let_env = initEnv(env)
f5cf5237 95 case a1.kind
96 of List, Vector:
97 for i in countup(0, a1.list.high, 2):
98 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
99 else: raise newException(ValueError, "Illegal kind in let*")
100 ast = a2
101 env = let_env
102 # Continue loop (TCO)
103
104 of "quote":
105 return ast.list[1]
106
fbfe6784
NB
107 of "quasiquoteexpand":
108 return ast.list[1].quasiquote
109
f5cf5237 110 of "quasiquote":
111 ast = ast.list[1].quasiquote
112 # Continue loop (TCO)
113
114 of "defmacro!":
115 var fun = ast.list[2].eval(env)
116 fun.malfun.is_macro = true
117 return env.set(ast.list[1].str, fun)
118
119 of "macroexpand":
120 return ast.list[1].macroexpand(env)
121
122 of "do":
123 let last = ast.list.high
41558f01 124 discard (list ast.list[1 ..< last]).eval_ast(env)
2800f318 125 ast = ast.list[last]
f5cf5237 126 # Continue loop (TCO)
127
128 of "if":
129 let
130 a1 = ast.list[1]
131 a2 = ast.list[2]
132 cond = a1.eval(env)
133
134 if cond.kind in {Nil, False}:
135 if ast.list.len > 3: ast = ast.list[3]
136 else: ast = nilObj
137 else: ast = a2
138
139 of "fn*":
140 let
141 a1 = ast.list[1]
142 a2 = ast.list[2]
143 var env2 = env
144 let fn = proc(a: varargs[MalType]): MalType =
145 var newEnv = initEnv(env2, a1, list(a))
146 a2.eval(newEnv)
2800f318 147 return malfun(fn, a2, a1, env)
f5cf5237 148
149 else:
150 defaultApply()
151
152 else:
153 defaultApply()
154
155proc print(exp: MalType): string = exp.pr_str
156
157var repl_env = initEnv()
158
159for k, v in ns.items:
160 repl_env.set(k, v)
161repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
162var ps = commandLineParams()
163repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
164
165
166# core.nim: defined using nim
167proc rep(str: string): string {.discardable.} =
168 str.read.eval(repl_env).print
169
170# core.mal: defined using mal itself
171rep "(def! not (fn* (a) (if a false true)))"
e6d41de4 172rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"
f5cf5237 173rep "(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)))))))"
f5cf5237 174
175if paramCount() >= 1:
176 rep "(load-file \"" & paramStr(1) & "\")"
177 quit()
178
179while true:
180 try:
181 let line = readLineFromStdin("user> ")
182 echo line.rep
183 except Blank: discard
184 except:
185 echo getCurrentExceptionMsg()
186 echo getCurrentException().getStackTrace()