Implement step 9
[jackhill/mal.git] / coffee / step6_file.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 core = require("./core.coffee")
7
8 # read
9 READ = (str) -> reader.read_str str
10
11 # eval
12 eval_ast = (ast, env) ->
13 if types._symbol_Q(ast) then env.get ast
14 else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env))
15 else if types._vector_Q(ast)
16 types._vector(ast.map((a) -> EVAL(a, env))...)
17 else if types._hash_map_Q(ast)
18 new_hm = {}
19 new_hm[k] = EVAL(ast[k],env) for k,v of ast
20 new_hm
21 else ast
22
23 EVAL = (ast, env) ->
24 loop
25 #console.log "EVAL:", printer._pr_str ast
26 if !types._list_Q ast then return eval_ast ast, env
27
28 # apply list
29 [a0, a1, a2, a3] = ast
30 switch a0.name
31 when "def!"
32 return env.set(a1, EVAL(a2, env))
33 when "let*"
34 let_env = new Env(env)
35 for k,i in a1 when i %% 2 == 0
36 let_env.set(a1[i], EVAL(a1[i+1], let_env))
37 ast = a2
38 env = let_env
39 when "do"
40 eval_ast(ast[1..-2], env)
41 ast = ast[ast.length-1]
42 when "if"
43 cond = EVAL(a1, env)
44 if cond == null or cond == false
45 if a3? then ast = a3 else return null
46 else
47 ast = a2
48 when "fn*"
49 return types._function(EVAL, a2, env, a1)
50 else
51 [f, args...] = eval_ast ast, env
52 if types._function_Q(f)
53 ast = f.__ast__
54 env = f.__gen_env__(args)
55 else
56 return f(args...)
57
58
59 # print
60 PRINT = (exp) -> printer._pr_str exp, true
61
62 # repl
63 repl_env = new Env()
64 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
65
66 # core.coffee: defined using CoffeeScript
67 repl_env.set types._symbol(k), v for k,v of core.ns
68 repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
69 repl_env.set types._symbol('*ARGV*'), []
70
71 # core.mal: defined using the language itself
72 rep("(def! not (fn* (a) (if a false true)))");
73 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
74
75 if process? && process.argv.length > 2
76 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
77 rep('(load-file "' + process.argv[2] + '")')
78 process.exit 0
79
80 # repl loop
81 while (line = readline.readline("user> ")) != null
82 continue if line == ""
83 try
84 console.log rep line
85 catch exc
86 continue if exc instanceof reader.BlankException
87 if exc.stack? and exc.stack.length > 2000
88 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
89 else if exc.stack? console.log exc.stack
90 else console.log exc
91
92 # vim: ts=2:sw=2