Merge pull request #440 from aasimk2000/add-nil-if-test
[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
90c66423 90 return false if list.empty?
9bbb8ccc 91
97d0deb1 92 sym = list.first.unwrap
93 return false unless sym.is_a? Mal::Symbol
9bbb8ccc 94
97d0deb1 95 func = env.find(sym.str).try(&.data[sym.str])
96 return false unless func && func.macro?
9bbb8ccc 97
97d0deb1 98 true
99 end
9bbb8ccc 100
97d0deb1 101 def macroexpand(ast, env)
102 while macro_call?(ast, env)
97d0deb1 103 # Already checked in macro_call?
5185c56e
OR
104 list = ast.unwrap.as(Mal::List)
105 func_sym = list[0].unwrap.as(Mal::Symbol)
97d0deb1 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
90c66423
JM
140 list = ast.unwrap
141
142 return eval_ast(ast, env) unless list.is_a? Mal::List
143 return ast if list.empty?
97d0deb1 144
145 ast = macroexpand(ast, env)
146
147 list = ast.unwrap
148
b1165f91 149 return eval_ast(ast, env) unless list.is_a? Mal::List
97d0deb1 150 return ast if list.empty?
151
152 head = list.first.unwrap
153
154 return invoke_list(list, env) unless head.is_a? Mal::Symbol
155
156 return Mal::Type.new case head.str
5185c56e
OR
157 when "def!"
158 eval_error "wrong number of argument for 'def!'" unless list.size == 3
159 a1 = list[1].unwrap
160 eval_error "1st argument of 'def!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
161 env.set(a1.str, eval(list[2], env))
162 when "let*"
163 eval_error "wrong number of argument for 'def!'" unless list.size == 3
164
165 bindings = list[1].unwrap
166 eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
167 eval_error "size of binding list must be even" unless bindings.size.even?
168
169 new_env = Mal::Env.new env
170 bindings.each_slice(2) do |binding|
171 key, value = binding
172 name = key.unwrap
173 eval_error "name of binding must be specified as symbol #{name}" unless name.is_a? Mal::Symbol
174 new_env.set(name.str, eval(value, new_env))
175 end
176
177 ast, env = list[2], new_env
178 next # TCO
179 when "do"
180 if list.empty?
181 ast = Mal::Type.new nil
182 next
183 end
184
185 eval_ast(list[1..-2].each_with_object(Mal::List.new) { |i, l| l << i }, env)
186 ast = list.last
187 next # TCO
188 when "if"
189 ast = unless eval(list[1], env).unwrap
190 list.size >= 4 ? list[3] : Mal::Type.new(nil)
9bbb8ccc 191 else
5185c56e
OR
192 list[2]
193 end
194 next # TCO
195 when "fn*"
196 params = list[1].unwrap
197 unless params.is_a? Array
198 eval_error "'fn*' parameters must be list or vector: #{params}"
9bbb8ccc 199 end
5185c56e
OR
200 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
201 when "quote"
202 list[1]
203 when "quasiquote"
204 ast = quasiquote list[1]
205 next # TCO
206 when "defmacro!"
207 eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
208 a1 = list[1].unwrap
209 eval_error "1st argument of 'defmacro!' must be symbol: #{a1}" unless a1.is_a? Mal::Symbol
210 env.set(a1.str, eval(list[2], env).tap { |n| n.is_macro = true })
211 when "macroexpand"
212 macroexpand(list[1], env)
213 else
214 invoke_list(list, env)
215 end
97d0deb1 216 end
9bbb8ccc 217 end
9bbb8ccc 218
97d0deb1 219 def print(result)
220 pr_str(result, true)
221 end
9bbb8ccc 222
97d0deb1 223 def rep(str)
5185c56e 224 print(eval(read(str), REPL_ENV))
97d0deb1 225 end
9bbb8ccc 226end
227
5185c56e
OR
228REPL_ENV = Mal::Env.new nil
229Mal::NS.each { |k, v| REPL_ENV.set(k, Mal::Type.new(v)) }
230REPL_ENV.set("eval", Mal::Type.new ->(args : Array(Mal::Type)) { Mal.eval(args[0], REPL_ENV) })
97d0deb1 231Mal.rep "(def! not (fn* (a) (if a false true)))"
e6d41de4 232Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"
97d0deb1 233Mal.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)))))))"
9bbb8ccc 234
5185c56e
OR
235argv = Mal::List.new
236REPL_ENV.set("*ARGV*", Mal::Type.new argv)
9bbb8ccc 237
238unless ARGV.empty?
239 if ARGV.size > 1
240 ARGV[1..-1].each do |a|
5185c56e 241 argv << Mal::Type.new(a)
9bbb8ccc 242 end
243 end
244
245 begin
97d0deb1 246 Mal.rep "(load-file \"#{ARGV[0]}\")"
9bbb8ccc 247 rescue e
248 STDERR.puts e
249 end
250 exit
251end
252
5185c56e 253while line = Readline.readline("user> ", true)
9bbb8ccc 254 begin
97d0deb1 255 puts Mal.rep(line)
dd7a4f55
JM
256 rescue e : Mal::RuntimeException
257 STDERR.puts "Error: #{pr_str(e.thrown, true)}"
9bbb8ccc 258 rescue e
dd7a4f55 259 STDERR.puts "Error: #{e}"
9bbb8ccc 260 end
261end