DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / coffee / stepA_mal.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 || exc.toString()
80 return EVAL a2[2], new Env(env, [a2[1]], [exc])
81 else
82 throw exc
83 when "js*"
84 res = eval(a1.toString())
85 return if typeof(res) == 'undefined' then null else res
86 when "."
87 el = eval_ast(ast[2..], env)
88 return eval(a1.toString())(el...)
89 when "do"
90 eval_ast(ast[1..-2], env)
91 ast = ast[ast.length-1]
92 when "if"
93 cond = EVAL(a1, env)
94 if cond == null or cond == false
95 if a3? then ast = a3 else return null
96 else
97 ast = a2
98 when "fn*"
99 return types._function(EVAL, a2, env, a1)
100 else
101 [f, args...] = eval_ast ast, env
102 if types._function_Q(f)
103 ast = f.__ast__
104 env = f.__gen_env__(args)
105 else
106 return f(args...)
107
108
109 # print
110 PRINT = (exp) -> printer._pr_str exp, true
111
112 # repl
113 repl_env = new Env()
114 rep = (str) -> PRINT(EVAL(READ(str), repl_env))
115
116 # core.coffee: defined using CoffeeScript
117 repl_env.set types._symbol(k), v for k,v of core.ns
118 repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
119 repl_env.set types._symbol('*ARGV*'), []
120
121 # core.mal: defined using the language itself
122 rep("(def! *host-language* \"CoffeeScript\")")
123 rep("(def! not (fn* (a) (if a false true)))");
124 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
125 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)))))))")
126
127 if process? && process.argv.length > 2
128 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
129 rep('(load-file "' + process.argv[2] + '")')
130 process.exit 0
131
132 # repl loop
133 rep("(println (str \"Mal [\" *host-language* \"]\"))")
134 while (line = readline.readline("user> ")) != null
135 continue if line == ""
136 try
137 console.log rep line
138 catch exc
139 continue if exc instanceof reader.BlankException
140 if exc.stack? and exc.stack.length > 2000
141 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
142 else if exc.stack?
143 console.log exc.stack
144 else if exc.object?
145 console.log "Error:", printer._pr_str exc.object, true
146 else
147 console.log exc
148
149 # vim: ts=2:sw=2