1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/nim/step5_tco.nim

118 lines
2.8 KiB
Nim
Raw Normal View History

2015-03-01 05:55:05 +03:00
import rdstdin, tables, sequtils, types, reader, printer, env, core
proc read(str: string): MalType = str.read_str
proc eval(ast: MalType, env: Env): MalType
2015-03-01 05:55:05 +03:00
proc eval_ast(ast: MalType, env: var Env): MalType =
case ast.kind
of Symbol:
2015-03-01 06:38:43 +03:00
result = env.get(ast.str)
2015-03-01 05:55:05 +03:00
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
of HashMap:
result = hash_map()
for k, v in ast.hash_map.pairs:
result.hash_map[k] = v.eval(env)
else:
result = ast
proc eval(ast: MalType, env: Env): MalType =
2015-03-04 16:12:19 +03:00
var ast = ast
var env = env
2015-03-04 16:12:19 +03:00
2015-03-01 05:55:05 +03:00
template defaultApply =
let el = ast.eval_ast(env)
let f = el.list[0]
case f.kind
of MalFun:
ast = f.malfun.ast
2015-03-29 04:07:10 +03:00
env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. ^1]))
2015-03-01 05:55:05 +03:00
else:
2015-03-29 04:07:10 +03:00
return f.fun(el.list[1 .. ^1])
2015-03-01 05:55:05 +03:00
while true:
2015-03-04 16:12:19 +03:00
if ast.kind != List: return ast.eval_ast(env)
if ast.list.len == 0: return ast
2015-03-01 05:55:05 +03:00
let a0 = ast.list[0]
case a0.kind
of Symbol:
2015-03-01 06:38:43 +03:00
case a0.str
2015-03-01 05:55:05 +03:00
of "def!":
let
a1 = ast.list[1]
a2 = ast.list[2]
2015-03-01 06:38:43 +03:00
return env.set(a1.str, a2.eval(env))
2015-03-01 05:55:05 +03:00
of "let*":
let
a1 = ast.list[1]
a2 = ast.list[2]
var let_env = initEnv(env)
2015-03-01 05:55:05 +03:00
case a1.kind
of List, Vector:
for i in countup(0, a1.list.high, 2):
2015-03-01 08:08:18 +03:00
let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
else: raise newException(ValueError, "Illegal kind in let*")
ast = a2
env = let_env
2015-03-01 05:55:05 +03:00
# Continue loop (TCO)
of "do":
let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env)
2015-03-04 16:12:19 +03:00
ast = ast.list[last]
2015-03-01 05:55:05 +03:00
# Continue loop (TCO)
of "if":
let
a1 = ast.list[1]
a2 = ast.list[2]
cond = a1.eval(env)
if cond.kind in {Nil, False}:
if ast.list.len > 3: ast = ast.list[3]
else: ast = nilObj
else: ast = a2
of "fn*":
let
a1 = ast.list[1]
a2 = ast.list[2]
var env2 = env
let fn = proc(a: varargs[MalType]): MalType =
var newEnv = initEnv(env2, a1, list(a))
a2.eval(newEnv)
2015-03-04 16:12:19 +03:00
return malfun(fn, a2, a1, env)
2015-03-01 05:55:05 +03:00
else:
defaultApply()
else:
defaultApply()
proc print(exp: MalType): string = exp.pr_str
var repl_env = initEnv()
for k, v in ns.items:
repl_env.set(k, v)
# core.nim: defined using nim
proc rep(str: string): string =
str.read.eval(repl_env).print
# core.mal: defined using mal itself
discard rep "(def! not (fn* (a) (if a false true)))"
while true:
try:
let line = readLineFromStdin("user> ")
echo line.rep
except:
echo getCurrentExceptionMsg()
echo getCurrentException().getStackTrace()