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 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)
19 new_hm[k] = EVAL(ast[k],env) for k,v of ast
25 #console.log "EVAL:", printer._pr_str ast
26 if !types._list_Q ast then return eval_ast ast, env
29 [a0, a1, a2, a3] = ast
32 return env.set(a1, EVAL(a2, env))
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))
40 eval_ast(ast[1..-2], env)
41 ast = ast[ast.length-1]
44 if cond == null or cond == false
45 if a3? then ast = a3 else return null
49 return types._function(EVAL, a2, env, a1)
51 [f, args...] = eval_ast ast, env
52 if types._function_Q(f)
54 env = f.__gen_env__(args)
60 PRINT = (exp) -> printer._pr_str exp, true
64 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
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*'), []
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) \")\")))))");
75 if process? && process.argv.length > 2
76 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
77 rep('(load-file "' + process.argv[2] + '")')
81 while (line = readline.readline("user> ")) != null
82 continue if line == ""
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