Commit | Line | Data |
---|---|---|
f5223195 JM |
1 | --- step2_eval ---------------------------------- |
2 | import types, reader, printer | |
3 | ||
4 | READ(str): return reader.read_str(str) | |
5 | ||
6 | eval_ast(ast,env): | |
7 | switch type(ast): | |
8 | symbol: return lookup(env, ast) OR raise "'" + ast + "' not found" | |
9 | list,vector: return ast.map((x) -> EVAL(x,env)) | |
10 | hash: return ast.map((k,v) -> list(k, EVAL(v,env))) | |
11 | _default_: return ast | |
12 | ||
13 | EVAL(ast,env): | |
14 | if not list?(ast): return eval_ast(ast, env) | |
1af7aff1 | 15 | if empty?(ast): return ast |
f5223195 JM |
16 | f, args = eval_ast(ast, env) |
17 | return apply(f, args) | |
18 | ||
19 | PRINT(exp): return printer.pr_str(exp) | |
20 | ||
21 | repl_env = {'+: add_fn, ...} | |
22 | rep(str): return PRINT(EVAL(READ(str),repl_env)) | |
23 | ||
24 | main loop: | |
25 | try: println(rep(readline("user> "))) | |
26 | catch e: println("Error: ", e) |