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).as(Mal::Type) }
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 is_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 }
77 Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
80 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new) { |e, l| l << e }
82 Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
87 macro invoke_list(l, env)
88 f = eval({{l}}.first, {{env}}).unwrap
89 args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}})
93 {{env}} = Mal::Env.new(f.env, f.params, args)
98 eval_error "expected function as the first argument"
103 # 'next' in 'do...end' has a bug in crystal 0.7.1
104 # https://github.com/manastech/crystal/issues/659
108 return eval_ast(ast, env) unless list.is_a? Mal::List
109 return gen_type Mal::List if list.empty?
111 head = list.first.unwrap
113 unless head.is_a? Mal::Symbol
114 return invoke_list(list, env)
117 return Mal::Type.new case head.str
119 eval_error "wrong number of argument for 'def!'" unless list.size == 3
121 eval_error "1st argument of 'def!' must be symbol" unless a1.is_a? Mal::Symbol
122 env.set(a1.str, eval(list[2], env))
124 eval_error "wrong number of argument for 'def!'" unless list.size == 3
126 bindings = list[1].unwrap
127 eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
128 eval_error "size of binding list must be even" unless bindings.size.even?
130 new_env = Mal::Env.new env
131 bindings.each_slice(2) do |binding|
134 eval_error "name of binding must be specified as symbol" unless name.is_a? Mal::Symbol
135 new_env.set(name.str, eval(value, new_env))
138 ast, env = list[2], new_env
142 ast = Mal::Type.new nil
146 eval_ast(list[1..-2].each_with_object(Mal::List.new) { |i, l| l << i }, env)
150 ast = unless eval(list[1], env).unwrap
151 list.size >= 4 ? list[3] : Mal::Type.new(nil)
157 params = list[1].unwrap
158 unless params.is_a? Array
159 eval_error "'fn*' parameters must be list"
161 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
165 ast = quasiquote list[1]
168 invoke_list(list, env)
178 print(eval(read(str), REPL_ENV))
182 REPL_ENV = Mal::Env.new nil
183 Mal::NS.each { |k, v| REPL_ENV.set(k, Mal::Type.new(v)) }
184 REPL_ENV.set("eval", Mal::Type.new ->(args : Array(Mal::Type)) { Mal.eval(args[0], REPL_ENV) })
185 Mal.rep "(def! not (fn* (a) (if a false true)))"
186 Mal.rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
188 REPL_ENV.set("*ARGV*", Mal::Type.new argv)
192 ARGV[1..-1].each do |a|
193 argv << Mal::Type.new(a)
198 Mal.rep "(load-file \"#{ARGV[0]}\")"
205 while line = Readline.readline("user> ", true)
208 rescue e : Mal::RuntimeException
209 STDERR.puts "Error: #{pr_str(e.thrown, true)}"
211 STDERR.puts "Error: #{e}"