12 function EvalAst(ast, env)
14 let varname = ObjValue(a:ast)
15 return a:env.get(varname)
18 for e in ObjValue(a:ast)
19 call add(ret, EVAL(e, a:env))
24 for e in ObjValue(a:ast)
25 call add(ret, EVAL(e, a:env))
30 for [k,v] in items(ObjValue(a:ast))
31 let keyobj = HashParseKey(k)
32 let newkey = EVAL(keyobj, a:env)
33 let newval = EVAL(v, a:env)
34 let keystring = HashMakeKey(newkey)
35 let ret[keystring] = newval
43 function EVAL(ast, env)
49 return EvalAst(ast, env)
55 let first = ListFirst(ast)
56 let first_symbol = SymbolQ(first) ? ObjValue(first) : ""
57 if first_symbol == "def!"
58 let a1 = ObjValue(ast)[1]
59 let a2 = ObjValue(ast)[2]
60 let ret = env.set(ObjValue(a1), EVAL(a2, env))
62 elseif first_symbol == "let*"
63 let a1 = ObjValue(ast)[1]
64 let a2 = ObjValue(ast)[2]
66 let let_binds = ObjValue(a1)
68 while i < len(let_binds)
69 call env.set(ObjValue(let_binds[i]), EVAL(let_binds[i+1], env))
74 elseif first_symbol == "if"
75 let condvalue = EVAL(ObjValue(ast)[1], env)
76 if FalseQ(condvalue) || NilQ(condvalue)
77 if len(ObjValue(ast)) < 4
80 let ast = ObjValue(ast)[3]
83 let ast = ObjValue(ast)[2]
86 elseif first_symbol == "do"
87 let astlist = ObjValue(ast)
88 call EvalAst(ListNew(astlist[1:-2]), env)
91 elseif first_symbol == "fn*"
92 let fn = NewFn(ListNth(ast, 2), env, ListNth(ast, 1))
96 let el = EvalAst(ast, env)
97 let funcobj = ListFirst(el)
98 let args = ListRest(el)
99 if NativeFunctionQ(funcobj)
100 return NativeFuncInvoke(funcobj, args)
101 elseif FunctionQ(funcobj)
102 let fn = ObjValue(funcobj)
104 let env = NewEnvWithBinds(fn.env, fn.params, args)
107 throw "Not a function"
114 return PrStr(a:exp, 1)
117 function REP(str, env)
118 return PRINT(EVAL(READ(a:str), a:env))
121 set maxfuncdepth=10000
122 let repl_env = NewEnv("")
124 for [k, v] in items(CoreNs)
125 call repl_env.set(k, v)
128 call REP("(def! not (fn* (a) (if a false true)))", repl_env)
131 let [eof, line] = Readline("user> ")
139 call PrintLn(REP(line, repl_env))
141 call PrintLn("Error: " . v:exception)