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