Nim: rename stepA_interop to stepA_mal
[jackhill/mal.git] / nim / step7_quote.nim
CommitLineData
dc7f0b6a 1import rdstdin, tables, sequtils, os, types, reader, printer, env, core
2
3proc read(str: string): MalType = str.read_str
4
5proc is_pair(x: MalType): bool =
6 x.kind in {List, Vector} and x.list.len > 0
7
8proc quasiquote(ast: MalType): MalType =
9 if not ast.is_pair:
10 return list(symbol "quote", ast)
11 elif ast.list[0] == symbol "unquote":
12 return ast.list[1]
13 elif ast.list[0].is_pair and ast.list[0].list[0] == symbol "splice-unquote":
14 return list(symbol "concat", ast.list[0].list[1],
15 quasiquote(list ast.list[1 .. -1]))
16 else:
17 return list(symbol "cons", quasiquote(ast.list[0]), quasiquote(list(ast.list[1 .. -1])))
18
19proc eval(ast: MalType, env: var Env): MalType
20
21proc eval_ast(ast: MalType, env: var Env): MalType =
22 case ast.kind
23 of Symbol:
24 result = env.get(ast.str)
25 of List:
26 result = list ast.list.mapIt(MalType, it.eval(env))
27 of Vector:
28 result = vector ast.list.mapIt(MalType, it.eval(env))
29 of HashMap:
30 result = hash_map()
31 for k, v in ast.hash_map.pairs:
32 result.hash_map[k] = v.eval(env)
33 else:
34 result = ast
35
36proc eval(ast: MalType, env: var Env): MalType =
37 template defaultApply =
38 let el = ast.eval_ast(env)
39 let f = el.list[0]
40 case f.kind
41 of MalFun:
42 ast = f.malfun.ast
f5cf5237 43 env = initEnv(env, f.malfun.params, list(el.list[1 .. -1]))
dc7f0b6a 44 else:
45 return f.fun(el.list[1 .. -1])
46
47 var ast = ast
48 while true:
49 if ast.kind != List:
50 return ast.eval_ast(env)
51
52 let a0 = ast.list[0]
53 case a0.kind
54 of Symbol:
55 case a0.str
56 of "def!":
57 let
58 a1 = ast.list[1]
59 a2 = ast.list[2]
60 return env.set(a1.str, a2.eval(env))
61
62 of "let*":
63 let
64 a1 = ast.list[1]
65 a2 = ast.list[2]
f5cf5237 66 var let_env = Env(env)
dc7f0b6a 67 case a1.kind
68 of List, Vector:
69 for i in countup(0, a1.list.high, 2):
f5cf5237 70 let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
71 else: raise newException(ValueError, "Illegal kind in let*")
72 ast = a2
73 env = let_env
dc7f0b6a 74 # Continue loop (TCO)
75
76 of "quote":
77 return ast.list[1]
78
79 of "quasiquote":
80 ast = ast.list[1].quasiquote
81 # Continue loop (TCO)
82
83 of "do":
84 let last = ast.list.high
85 let el = (list ast.list[1 .. <last]).eval_ast(env)
86 ast = ast.list[last].eval(env)
87 # Continue loop (TCO)
88
89 of "if":
90 let
91 a1 = ast.list[1]
92 a2 = ast.list[2]
93 cond = a1.eval(env)
94
95 if cond.kind in {Nil, False}:
96 if ast.list.len > 3: ast = ast.list[3]
97 else: ast = nilObj
98 else: ast = a2
99
100 of "fn*":
101 let
102 a1 = ast.list[1]
103 a2 = ast.list[2]
104 var env2 = env
105 let fn = proc(a: varargs[MalType]): MalType =
106 var newEnv = initEnv(env2, a1, list(a))
107 a2.eval(newEnv)
108 return malfun(fn, a2, a1, env2)
109
110 else:
111 defaultApply()
112
113 else:
114 defaultApply()
115
116proc print(exp: MalType): string = exp.pr_str
117
118var repl_env = initEnv()
119
120for k, v in ns.items:
121 repl_env.set(k, v)
122repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env)))
123var ps = commandLineParams()
124repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str)))
125
126
127# core.nim: defined using nim
128proc rep(str: string): string {.discardable.} =
129 str.read.eval(repl_env).print
130
131# core.mal: defined using mal itself
132rep "(def! not (fn* (a) (if a false true)))"
133rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
134
135if paramCount() >= 1:
136 rep "(load-file \"" & paramStr(1) & "\")"
137 quit()
138
139while true:
140 try:
141 let line = readLineFromStdin("user> ")
142 echo line.rep
143 except Blank: discard
144 except:
145 echo getCurrentExceptionMsg()
146 echo getCurrentException().getStackTrace()