Implement step 8
[jackhill/mal.git] / coffee / stepA_mal.coffee
CommitLineData
891c3f3b
JM
1readline = require "./node_readline.coffee"
2types = require "./types.coffee"
3reader = require "./reader.coffee"
4printer = require "./printer.coffee"
5Env = require("./env.coffee").Env
6core = require("./core.coffee")
7
8# read
9READ = (str) -> reader.read_str str
10
11# eval
12is_pair = (x) -> types._sequential_Q(x) && x.length > 0
13
14quasiquote = (ast) ->
15 if !is_pair(ast) then [types._symbol('quote'), ast]
c963be7a 16 else if ast[0] != null && ast[0].name == 'unquote' then ast[1]
891c3f3b
JM
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
22is_macro_call = (ast, env) ->
23 return types._list_Q(ast) && types._symbol_Q(ast[0]) &&
b8ee29b2 24 env.find(ast[0]) && env.get(ast[0]).__ismacro__
891c3f3b
JM
25
26macroexpand = (ast, env) ->
27 while is_macro_call(ast, env)
b8ee29b2 28 ast = env.get(ast[0])(ast[1..]...)
891c3f3b
JM
29 ast
30
31
32
33eval_ast = (ast, env) ->
b8ee29b2 34 if types._symbol_Q(ast) then env.get ast
891c3f3b
JM
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
44EVAL = (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
bfa3dd35
DM
50 ast = macroexpand ast, env
51 if !types._list_Q ast then return eval_ast ast, env
891c3f3b
JM
52
53 [a0, a1, a2, a3] = ast
54 switch a0.name
55 when "def!"
b8ee29b2 56 return env.set(a1, EVAL(a2, env))
891c3f3b
JM
57 when "let*"
58 let_env = new Env(env)
59 for k,i in a1 when i %% 2 == 0
b8ee29b2 60 let_env.set(a1[i], EVAL(a1[i+1], let_env))
891c3f3b
JM
61 ast = a2
62 env = let_env
63 when "quote"
64 return a1
65 when "quasiquote"
66 ast = quasiquote(a1)
67 when "defmacro!"
68 f = EVAL(a2, env)
69 f.__ismacro__ = true
b8ee29b2 70 return env.set(a1, f)
891c3f3b
JM
71 when "macroexpand"
72 return macroexpand(a1, env)
73 when "try*"
74 try return EVAL(a1, env)
75 catch exc
76 if a2 && a2[0].name == "catch*"
77 if exc instanceof Error then exc = exc.message
78 return EVAL a2[2], new Env(env, [a2[1]], [exc])
79 else
80 throw exc
81 when "js*"
82 res = eval(a1.toString())
83 return if typeof(res) == 'undefined' then null else res
84 when "."
85 el = eval_ast(ast[2..], env)
86 return eval(a1.toString())(el...)
87 when "do"
88 eval_ast(ast[1..-2], env)
89 ast = ast[ast.length-1]
90 when "if"
91 cond = EVAL(a1, env)
92 if cond == null or cond == false
93 if a3? then ast = a3 else return null
94 else
95 ast = a2
96 when "fn*"
97 return types._function(EVAL, a2, env, a1)
98 else
99 [f, args...] = eval_ast ast, env
100 if types._function_Q(f)
101 ast = f.__ast__
102 env = f.__gen_env__(args)
103 else
104 return f(args...)
105
106
107# print
108PRINT = (exp) -> printer._pr_str exp, true
109
110# repl
111repl_env = new Env()
112rep = (str) -> PRINT(EVAL(READ(str), repl_env))
113
114# core.coffee: defined using CoffeeScript
b8ee29b2
JM
115repl_env.set types._symbol(k), v for k,v of core.ns
116repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
117repl_env.set types._symbol('*ARGV*'), []
891c3f3b
JM
118
119# core.mal: defined using the language itself
120rep("(def! *host-language* \"CoffeeScript\")")
121rep("(def! not (fn* (a) (if a false true)))");
122rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
123rep("(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)))))))")
166da203
DM
124rep("(def! *gensym-counter* (atom 0))")
125rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
126rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")
891c3f3b
JM
127
128if process? && process.argv.length > 2
b8ee29b2 129 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
891c3f3b
JM
130 rep('(load-file "' + process.argv[2] + '")')
131 process.exit 0
132
133# repl loop
134rep("(println (str \"Mal [\" *host-language* \"]\"))")
135while (line = readline.readline("user> ")) != null
136 continue if line == ""
137 try
138 console.log rep line
139 catch exc
140 continue if exc instanceof reader.BlankException
4ed89670
JM
141 if exc.stack? and exc.stack.length > 2000
142 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
143 else if exc.stack? console.log exc.stack
144 else console.log exc
891c3f3b
JM
145
146# vim: ts=2:sw=2