-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmsimi.simi
773 lines (704 loc) · 17.7 KB
/
vmsimi.simi
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# Comment
#* multiline comments
span
multiple lines *#
import "date"
import "io" for *
import "net" for *
SUCCESS = "SUCCESS"
testResults = $[total = 0, failureCount = 0, failures = $[]]
fn test(title, expected, proc) {
# print "running $(title)"
r = proc()
testResult = if r == expected SUCCESS else "FAIL got $r expected $expected"
msg = "Testing $title: $testResult"
testResults.total += 1
if testResult != SUCCESS {
testResults.failureCount += 1
testResults.failures += msg
}
}
a = 5
b = a + 3
a = b - 4 / (2 + 2 * 2)
b /= 2
test("a is", 7.333333333333333, a)
test("b is", 4, b)
test("if", "a", =if a < 2 or a < 10 {
"a"
} else if b == 4 {
"b"
} else {
"both are false"
})
test("nil coalescence", 5, fn {
v = nil
return v ?? 5
})
test("nil coalescence assign", 5, fn {
v = nil
v ??= 5
return v
})
test("nil coalescence assign again", 5, fn {
v = 5
v ??= 6
return v
})
test("exception coalescence", 5, fn {
v = Exception("")
return v ?! 5
})
test("exception coalescence assign", 5, fn {
v = Exception("")
v ?!= 5
return v
})
test("exception coalescence assign again", 5, fn {
v = 5
v ?!= 6
return v
})
test("while", "5again43again2", fn {
i = 5
s = ""
while i > 1 {
s += "" + i
if i == 4 {
i -= 1
continue
} else if i == 2 {
break
}
s += "again"
i -= 1
}
return s
})
test("do-while", "123", fn {
i = 1
s = ""
do {
s += "$i"
i += 1
} while i < 4
return s
})
test("do-while one too many", "4", fn {
i = 4
s = ""
do {
s += "$i"
i += 1
} while i < 4
return s
})
test("when", "3 or 4", fn {
i = 3
when i {
2 { return "2" }
3 or 4 { return "3 or 4" }
else { return "else $i" }
}
})
test("if expr", 2, if false {
5
} else if false {
6
} else {
2
})
test("when expr", 5, when b {
2 = 2
4 = 5
else = 6
})
fn funcWithOptionals(what = "func", smth = 6) {
return what + smth
}
test("func with 2 params", "aa", funcWithOptionals("a", "a"))
test("func with 1 optional", "a6", funcWithOptionals("a"))
test("func with 2 optionals", "func6", funcWithOptionals())
fn retFunc() {
return 5
print "this shouldn't be printed"
}
test("func with return", 5, retFunc())
test("recursion", 55, fn {
fn fibo(n) = if n <= 1 n else fibo(n - 1) + fibo(n - 2)
return fibo(10)
})
fn outer() {
x = "outside"
fn inner() {
return x
}
return inner
}
test("closure", "outside", fn {
closure = outer()
return closure()
})
fn selfReturning() {
return self(fn)
}
test("self(fn)", selfReturning, selfReturning)
class Brioche {
fn eat(a, b) {
return "ate $a and $b"
}
}
test("class instantiation and invoke", "ate jam and jelly", Brioche().eat("jam", "jelly"))
test("is", true, Brioche() is Brioche)
test("is not", false, Brioche() is not Brioche)
test("class is", true, Brioche is Class)
class Pair {
init(@first = 5, @second = 6)
}
test("class init with autoset", 3, fn {
pair = Pair(1, 2)
return pair.first + pair.second
})
test("class init with autoset one default", 7, fn {
pair = Pair(1)
return pair.first + pair.second
})
test("class init with autoset two defaults", 11, fn {
pair = Pair()
return pair.first + pair.second
})
class Nested {
ichBin = "Kuhlen"
fn method() {
fn function() {
return @ichBin
}
return function()
}
}
test("nested self access", "Kuhlen", Nested().method())
class Superclass {
fn methodToInherit() {
return "superclass method"
}
}
class Subclass is Superclass {
superClassStr = "my superclass is $(super)"
fn methodToInherit() {
return "$(super.methodToInherit()) overridden"
}
}
test("super access", "my superclass is Superclass", Subclass().superClassStr)
test("method overriding with super invoke", "superclass method overridden", Subclass().methodToInherit())
test("is with superclass", true, Subclass() is Superclass)
test("class is with superclass", true, Subclass is Superclass)
test("Range operator", true, 1..3 is Range)
test("in", true, 2 in 1..4)
test("not in", true, 2 not in 3..4)
test("rangeUntil", true, 4 not in 1..4)
test("rangeTo", true, 4 in 1...4)
test("for loop", "1 2 3 4 5 ", fn {
s = ""
for i in 1...5 {
s += "$i "
}
return s
})
test("for loop with obj decomp", "1a2b3c", fn {
s = ""
for [k, v] in [[1, "a"], [2, "b"], [3, "c"]] {
s += k + v
}
return s
})
test("for else with else triggering", "for else", fn {
for g in nil {
return "nope"
} else {
return "for else"
}
})
test("for else with else not triggering", "yep", fn {
for g in 1 {
return "yep"
} else {
return "for else"
}
})
test("lambda with two implicit params", 3, fn {
return $0 + $1
}(1, 2))
test("shorthand lambda with three implicit params", 3, (=$0+$1+$2)(1, 1, 1))
test("native init", true, Date() != nil)
test("static native function", 1, Date.at(1).time)
test("static native function default arg", 0, Date.at(0).time)
fn funcForBoxing
test("function boxing", "funcForBoxing0", funcForBoxing.name + funcForBoxing.arity)
test("function is", true, funcForBoxing is Function)
class SomeException is Exception
someExceptionMessage = "Some exception"
fn funcReturningException = SomeException(someExceptionMessage)
test("returning exception", someExceptionMessage, funcReturningException().message)
test("rescue alternative value", "alternative", =funcReturningException() catch { return "alternative"} )
fn rescueFunctionWithValueAssign {
value = funcReturningException() catch {
s = "exception $(it.message)"
return s
}
print "nothing happened"
return 1
}
test("rescue function with value assign", "exception $someExceptionMessage", rescueFunctionWithValueAssign())
fn rescueFunctionWithoutValueAssign {
funcReturningException() catch {
s = "exception $(it.message)"
return s
}
print "nothing happened"
return 1
}
test("rescue function with value assign", "exception $someExceptionMessage", rescueFunctionWithoutValueAssign())
fn funcReturningExceptionIfTrue(arg) = if arg funcReturningException() else 1
fn rescueFunctionWithTwoExceptions() {
s = ""
s += funcReturningExceptionIfTrue(false) + funcReturningExceptionIfTrue(false) catch {
s += "error"
}
s += funcReturningExceptionIfTrue(true) + funcReturningExceptionIfTrue(false) catch {
s += "error"
}
s += funcReturningExceptionIfTrue(false) + funcReturningExceptionIfTrue(true) catch {
s += "error"
}
s += funcReturningExceptionIfTrue(true) + funcReturningExceptionIfTrue(true) catch {
s += "error"
}
return s
}
test("rescue function with multiple exception calls", "2errorerrorerror", rescueFunctionWithTwoExceptions())
fn funcWithArgTypeCheck(a0 is String, a1 is String?, a2 is String!) {
return 1
}
test("arg type check proper type passed", 1, funcWithArgTypeCheck("", "", ""))
test("arg type check first nil", "tme", funcWithArgTypeCheck(nil, "", "").message) # TODO rework the checks
test("arg type check first Exception", "tme", funcWithArgTypeCheck(Exception("a"), "", "").message)
test("arg type check second nil", "tme", funcWithArgTypeCheck("", nil, "").message)
test("arg type check second Exception", "tme", funcWithArgTypeCheck("", Exception("a"), "").message)
test("arg type check third nil", "tme", funcWithArgTypeCheck("", "", nil).message)
test("arg type check third Exception", 1, funcWithArgTypeCheck("", "", Exception("a")))
fn safeGetAndCall(arg) = arg?.field?()
test("safe get and call pass", 0, safeGetAndCall([field = =0]))
test("safe get fail", "[class = NilReferenceException, ]", "" + safeGetAndCall(nil))
test("safe call fail", "[class = NilReferenceException, ]", "" + safeGetAndCall([=]))
test("safe get fail and catch", 1, fn {
return safeGetAndCall(nil) catch {
return 1
}
})
fn funcWithReturnTypeCheckCorrect is String {
return ""
}
test("return type check proper type", "", funcWithReturnTypeCheckCorrect())
fn funcWithReturnTypeCheckIncorrect is String {
return 1
}
test("return type check incorrect type", "tme", funcWithReturnTypeCheckIncorrect().message)
fn basicDoBlockTest {
s = ""
do {
s += "in do block"
s += " yup yup"
}
return s
}
test("basic do block", "in do block yup yup", basicDoBlockTest())
fn basicDoElseBlockTest {
s = ""
do {
s += "another do"
} else {
s += "nope"
}
return s
}
test("basic do block", "another do", basicDoElseBlockTest())
fn doElseBreakIt {
s = ""
do {
ab = 2
q = funcReturningException() catch {
c = 6
s += it
s += c
break it
}
s += "nope"
} else {
c = 6
s += c
s += "exception $it"
s += c
}
return s
}
test("do else break it", "[class = SomeException, message = Some exception]66exception [class = SomeException, message = Some exception]6", doElseBreakIt())
fn doElseBreakNil {
s = ""
do {
s += "3rd do"
break
s += "nope"
} else {
c = 6
s += "yup, nil == " + it
s += c
s += it
}
return s
}
test("do else break nil", "3rd doyup, nil == nil6nil", doElseBreakNil())
fn testImplicitSelf {
class SelfTesting {
q = 5
fn r = 3
fn _p = 2
fn w {
s = "" + q
q = 6
s += q
s += _p()
c = r()
s += c
return s
}
}
return SelfTesting().w()
}
test("implicit self", "5623", testImplicitSelf())
test("compound assignment with setters", 7, fn {
oo = $[=]
oo.a = 3
oo.a += 4
return oo.a
})
test("obj literal", 5, =[a = 1, b = 3, c = 5].c)
test("mut list literal", "$[1, 2, 3, 4, 5]", ="" + $[1, 2, 3, 4, 5])
test("mut list value change", 10, fn {
lst = $[1, 2, 3, 4, 5]
lst.2 = 10
return lst.2
})
test("list for iteration", "123", fn {
s = ""
for i in [1, 2, 3] {
s += i
}
return s
})
test("list get num", 2, [1, 2, 3].1)
test("list get negative num", 3, [1, 2, 3].(-1))
#test("list get identifier", "aaa", [1, 2, 3].size)
test("list get range", "[2, 3]", "" + [1, 2, 3].(1...3))
test("obj decomp", "nilnil5", fn {
obj = [a = 1, b = 3, c = 5]
[d, e, c] = obj
s = "" + d
s += e
s += c
return s
})
test("custom toString", "10", fn {
class ToString {
init(@val)
fn toString = "$(@val)"
}
return "" + ToString(10)
})
test("annotations", "[AnnotatedClass = [[a = class annot], Annotation constructor annot], field = [Annotation field annot], a = [[a = fun annot]], b = [[a = 2nd fun annot]]]", fn {
class AnnotationClass {
init(@msg)
fn toString = "Annotation $(@msg)"
}
![a = "class annot"]
!AnnotationClass("constructor annot")
class AnnotatedClass {
!AnnotationClass("field annot")
field = 5
![a = "fun annot"]
fn a() {
print "55"
}
![a = "2nd fun annot"]
fn b() {
print "66"
}
}
return "" + AnnotatedClass!
})
test("gu", 6, gu "2 + 2 * 2")
test("gu with lexer exception", "lex", fn {
return gu "2 ++ 2" catch { return "lex" }
})
test("gu with compile exception", "comp", fn {
return gu "for 2 in 3" catch { return "comp" }
})
test("fiber yield from foreach", "121232453564785906", fn {
fn foreach(it, fun) {
for i in it {
fun(i)
}
}
fib Fiber(p1, p2) {
foreach(1..10, fn (i){
yield "" + p1 + p2 + i
})
}
s = ""
fajber = Fiber()
s += fajber(1, 2)
s += fajber(2, 3)
s += fajber(4, 5)
s += fajber(5, 6)
s += fajber(7, 8)
s += fajber(9, 0)
return s
})
fn foreach(it, fun) {
for i in it {
fun(i)
}
}
test("two fibers", 8, fn {
fib F1(p) {
foreach(p..5, fn (i){
yield i
})
}
fib F2(p) {
sum = 0
f1 = F1()
while sum < 5 {
sum += p
sum += f1(1)
yield sum
}
}
f2 = F2()
res = f2(1)
res += f2(2)
return res
})
test("onsite import", "6 innerA innerB 3 3", fn {
module ModuleClass {
field = 2 + 2 * 2
class InnerA {
fn toString = "innerA"
}
class InnerB {
fn toString = "innerB"
}
module InnerModule {
class InnerModuleClass
fn InnerModuleDef = 3
}
}
fn moduleTest {
import ModuleClass for field, InnerA, InnerB, InnerModule
return field + " " + InnerA() + " " + InnerB() + " " + InnerModule.InnerModuleDef() + " " + ModuleClass.InnerModule.InnerModuleDef()
}
return moduleTest()
})
test("proc do", 5, fn {
i = 10
p = do {
if i < 20 {
break 20
}
5
}
i = p() + 5
j = p()
return j
})
test("proc do else", 20, fn {
i = 10
p = do {
if i < 20 {
break 20
}
5
} else {
25
}
i = p() + 5
j = p()
return j
})
test("list comprehension", "[2, 3, 4, 5]", "" + [for i in 1..5 do i + 1])
test("mutable list comprehension with if", "$[2, 4]", "" + $[for i in 1..5 if i % 2 == 0 do i])
test("mutable obj comprehension", "$[key1 = 11, key2 = 12, key3 = 13, key4 = 14]", "" + $[for i in 1..5 do "key$i" = i + 10])
test("obj comprehension with if", "[key4 = 16]", "" + [for i in 1..5 if i > 3 do "key$i" = i * i])
enum Enum {
RED(1, 2), BLUE(3, 4), GREEN(5, 6)
init(@a, @b)
}
test("enum same", true, fn {
blu = Enum.BLUE
return blu == Enum.BLUE
})
test("enum different", true, fn {
blu = Enum.BLUE
return blu != Enum.RED
})
test("enum values and toString", "[RED, BLUE, GREEN]", "" + Enum.values)
test("enum ordinal", 2, Enum.GREEN.ordinal())
test("stream", "$[6, 12]", "" + Stream([1, 2, 3, 4, 5]).where(=$0 < 5).map(=$0 * 2).where(=$0 < 5).map(=$0 * 3).toList())
test("stream without collect", "$[2, 4]", "" + [1, 2, 3].where(=$0 < 3).map(=$0 * 2))
test("when expression with complex expression", 3, fn {
par = Pair(1, 2)
return when par.first + par.second {
1 = 1
3 = 3
else = 5
}
})
extend Pair {
fn sum = first + second
}
test("extensions", 3, fn {
par = Pair(1, 2)
return par.sum()
})
test("string iterate", "abcde", fn {
s = ""
for c in "abcde" {
s += c
}
return s
})
print "Testing complete, failed $(testResults.failureCount) / $(testResults.total)"
for failure in testResults.failures {
print failure
}
fib MyFiber(a, b, c) {
foreach(a..b, fn (i) {
yield i + c
})
}
myFiber = MyFiber()
print myFiber(0, 3, 1)
print myFiber(5, 3, 5)
print myFiber(6, 3, 10)
print myFiber(8, 3, 20)
print myFiber(5, 10, 0)
#def threeArg(a, b, c) {
# print a
# print b
# print c
#}
#ls = [1, 2, 3]
#threeArg(*ls)
print "three stooges".chars()
extend String {
fn replaceNewlinesWithBreaks = replacing("\n", "<br/>")
}
print "a string\nwith\nnewlines\nyay!".replaceNewlinesWithBreaks()
class DbTable {
init(@name = nil)
}
class DbField {
init(@type, @name = nil, @primaryKey = false)
}
class Model {
!DbField(Num, nil, true)
id = 0
}
!DbTable("Users")
class User is Model {
!DbField(String, "first_name")
firstName = ""
!DbField(String, "last_name")
lastName = ""
}
!DbTable("Appointments")
class Appointment is Model {
!DbField(String)
userId = ""
!DbField(Date, "timeslot")
time = Date()
!DbTable()
fake = 0
}
class DbLib {
init(tables) {
sqlBuilder = String.builder()
for table in tables {
@sqlForTable(table, sqlBuilder)
}
@sql = sqlBuilder.build()
}
fn sqlForTable(table, sqlBuilder) {
ans = table!
tableName = "" + table
for tableAn in ans.(tableName).where(=$0 is DbTable) {
name = tableAn.name ?? tableName
sqlBuilder.add("CREATE TABLE ").add(name).add(" (\n")
@sqlForFields(ans.where(=$0 != tableName), sqlBuilder)
sqlBuilder.add(");\n")
}
}
fn sqlForFields(ans, sqlBuilder) {
for [field, fieldAns] in ans.zip() {
print "inspecting $field" # B
dbFieldAn = fieldAns.where(=$0 is DbField).0 catch {
print "don't have one"
continue
}
name = dbFieldAn.name ?? field
type = when dbFieldAn.type {
Num = "int"
String = "varchar(255)"
Date = "date"
else = nil
}
sqlBuilder.add(name).add(" ").add(type)
if dbFieldAn.primaryKey {
if type == "int" {
sqlBuilder.add(" NOT NULL AUTO_INCREMENT,")
}
sqlBuilder.add("\nPRIMARY KEY (").add(name).add("),")
} else {
sqlBuilder.add(",")
}
sqlBuilder.add("\n")
}
}
}
print DbLib([Model, User, Appointment]).sql
print ["a", "b", "c"].joinToString()
class ArithmeticSequence {
init(@a0, @d)
fib Generator(sequence) {
an = sequence.a0
while true {
yield an
an += sequence.d
}
}
fn iterate = [g = Generator(), src = self, next = =@g(@src)]
}
#print httpRequest([verb = "GET", url = "http://httpbin.org/ip"])
#for i in ArithmeticSequence(1, 5) { # B
# print i # 1, 6, 11, 16, etc. - this is an infinite loop after all!
#}
#for i in 1000 {
# lst = List()
# for j in 1000 {
# print j
# if j % 2 == 0 {
# continue
# }
# lst += j
# }
## [for j in 1000 do j]
#}