Merge remote-tracking branch 'kanaka/master' into fsharp
[jackhill/mal.git] / crystal / core.cr
1 require "time"
2
3 require "./types"
4 require "./error"
5 require "./printer"
6 require "./reader"
7 require "./readline"
8
9 module Mal
10
11 macro calc_op(op)
12 -> (args : Array(Mal::Type)) {
13 x, y = args[0].unwrap, args[1].unwrap
14 eval_error "invalid arguments for binary operator {{op.id}}" unless x.is_a?(Int32) && y.is_a?(Int32)
15 Mal::Type.new(x {{op.id}} y)
16 }
17 end
18
19 def self.list(args)
20 args.to_mal
21 end
22
23 def self.list?(args)
24 args.first.unwrap.is_a? Mal::List
25 end
26
27 def self.empty?(args)
28 a = args.first.unwrap
29 a.is_a?(Array) ? a.empty? : false
30 end
31
32 def self.count(args)
33 a = args.first.unwrap
34 case a
35 when Array
36 a.size as Int32
37 when Nil
38 0
39 else
40 eval_error "invalid argument for function 'count'"
41 end
42 end
43
44 def self.pr_str_(args)
45 args.map{|a| pr_str(a)}.join(" ")
46 end
47
48 def self.str(args)
49 args.map{|a| pr_str(a, false)}.join
50 end
51
52 def self.prn(args)
53 puts self.pr_str_(args)
54 nil
55 end
56
57 def self.println(args)
58 puts args.map{|a| pr_str(a, false)}.join(" ")
59 nil
60 end
61
62 def self.read_string(args)
63 head = args.first.unwrap
64 eval_error "argument of read-str must be string" unless head.is_a? String
65 read_str head
66 end
67
68 def self.slurp(args)
69 head = args.first.unwrap
70 eval_error "argument of slurp must be string" unless head.is_a? String
71 begin
72 File.read head
73 rescue e : Errno
74 eval_error "no such file"
75 end
76 end
77
78 def self.cons(args)
79 head, tail = args[0] as Mal::Type, args[1].unwrap
80 eval_error "2nd arg of cons must be list" unless tail.is_a? Array
81 ([head] + tail).to_mal
82 end
83
84 def self.concat(args)
85 args.each_with_object(Mal::List.new) do |arg, list|
86 a = arg.unwrap
87 eval_error "arguments of concat must be list" unless a.is_a?(Array)
88 a.each{|e| list << e}
89 end
90 end
91
92 def self.nth(args)
93 a0, a1 = args[0].unwrap, args[1].unwrap
94 eval_error "1st argument of nth must be list or vector" unless a0.is_a? Array
95 eval_error "2nd argument of nth must be integer" unless a1.is_a? Int32
96 a0[a1]
97 end
98
99 def self.first(args)
100 a0 = args[0].unwrap
101
102 return nil if a0.nil?
103 eval_error "1st argument of first must be list or vector or nil" unless a0.is_a? Array
104 a0.empty? ? nil : a0.first
105 end
106
107 def self.rest(args)
108 a0 = args[0].unwrap
109
110 return Mal::List.new if a0.nil?
111 eval_error "1st argument of first must be list or vector or nil" unless a0.is_a? Array
112 return Mal::List.new if a0.empty?
113 a0[1..-1].to_mal
114 end
115
116 def self.apply(args)
117 eval_error "apply must take at least 2 arguments" unless args.size >= 2
118
119 head = args.first.unwrap
120 last = args.last.unwrap
121
122 eval_error "last argument of apply must be list or vector" unless last.is_a? Array
123
124 case head
125 when Mal::Closure
126 head.fn.call(args[1..-2] + last)
127 when Mal::Func
128 head.call(args[1..-2] + last)
129 else
130 eval_error "1st argument of apply must be function or closure"
131 end
132 end
133
134 def self.map(args)
135 func = args.first.unwrap
136 list = args[1].unwrap
137
138 eval_error "2nd argument of map must be list or vector" unless list.is_a? Array
139
140 f = case func
141 when Mal::Closure then func.fn
142 when Mal::Func then func
143 else eval_error "1st argument of map must be function"
144 end
145
146 list.each_with_object(Mal::List.new) do |elem, mapped|
147 mapped << f.call([elem])
148 end
149 end
150
151 def self.nil?(args)
152 args.first.unwrap.nil?
153 end
154
155 def self.true?(args)
156 a = args.first.unwrap
157 a.is_a?(Bool) && a
158 end
159
160 def self.false?(args)
161 a = args.first.unwrap
162 a.is_a?(Bool) && !a
163 end
164
165 def self.symbol?(args)
166 args.first.unwrap.is_a?(Mal::Symbol)
167 end
168
169 def self.symbol(args)
170 head = args.first.unwrap
171 eval_error "1st argument of symbol function must be string" unless head.is_a? String
172 Mal::Symbol.new head
173 end
174
175 def self.keyword(args)
176 head = args.first.unwrap
177 eval_error "1st argument of symbol function must be string" unless head.is_a? String
178 "\u029e" + head
179 end
180
181 def self.keyword?(args)
182 head = args.first.unwrap
183 head.is_a?(String) && !head.empty? && head[0] == '\u029e'
184 end
185
186 def self.vector(args)
187 args.to_mal(Mal::Vector)
188 end
189
190 def self.vector?(args)
191 args.first.unwrap.is_a? Mal::Vector
192 end
193
194 def self.hash_map(args)
195 eval_error "hash-map must take even number of arguments" unless args.size.even?
196 map = Mal::HashMap.new
197 args.each_slice(2) do |kv|
198 k = kv[0].unwrap
199 eval_error "key must be string" unless k.is_a? String
200 map[k] = kv[1]
201 end
202 map
203 end
204
205 def self.map?(args)
206 args.first.unwrap.is_a? Mal::HashMap
207 end
208
209 def self.assoc(args)
210 head = args.first.unwrap
211 eval_error "1st argument of assoc must be hashmap" unless head.is_a? Mal::HashMap
212 eval_error "assoc must take a list and even number of arguments" unless (args.size - 1).even?
213
214 map = Mal::HashMap.new
215 head.each{|k, v| map[k] = v}
216
217 args[1..-1].each_slice(2) do |kv|
218 k = kv[0].unwrap
219 eval_error "key must be string" unless k.is_a? String
220 map[k] = kv[1]
221 end
222
223 map
224 end
225
226 def self.dissoc(args)
227 head = args.first.unwrap
228 eval_error "1st argument of assoc must be hashmap" unless head.is_a? Mal::HashMap
229
230 map = Mal::HashMap.new
231 head.each{|k,v| map[k] = v}
232
233 args[1..-1].each do |arg|
234 key = arg.unwrap
235 eval_error "key must be string" unless key.is_a? String
236 map.delete key
237 end
238
239 map
240 end
241
242 def self.get(args)
243 a0, a1 = args[0].unwrap, args[1].unwrap
244 return nil unless a0.is_a? Mal::HashMap
245 eval_error "2nd argument of get must be string" unless a1.is_a? String
246
247 # a0[a1]? isn't available because type ofa0[a1] is infered NoReturn
248 a0.has_key?(a1) ? a0[a1] : nil
249 end
250
251 def self.contains?(args)
252 a0, a1 = args[0].unwrap, args[1].unwrap
253 eval_error "1st argument of get must be hashmap" unless a0.is_a? Mal::HashMap
254 eval_error "2nd argument of get must be string" unless a1.is_a? String
255 a0.has_key? a1
256 end
257
258 def self.keys(args)
259 head = args.first.unwrap
260 eval_error "1st argument of assoc must be hashmap" unless head.is_a? Mal::HashMap
261 head.keys.each_with_object(Mal::List.new){|e,l| l << Mal::Type.new(e)}
262 end
263
264 def self.vals(args)
265 head = args.first.unwrap
266 eval_error "1st argument of assoc must be hashmap" unless head.is_a? Mal::HashMap
267 head.values.to_mal
268 end
269
270 def self.sequential?(args)
271 args.first.unwrap.is_a? Array
272 end
273
274 def self.readline(args)
275 head = args.first.unwrap
276 eval_error "1st argument of readline must be string" unless head.is_a? String
277 my_readline head
278 end
279
280 def self.meta(args)
281 m = args.first.meta
282 m.nil? ? nil : m
283 end
284
285 def self.with_meta(args)
286 t = args.first.dup
287 t.meta = args[1]
288 t
289 end
290
291 def self.atom(args)
292 Mal::Atom.new args.first
293 end
294
295 def self.atom?(args)
296 args.first.unwrap.is_a? Mal::Atom
297 end
298
299 def self.deref(args)
300 head = args.first.unwrap
301 eval_error "1st argument of deref must be atom" unless head.is_a? Mal::Atom
302 head.val
303 end
304
305 def self.reset!(args)
306 head = args.first.unwrap
307 eval_error "1st argument of reset! must be atom" unless head.is_a? Mal::Atom
308 head.val = args[1]
309 end
310
311 def self.swap!(args)
312 atom = args.first.unwrap
313 eval_error "1st argument of swap! must be atom" unless atom.is_a? Mal::Atom
314
315 a = [atom.val] + args[2..-1]
316
317 func = args[1].unwrap
318 case func
319 when Mal::Func
320 atom.val = func.call a
321 when Mal::Closure
322 atom.val = func.fn.call a
323 else
324 eval_error "2nd argumetn of swap! must be function"
325 end
326 end
327
328 def self.conj(args)
329 seq = args.first.unwrap
330 case seq
331 when Mal::List
332 (args[1..-1].reverse + seq).to_mal
333 when Mal::Vector
334 (seq + args[1..-1]).to_mal(Mal::Vector)
335 else
336 eval_error "1st argument of conj must be list or vector"
337 end
338 end
339
340 def self.time_ms(args)
341 (Time.now.to_i.to_i32) * 1000
342 end
343
344 # Note:
345 # Simply using ->self.some_func doesn't work
346 macro func(name)
347 -> (args : Array(Mal::Type)) { Mal::Type.new self.{{name.id}}(args) }
348 end
349
350 macro rel_op(op)
351 -> (args : Array(Mal::Type)) { Mal::Type.new (args[0] {{op.id}} args[1]) }
352 end
353
354 NS = {
355 "+" => calc_op(:+)
356 "-" => calc_op(:-)
357 "*" => calc_op(:*)
358 "/" => calc_op(:/)
359 "list" => func(:list)
360 "list?" => func(:list?)
361 "empty?" => func(:empty?)
362 "count" => func(:count)
363 "=" => rel_op(:==)
364 "<" => rel_op(:<)
365 ">" => rel_op(:>)
366 "<=" => rel_op(:<=)
367 ">=" => rel_op(:>=)
368 "pr-str" => func(:pr_str_)
369 "str" => func(:str)
370 "prn" => func(:prn)
371 "println" => func(:println)
372 "read-string" => func(:read_string)
373 "slurp" => func(:slurp)
374 "cons" => func(:cons)
375 "concat" => func(:concat)
376 "nth" => func(:nth)
377 "first" => func(:first)
378 "rest" => func(:rest)
379 "throw" => -> (args : Array(Mal::Type)) { raise Mal::RuntimeException.new args[0] }
380 "apply" => func(:apply)
381 "map" => func(:map)
382 "nil?" => func(:nil?)
383 "true?" => func(:true?)
384 "false?" => func(:false?)
385 "symbol?" => func(:symbol?)
386 "symbol" => func(:symbol)
387 "keyword" => func(:keyword)
388 "keyword?" => func(:keyword?)
389 "vector" => func(:vector)
390 "vector?" => func(:vector?)
391 "hash-map" => func(:hash_map)
392 "map?" => func(:map?)
393 "assoc" => func(:assoc)
394 "dissoc" => func(:dissoc)
395 "get" => func(:get)
396 "contains?" => func(:contains?)
397 "keys" => func(:keys)
398 "vals" => func(:vals)
399 "sequential?" => func(:sequential?)
400 "readline" => func(:readline)
401 "meta" => func(:meta)
402 "with-meta" => func(:with_meta)
403 "atom" => func(:atom)
404 "atom?" => func(:atom?)
405 "deref" => func(:deref)
406 "deref" => func(:deref)
407 "reset!" => func(:reset!)
408 "swap!" => func(:swap!)
409 "conj" => func(:conj)
410 "time-ms" => func(:time_ms)
411 } of String => Mal::Func
412
413 end