DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / coffee / step9_try.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 is_macro_call = (ast, env) ->
23 return types._list_Q(ast) && types._symbol_Q(ast[0]) &&
24 env.find(ast[0]) && env.get(ast[0]).__ismacro__
25
26 macroexpand = (ast, env) ->
27 while is_macro_call(ast, env)
28 ast = env.get(ast[0])(ast[1..]...)
29 ast
30
31
32
33 eval_ast = (ast, env) ->
34 if types._symbol_Q(ast) then env.get ast
35 else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env))
36 else if types._vector_Q(ast)
37 types._vector(ast.map((a) -> EVAL(a, env))...)
38 else if types._hash_map_Q(ast)
39 new_hm = {}
40 new_hm[k] = EVAL(ast[k],env) for k,v of ast
41 new_hm
42 else ast
43
44 EVAL = (ast, env) ->
45 loop
46 #console.log "EVAL:", printer._pr_str ast
47 if !types._list_Q ast then return eval_ast ast, env
48
49 # apply list
50 ast = macroexpand ast, env
51 if !types._list_Q ast then return eval_ast ast, env
52 if ast.length == 0 then return ast
53
54 [a0, a1, a2, a3] = ast
55 switch a0.name
56 when "def!"
57 return env.set(a1, EVAL(a2, env))
58 when "let*"
59 let_env = new Env(env)
60 for k,i in a1 when i %% 2 == 0
61 let_env.set(a1[i], EVAL(a1[i+1], let_env))
62 ast = a2
63 env = let_env
64 when "quote"
65 return a1
66 when "quasiquote"
67 ast = quasiquote(a1)
68 when "defmacro!"
69 f = EVAL(a2, env)
70 f.__ismacro__ = true
71 return env.set(a1, f)
72 when "macroexpand"
73 return macroexpand(a1, env)
74 when "try*"
75 try return EVAL(a1, env)
76 catch exc
77 if a2 && a2[0].name == "catch*"
78 if exc.object? then exc = exc.object
79 else exc = exc.message
80 return EVAL a2[2], new Env(env, [a2[1]], [exc])
81 else
82 throw exc
83 when "do"
84 eval_ast(ast[1..-2], env)
85 ast = ast[ast.length-1]
86 when "if"
87 cond = EVAL(a1, env)
88 if cond == null or cond == false
89 if a3? then ast = a3 else return null
90 else
91 ast = a2
92 when "fn*"
93 return types._function(EVAL, a2, env, a1)
94 else
95 [f, args...] = eval_ast ast, env
96 if types._function_Q(f)
97 ast = f.__ast__
98 env = f.__gen_env__(args)
99 else
100 return f(args...)
101
102
103 # print
104 PRINT = (exp) -> printer._pr_str exp, true
105
106 # repl
107 repl_env = new Env()
108 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
109
110 # core.coffee: defined using CoffeeScript
111 repl_env.set types._symbol(k), v for k,v of core.ns
112 repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
113 repl_env.set types._symbol('*ARGV*'), []
114
115 # core.mal: defined using the language itself
116 rep("(def! not (fn* (a) (if a false true)))");
117 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
118 rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
119
120 if process? && process.argv.length > 2
121 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
122 rep('(load-file "' + process.argv[2] + '")')
123 process.exit 0
124
125 # repl loop
126 while (line = readline.readline("user> ")) != null
127 continue if line == ""
128 try
129 console.log rep line
130 catch exc
131 continue if exc instanceof reader.BlankException
132 if exc.stack? and exc.stack.length > 2000
133 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
134 else if exc.stack?
135 console.log exc.stack
136 else if exc.object?
137 console.log "Error:", printer._pr_str exc.object, true
138 else
139 console.log exc
140
141 # vim: ts=2:sw=2