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
27 if ast.length == 0 then return ast
30 [a0, a1, a2, a3] = ast
33 return env.set(a1, EVAL(a2, env))
35 let_env = new Env(env)
36 for k,i in a1 when i %% 2 == 0
37 let_env.set(a1[i], EVAL(a1[i+1], let_env))
41 eval_ast(ast[1..-2], env)
42 ast = ast[ast.length-1]
45 if cond == null or cond == false
46 if a3? then ast = a3 else return null
50 return types._function(EVAL, a2, env, a1)
52 [f, args...] = eval_ast ast, env
53 if types._function_Q(f)
55 env = f.__gen_env__(args)
61 PRINT = (exp) -> printer._pr_str exp, true
65 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
67 # core.coffee: defined using CoffeeScript
68 repl_env.set types._symbol(k), v for k,v of core.ns
69 repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
70 repl_env.set types._symbol('*ARGV*'), []
72 # core.mal: defined using the language itself
73 rep("(def! not (fn* (a) (if a false true)))");
74 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
76 if process? && process.argv.length > 2
77 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
78 rep('(load-file "' + process.argv[2] + '")')
82 while (line = readline.readline("user> ")) != null
83 continue if line == ""
87 continue if exc instanceof reader.BlankException
88 if exc.stack? and exc.stack.length > 2000
89 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
90 else if exc.stack? then console.log exc.stack