Update Crystal implementation
[jackhill/mal.git] / crystal / step8_macros.cr
CommitLineData
9bbb8ccc 1#! /usr/bin/env crystal run
2
5185c56e 3require "readline"
9bbb8ccc 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)
5185c56e 18 ->(args : Array(Mal::Type)) {
97d0deb1 19 new_env = Mal::Env.new(env, binds, args)
20 eval(body, new_env)
5185c56e 21 }.as(Mal::Func)
97d0deb1 22 end
9bbb8ccc 23
97d0deb1 24 def eval_ast(ast, env)
5185c56e 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
5185c56e 37 val.each_with_object(Mal::List.new) { |n, l| l << eval(n, env) }
97d0deb1 38 when Mal::Vector
5185c56e 39 val.each_with_object(Mal::Vector.new) { |n, l| l << eval(n, env) }
97d0deb1 40 when Array(Mal::Type)
5185c56e 41 val.map { |n| eval(n, env).as(Mal::Type) }
97d0deb1 42 when Mal::HashMap
5185c56e 43 val.each { |k, v| val[k] = eval(v, env) }
97d0deb1 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]
5185c56e 73 # (("splice-unquote" ...) ...)
97d0deb1 74 when pair?(head) && (arg0 = head.first.unwrap).is_a?(Mal::Symbol) && arg0.str == "splice-unquote"
5185c56e 75 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new) { |e, l| l << e }
97d0deb1 76 Mal::Type.new(
77 Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
78 )
9bbb8ccc 79 else
5185c56e 80 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new) { |e, l| l << e }
97d0deb1 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)
97d0deb1 102 # Already checked in macro_call?
5185c56e
OR
103 list = ast.unwrap.as(Mal::List)
104 func_sym = list[0].unwrap.as(Mal::Symbol)
97d0deb1 105 func = env.get(func_sym.str).unwrap
9bbb8ccc 106
97d0deb1 107 case func
108 when Mal::Func
109 ast = func.call(list[1..-1])
110 when Mal::Closure
111 ast = func.fn.call(list[1..-1])
112 else
113 eval_error "macro '#{func_sym.str}' must be function: #{ast}"
114 end
115 end
9bbb8ccc 116
97d0deb1 117 ast
118 end
9bbb8ccc 119
97d0deb1 120 macro invoke_list(l, env)
121 f = eval({{l}}.first, {{env}}).unwrap
122 args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}})
123 case f
124 when Mal::Closure
125 ast = f.ast
126 {{env}} = Mal::Env.new(f.env, f.params, args)
127 next # TCO
128 when Mal::Func
129 return f.call args
130 else
131 eval_error "expected function as the first argument: #{f}"
132 end
133 end
9bbb8ccc 134
97d0deb1 135 def eval(ast, env)
136 # 'next' in 'do...end' has a bug in crystal 0.7.1
137 # https://github.com/manastech/crystal/issues/659
138 while true
139 return eval_ast(ast, env) unless ast.unwrap.is_a? Mal::List
140
141 ast = macroexpand(ast, env)
142
143 list = ast.unwrap
144
b1165f91 145 return eval_ast(ast, env) unless list.is_a? Mal::List
97d0deb1 146 return ast if list.empty?
147
148 head = list.first.unwrap
149
150 return invoke_list(list, env) unless head.is_a? Mal::Symbol
151
152 return Mal::Type.new case head.str
5185c56e
OR
153 when "def!"
154 eval_error "wrong number of argument for 'def!'" unless list.size == 3
155 a1 = list[1].unwrap
156 eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
157 env.set(a1.str, eval(list[2], env))
158 when "let*"
159 eval_error "wrong number of argument for 'def!'" unless list.size == 3
160
161 bindings = list[1].unwrap
162 eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
163 eval_error "size of binding list must be even" unless bindings.size.even?
164
165 new_env = Mal::Env.new env
166 bindings.each_slice(2) do |binding|
167 key, value = binding
168 name = key.unwrap
169 eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Mal::Symbol
170 new_env.set(name.str, eval(value, new_env))
171 end
172
173 ast, env = list[2], new_env
174 next # TCO
175 when "do"
176 if list.empty?
177 ast = Mal::Type.new nil
178 next
179 end
180
181 eval_ast(list[1..-2].each_with_object(Mal::List.new) { |i, l| l << i }, env)
182 ast = list.last
183 next # TCO
184 when "if"
185 ast = unless eval(list[1], env).unwrap
186 list.size >= 4 ? list[3] : Mal::Type.new(nil)
9bbb8ccc 187 else
5185c56e
OR
188 list[2]
189 end
190 next # TCO
191 when "fn*"
192 params = list[1].unwrap
193 unless params.is_a? Array
194 eval_error "'fn*' parameters must be list or vector: #{params}"
9bbb8ccc 195 end
5185c56e
OR
196 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
197 when "quote"
198 list[1]
199 when "quasiquote"
200 ast = quasiquote list[1]
201 next # TCO
202 when "defmacro!"
203 eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
204 a1 = list[1].unwrap
205 eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
206 env.set(a1.str, eval(list[2], env).tap { |n| n.is_macro = true })
207 when "macroexpand"
208 macroexpand(list[1], env)
209 else
210 invoke_list(list, env)
211 end
97d0deb1 212 end
9bbb8ccc 213 end
9bbb8ccc 214
97d0deb1 215 def print(result)
216 pr_str(result, true)
217 end
9bbb8ccc 218
97d0deb1 219 def rep(str)
5185c56e 220 print(eval(read(str), REPL_ENV))
97d0deb1 221 end
9bbb8ccc 222end
223
5185c56e
OR
224REPL_ENV = Mal::Env.new nil
225Mal::NS.each { |k, v| REPL_ENV.set(k, Mal::Type.new(v)) }
226REPL_ENV.set("eval", Mal::Type.new ->(args : Array(Mal::Type)) { Mal.eval(args[0], REPL_ENV) })
97d0deb1 227Mal.rep "(def! not (fn* (a) (if a false true)))"
228Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
229Mal.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)))))))"
230Mal.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 231
5185c56e
OR
232argv = Mal::List.new
233REPL_ENV.set("*ARGV*", Mal::Type.new argv)
9bbb8ccc 234
235unless ARGV.empty?
236 if ARGV.size > 1
237 ARGV[1..-1].each do |a|
5185c56e 238 argv << Mal::Type.new(a)
9bbb8ccc 239 end
240 end
241
242 begin
97d0deb1 243 Mal.rep "(load-file \"#{ARGV[0]}\")"
9bbb8ccc 244 rescue e
245 STDERR.puts e
246 end
247 exit
248end
249
5185c56e 250while line = Readline.readline("user> ", true)
9bbb8ccc 251 begin
97d0deb1 252 puts Mal.rep(line)
9bbb8ccc 253 rescue e
254 STDERR.puts e
255 end
256end