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