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")
9 READ = (str) -> reader.read_str str
12 is_pair = (x) -> types._sequential_Q(x) && x.length > 0
15 if !is_pair(ast) then [types._symbol('quote'), ast]
16 else if ast[0] != null && ast[0].name == 'unquote' then ast[1]
17 else if is_pair(ast[0]) && ast[0][0].name == 'splice-unquote'
18 [types._symbol('concat'), ast[0][1], quasiquote(ast[1..])]
20 [types._symbol('cons'), quasiquote(ast[0]), quasiquote(ast[1..])]
24 eval_ast = (ast, env) ->
25 if types._symbol_Q(ast) then env.get ast
26 else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env))
27 else if types._vector_Q(ast)
28 types._vector(ast.map((a) -> EVAL(a, env))...)
29 else if types._hash_map_Q(ast)
31 new_hm[k] = EVAL(ast[k],env) for k,v of ast
37 #console.log "EVAL:", printer._pr_str ast
38 if !types._list_Q ast then return eval_ast ast, env
39 if ast.length == 0 then return ast
42 [a0, a1, a2, a3] = ast
45 return env.set(a1, EVAL(a2, env))
47 let_env = new Env(env)
48 for k,i in a1 when i %% 2 == 0
49 let_env.set(a1[i], EVAL(a1[i+1], let_env))
57 eval_ast(ast[1..-2], env)
58 ast = ast[ast.length-1]
61 if cond == null or cond == false
62 if a3? then ast = a3 else return null
66 return types._function(EVAL, a2, env, a1)
68 [f, args...] = eval_ast ast, env
69 if types._function_Q(f)
71 env = f.__gen_env__(args)
77 PRINT = (exp) -> printer._pr_str exp, true
81 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
83 # core.coffee: defined using CoffeeScript
84 repl_env.set types._symbol(k), v for k,v of core.ns
85 repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
86 repl_env.set types._symbol('*ARGV*'), []
88 # core.mal: defined using the language itself
89 rep("(def! not (fn* (a) (if a false true)))");
90 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
92 if process? && process.argv.length > 2
93 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
94 rep('(load-file "' + process.argv[2] + '")')
98 while (line = readline.readline("user> ")) != null
99 continue if line == ""
103 continue if exc instanceof reader.BlankException
104 if exc.stack? and exc.stack.length > 2000
105 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
106 else if exc.stack? then console.log exc.stack