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