bash, c, go, lua, racket: fix macro result evaluation
[jackhill/mal.git] / crystal / step8_macros.cr
CommitLineData
9bbb8ccc 1#! /usr/bin/env crystal run
2
3require "./readline"
4require "./reader"
5require "./printer"
6require "./types"
7require "./env"
8require "./core"
9require "./error"
10
11# Note:
12# Employed downcase names because Crystal prohibits uppercase names for methods
13
97d0deb1 14module Mal
15 extend self
16
17 def func_of(env, binds, body)
18 -> (args : Array(Mal::Type)) {
19 new_env = Mal::Env.new(env, binds, args)
20 eval(body, new_env)
21 } as Mal::Func
22 end
9bbb8ccc 23
97d0deb1 24 def eval_ast(ast, env)
25 return ast.map{|n| eval(n, env) as Mal::Type} if ast.is_a? Mal::List
9bbb8ccc 26
97d0deb1 27 val = ast.unwrap
9bbb8ccc 28
97d0deb1 29 Mal::Type.new case val
30 when Mal::Symbol
31 if e = env.get(val.str)
32 e
33 else
34 eval_error "'#{val.str}' not found"
35 end
36 when Mal::List
37 val.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
38 when Mal::Vector
39 val.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
40 when Array(Mal::Type)
41 val.map{|n| eval(n, env)}
42 when Mal::HashMap
43 val.each{|k, v| val[k] = eval(v, env)}
44 val
9bbb8ccc 45 else
97d0deb1 46 val
9bbb8ccc 47 end
9bbb8ccc 48 end
9bbb8ccc 49
97d0deb1 50 def read(str)
51 read_str str
9bbb8ccc 52 end
53
97d0deb1 54 macro pair?(list)
55 {{list}}.is_a?(Array) && !{{list}}.empty?
9bbb8ccc 56 end
9bbb8ccc 57
97d0deb1 58 def quasiquote(ast)
59 list = ast.unwrap
9bbb8ccc 60
97d0deb1 61 unless pair?(list)
62 return Mal::Type.new(
63 Mal::List.new << gen_type(Mal::Symbol, "quote") << ast
64 )
65 end
9bbb8ccc 66
97d0deb1 67 head = list.first.unwrap
9bbb8ccc 68
97d0deb1 69 case
70 # ("unquote" ...)
71 when head.is_a?(Mal::Symbol) && head.str == "unquote"
72 list[1]
73 # (("splice-unquote" ...) ...)
74 when pair?(head) && (arg0 = head.first.unwrap).is_a?(Mal::Symbol) && arg0.str == "splice-unquote"
75 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new){|e,l| l << e}
76 Mal::Type.new(
77 Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
78 )
9bbb8ccc 79 else
97d0deb1 80 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new){|e,l| l << e}
81 Mal::Type.new(
82 Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
83 )
9bbb8ccc 84 end
85 end
86
97d0deb1 87 def macro_call?(ast, env)
88 list = ast.unwrap
89 return false unless list.is_a? Mal::List
9bbb8ccc 90
97d0deb1 91 sym = list.first.unwrap
92 return false unless sym.is_a? Mal::Symbol
9bbb8ccc 93
97d0deb1 94 func = env.find(sym.str).try(&.data[sym.str])
95 return false unless func && func.macro?
9bbb8ccc 96
97d0deb1 97 true
98 end
9bbb8ccc 99
97d0deb1 100 def macroexpand(ast, env)
101 while macro_call?(ast, env)
9bbb8ccc 102
97d0deb1 103 # Already checked in macro_call?
104 list = ast.unwrap as Mal::List
105 func_sym = list[0].unwrap as Mal::Symbol
106 func = env.get(func_sym.str).unwrap
9bbb8ccc 107
97d0deb1 108 case func
109 when Mal::Func
110 ast = func.call(list[1..-1])
111 when Mal::Closure
112 ast = func.fn.call(list[1..-1])
113 else
114 eval_error "macro '#{func_sym.str}' must be function: #{ast}"
115 end
116 end
9bbb8ccc 117
97d0deb1 118 ast
119 end
9bbb8ccc 120
97d0deb1 121 macro invoke_list(l, env)
122 f = eval({{l}}.first, {{env}}).unwrap
123 args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}})
124 case f
125 when Mal::Closure
126 ast = f.ast
127 {{env}} = Mal::Env.new(f.env, f.params, args)
128 next # TCO
129 when Mal::Func
130 return f.call args
131 else
132 eval_error "expected function as the first argument: #{f}"
133 end
134 end
9bbb8ccc 135
97d0deb1 136 def eval(ast, env)
137 # 'next' in 'do...end' has a bug in crystal 0.7.1
138 # https://github.com/manastech/crystal/issues/659
139 while true
140 return eval_ast(ast, env) unless ast.unwrap.is_a? Mal::List
141
142 ast = macroexpand(ast, env)
143
144 list = ast.unwrap
145
146 return ast unless list.is_a? Mal::List
147 return ast if list.empty?
148
149 head = list.first.unwrap
150
151 return invoke_list(list, env) unless head.is_a? Mal::Symbol
152
153 return Mal::Type.new case head.str
154 when "def!"
155 eval_error "wrong number of argument for 'def!'" unless list.size == 3
156 a1 = list[1].unwrap
157 eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
158 env.set(a1.str, eval(list[2], env))
159 when "let*"
160 eval_error "wrong number of argument for 'def!'" unless list.size == 3
161
162 bindings = list[1].unwrap
163 eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
164 eval_error "size of binding list must be even" unless bindings.size.even?
165
166 new_env = Mal::Env.new env
167 bindings.each_slice(2) do |binding|
168 key, value = binding
169 name = key.unwrap
170 eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Mal::Symbol
171 new_env.set(name.str, eval(value, new_env))
172 end
173
174 ast, env = list[2], new_env
175 next # TCO
176 when "do"
177 if list.empty?
178 ast = Mal::Type.new nil
179 next
180 end
181
182 eval_ast(list[1..-2].each_with_object(Mal::List.new){|i,l| l << i}, env)
183 ast = list.last
184 next # TCO
185 when "if"
186 ast = unless eval(list[1], env).unwrap
187 list.size >= 4 ? list[3] : Mal::Type.new(nil)
188 else
189 list[2]
190 end
191 next # TCO
192 when "fn*"
193 params = list[1].unwrap
194 unless params.is_a? Array
195 eval_error "'fn*' parameters must be list or vector: #{params}"
196 end
197 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
198 when "quote"
199 list[1]
200 when "quasiquote"
201 ast = quasiquote list[1]
202 next # TCO
203 when "defmacro!"
204 eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
205 a1 = list[1].unwrap
206 eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
207 env.set(a1.str, eval(list[2], env).tap{|n| n.is_macro = true})
208 when "macroexpand"
209 macroexpand(list[1], env)
9bbb8ccc 210 else
97d0deb1 211 invoke_list(list, env)
9bbb8ccc 212 end
97d0deb1 213 end
9bbb8ccc 214 end
9bbb8ccc 215
97d0deb1 216 def print(result)
217 pr_str(result, true)
218 end
9bbb8ccc 219
97d0deb1 220 def rep(str)
221 print(eval(read(str), $repl_env))
222 end
9bbb8ccc 223end
224
225$repl_env = Mal::Env.new nil
226Mal::NS.each{|k,v| $repl_env.set(k, Mal::Type.new(v))}
97d0deb1 227$repl_env.set("eval", Mal::Type.new -> (args: Array(Mal::Type)){ Mal.eval(args[0], $repl_env) })
228Mal.rep "(def! not (fn* (a) (if a false true)))"
229Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
230Mal.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)))))))"
231Mal.rep "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"
9bbb8ccc 232
233$argv = Mal::List.new
234$repl_env.set("*ARGV*", Mal::Type.new $argv)
235
236unless ARGV.empty?
237 if ARGV.size > 1
238 ARGV[1..-1].each do |a|
239 $argv << Mal::Type.new(a)
240 end
241 end
242
243 begin
97d0deb1 244 Mal.rep "(load-file \"#{ARGV[0]}\")"
9bbb8ccc 245 rescue e
246 STDERR.puts e
247 end
248 exit
249end
250
251while line = my_readline("user> ")
252 begin
97d0deb1 253 puts Mal.rep(line)
9bbb8ccc 254 rescue e
255 STDERR.puts e
256 end
257end