aboutsummaryrefslogtreecommitdiff
path: root/process/step2_eval.txt
blob: beb550036e084e796e1a3e5fe3ea6f14a521ed51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--- step2_eval ----------------------------------
import types, reader, printer

READ(str): return reader.read_str(str)

eval_ast(ast,env):
  switch type(ast):
    symbol:      return lookup(env, ast) OR raise "'" + ast + "' not found"
    list,vector: return ast.map((x) -> EVAL(x,env))
    hash:        return ast.map((k,v) -> list(k, EVAL(v,env)))
    _default_:   return ast

EVAL(ast,env):
    if not list?(ast): return eval_ast(ast, env)
    f, args = eval_ast(ast, env)
    return apply(f, args)

PRINT(exp): return printer.pr_str(exp)

repl_env = {'+: add_fn, ...}
rep(str): return PRINT(EVAL(READ(str),repl_env))

main loop:
  try:      println(rep(readline("user> ")))
  catch e:  println("Error: ", e)