1 #! /usr/bin/env crystal run
12 # Employed downcase names because Crystal prohibits uppercase names for methods
17 def func_of(env, binds, body)
18 -> (args : Array(Mal::Type)) {
19 new_env = Mal::Env.new(env, binds, args)
24 def eval_ast(ast, env)
25 return ast.map{|n| eval(n, env) as Mal::Type} if ast.is_a? Mal::List
29 Mal::Type.new case val
31 if e = env.get(val.str)
34 eval_error "'#{val.str}' not found"
37 val.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
39 val.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
41 val.map{|n| eval(n, env)}
43 val.each{|k, v| val[k] = eval(v, env)}
55 {{list}}.is_a?(Array) && !{{list}}.empty?
63 Mal::List.new << gen_type(Mal::Symbol, "quote") << ast
67 head = list.first.unwrap
71 when head.is_a?(Mal::Symbol) && head.str == "unquote"
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].to_mal
77 Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
80 tail = Mal::Type.new list[1..-1].to_mal
82 Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
87 def macro_call?(ast, env)
89 return false unless list.is_a? Mal::List
91 sym = list.first.unwrap
92 return false unless sym.is_a? Mal::Symbol
94 func = env.find(sym.str).try(&.data[sym.str])
95 return false unless func && func.macro?
100 def macroexpand(ast, env)
101 while macro_call?(ast, env)
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
110 ast = func.call(list[1..-1])
112 ast = func.fn.call(list[1..-1])
114 eval_error "macro '#{func_sym.str}' must be function: #{ast}"
121 macro invoke_list(l, env)
122 f = eval({{l}}.first, {{env}}).unwrap
123 args = eval_ast({{l}}[1..-1].to_mal, {{env}})
127 {{env}} = Mal::Env.new(f.env, f.params, args)
132 eval_error "expected function as the first argument: #{f}"
137 # 'next' in 'do...end' has a bug in crystal 0.7.1
138 # https://github.com/manastech/crystal/issues/659
140 return eval_ast(ast, env) unless ast.unwrap.is_a? Mal::List
142 ast = macroexpand(ast, env)
146 return ast unless list.is_a? Mal::List
147 return ast if list.empty?
149 head = list.first.unwrap
151 return invoke_list(list, env) unless head.is_a? Mal::Symbol
153 return Mal::Type.new case head.str
155 eval_error "wrong number of argument for 'def!'" unless list.size == 3
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))
160 eval_error "wrong number of argument for 'def!'" unless list.size == 3
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?
166 new_env = Mal::Env.new env
167 bindings.each_slice(2) do |binding|
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))
174 ast, env = list[2], new_env
178 ast = Mal::Type.new nil
182 eval_ast(list[1..-2].to_mal, env)
186 ast = unless eval(list[1], env).unwrap
187 list.size >= 4 ? list[3] : Mal::Type.new(nil)
193 params = list[1].unwrap
194 unless params.is_a? Array
195 eval_error "'fn*' parameters must be list or vector: #{params}"
197 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
201 ast = quasiquote list[1]
204 eval_error "wrong number of argument for 'defmacro!'" unless list.size == 3
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})
209 macroexpand(list[1], env)
211 catch_list = list[2].unwrap
212 return eval(list[1], env) unless catch_list.is_a? Mal::List
214 catch_head = catch_list.first.unwrap
215 return eval(list[1], env) unless catch_head.is_a? Mal::Symbol
216 return eval(list[1], env) unless catch_head.str == "catch*"
220 rescue e : Mal::RuntimeException
221 new_env = Mal::Env.new(env, [catch_list[1]], [e.thrown])
222 eval(catch_list[2], new_env)
224 new_env = Mal::Env.new(env, [catch_list[1]], [Mal::Type.new e.message])
225 eval(catch_list[2], new_env)
228 invoke_list(list, env)
238 print(eval(read(str), $repl_env))
242 $repl_env = Mal::Env.new nil
243 Mal::NS.each{|k,v| $repl_env.set(k, Mal::Type.new(v))}
244 $repl_env.set("eval", Mal::Type.new -> (args: Array(Mal::Type)){ Mal.eval(args[0], $repl_env) })
245 Mal.rep "(def! not (fn* (a) (if a false true)))"
246 Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
247 Mal.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)))))))"
248 Mal.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))))))))"
250 $argv = Mal::List.new
251 $repl_env.set("*ARGV*", Mal::Type.new $argv)
255 ARGV[1..-1].each do |a|
256 $argv << Mal::Type.new(a)
261 Mal.rep "(load-file \"#{ARGV[0]}\")"
268 while line = my_readline("user> ")