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