1 import rdstdin, tables, sequtils, types, reader, printer
3 proc read(str: string): MalType = str.read_str
5 proc eval(ast: MalType, env: Table[string, MalType]): MalType
7 proc eval_ast(ast: MalType, env: Table[string, MalType]): MalType =
10 if not env.hasKey(ast.str):
11 raise newException(ValueError, "'" & ast.str & "' not found")
14 result = list ast.list.mapIt(MalType, it.eval(env))
16 result = vector ast.list.mapIt(MalType, it.eval(env))
19 for k, v in ast.hash_map.pairs:
20 result.hash_map[k] = v.eval(env)
24 proc eval(ast: MalType, env: Table[string, MalType]): MalType =
27 if ast.list.len == 0: return ast
28 let el = ast.eval_ast(env)
29 el.list[0].fun(el.list[1 .. ^1])
33 proc print(exp: MalType): string = exp.pr_str
35 template wrapNumberFun(op: expr): expr =
36 fun proc(xs: varargs[MalType]): MalType = number op(xs[0].number, xs[1].number)
38 let repl_env = toTable({
39 "+": wrapNumberFun `+`,
40 "-": wrapNumberFun `-`,
41 "*": wrapNumberFun `*`,
42 "/": wrapNumberFun `div`,
45 proc rep(str: string): string =
46 str.read.eval(repl_env).print
50 let line = readLineFromStdin("user> ")
53 echo getCurrentExceptionMsg()