DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / coffee / step3_env.coffee
1 readline = require "./node_readline.coffee"
2 types = require "./types.coffee"
3 reader = require "./reader.coffee"
4 printer = require "./printer.coffee"
5 Env = require("./env.coffee").Env
6
7 # read
8 READ = (str) -> reader.read_str str
9
10 # eval
11 eval_ast = (ast, env) ->
12 if types._symbol_Q(ast) then env.get ast
13 else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env))
14 else if types._vector_Q(ast)
15 types._vector(ast.map((a) -> EVAL(a, env))...)
16 else if types._hash_map_Q(ast)
17 new_hm = {}
18 new_hm[k] = EVAL(ast[k],env) for k,v of ast
19 new_hm
20 else ast
21
22 EVAL = (ast, env) ->
23 #console.log "EVAL:", printer._pr_str ast
24 if !types._list_Q ast then return eval_ast ast, env
25 if ast.length == 0 then return ast
26
27 # apply list
28 [a0, a1, a2, a3] = ast
29 switch a0.name
30 when "def!"
31 env.set(a1, EVAL(a2, env))
32 when "let*"
33 let_env = new Env(env)
34 for k,i in a1 when i %% 2 == 0
35 let_env.set(a1[i], EVAL(a1[i+1], let_env))
36 EVAL(a2, let_env)
37 else
38 [f, args...] = eval_ast ast, env
39 f(args...)
40
41
42 # print
43 PRINT = (exp) -> printer._pr_str exp, true
44
45 # repl
46 repl_env = new Env()
47 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
48
49 repl_env.set types._symbol("+"), (a,b) -> a+b
50 repl_env.set types._symbol("-"), (a,b) -> a-b
51 repl_env.set types._symbol("*"), (a,b) -> a*b
52 repl_env.set types._symbol("/"), (a,b) -> a/b
53
54 # repl loop
55 while (line = readline.readline("user> ")) != null
56 continue if line == ""
57 try
58 console.log rep line
59 catch exc
60 continue if exc instanceof reader.BlankException
61 if exc.stack? and exc.stack.length > 2000
62 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
63 else if exc.stack? then console.log exc.stack
64 else console.log exc
65
66 # vim: ts=2:sw=2