DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / coffee / step7_quote.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 is_pair = (x) -> types._sequential_Q(x) && x.length > 0
13
14 quasiquote = (ast) ->
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..])]
19 else
20 [types._symbol('cons'), quasiquote(ast[0]), quasiquote(ast[1..])]
21
22
23
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)
30 new_hm = {}
31 new_hm[k] = EVAL(ast[k],env) for k,v of ast
32 new_hm
33 else ast
34
35 EVAL = (ast, env) ->
36 loop
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
40
41 # apply list
42 [a0, a1, a2, a3] = ast
43 switch a0.name
44 when "def!"
45 return env.set(a1, EVAL(a2, env))
46 when "let*"
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))
50 ast = a2
51 env = let_env
52 when "quote"
53 return a1
54 when "quasiquote"
55 ast = quasiquote(a1)
56 when "do"
57 eval_ast(ast[1..-2], env)
58 ast = ast[ast.length-1]
59 when "if"
60 cond = EVAL(a1, env)
61 if cond == null or cond == false
62 if a3? then ast = a3 else return null
63 else
64 ast = a2
65 when "fn*"
66 return types._function(EVAL, a2, env, a1)
67 else
68 [f, args...] = eval_ast ast, env
69 if types._function_Q(f)
70 ast = f.__ast__
71 env = f.__gen_env__(args)
72 else
73 return f(args...)
74
75
76 # print
77 PRINT = (exp) -> printer._pr_str exp, true
78
79 # repl
80 repl_env = new Env()
81 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
82
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*'), []
87
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)\")))))");
91
92 if process? && process.argv.length > 2
93 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
94 rep('(load-file "' + process.argv[2] + '")')
95 process.exit 0
96
97 # repl loop
98 while (line = readline.readline("user> ")) != null
99 continue if line == ""
100 try
101 console.log rep line
102 catch exc
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
107 else console.log exc
108
109 # vim: ts=2:sw=2