12 function EvalAst(ast, env)
14 let varname = a:ast.val
15 return a:env.get(varname)
17 return ListNew(map(copy(a:ast.val), {_, e -> EVAL(e, a:env)}))
19 return VectorNew(map(copy(a:ast.val), {_, e -> EVAL(e, a:env)}))
22 for [k,v] in items(a:ast.val)
23 let keyobj = HashParseKey(k)
24 let newkey = EVAL(keyobj, a:env)
25 let newval = EVAL(v, a:env)
26 let keystring = HashMakeKey(newkey)
27 let ret[keystring] = newval
35 function EVAL(ast, env)
37 return EvalAst(a:ast, a:env)
43 let first = ListFirst(a:ast)
44 let first_symbol = SymbolQ(first) ? first.val : ""
45 if first_symbol == "def!"
48 let ret = a:env.set(a1.val, EVAL(a2, a:env))
50 elseif first_symbol == "let*"
53 let let_env = NewEnv(a:env)
54 let let_binds = a1.val
56 while i < len(let_binds)
57 call let_env.set(let_binds[i].val, EVAL(let_binds[i+1], let_env))
60 return EVAL(a2, let_env)
61 elseif first_symbol == "if"
62 let condvalue = EVAL(a:ast.val[1], a:env)
63 if FalseQ(condvalue) || NilQ(condvalue)
67 return EVAL(a:ast.val[3], a:env)
70 return EVAL(a:ast.val[2], a:env)
72 elseif first_symbol == "do"
73 let el = EvalAst(ListRest(a:ast), a:env)
75 elseif first_symbol == "fn*"
76 let fn = NewFn(ListNth(a:ast, 2), a:env, ListNth(a:ast, 1))
80 let el = EvalAst(a:ast, a:env)
81 let funcobj = ListFirst(el)
82 let args = ListRest(el)
83 if NativeFunctionQ(funcobj)
84 return NativeFuncInvoke(funcobj, args)
85 elseif FunctionQ(funcobj)
86 return FuncInvoke(funcobj, args)
88 throw "Not a function"
94 return PrStr(a:exp, 1)
97 function REP(str, env)
98 return PRINT(EVAL(READ(a:str), a:env))
101 let repl_env = NewEnv("")
103 for [k, Fn] in items(CoreNs)
104 call repl_env.set(k, Fn)
107 call REP("(def! not (fn* (a) (if a false true)))", repl_env)
110 let [eof, line] = Readline("user> ")
118 call PrintLn(REP(line, repl_env))
120 call PrintLn("Error: " . v:exception)