git @ Cat's Eye Technologies Castile / master tests / Castile.md
master

Tree @master (Download .tar.gz)

Castile.md @masterview rendered · raw · history · blame

   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
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
Castile
=======

This is a test suite for Castile, written in [Falderal][] format.

While it cannot serve as a complete specification, it comes as
close as anything currently does to a specification for Castile.

[Falderal]: https://catseye.tc/node/Falderal

    -> Tests for functionality "Run Castile Program"

### Rudiments ###

Minimal correct program.

    | fun main() {}
    = 

A program may evaluate to a value.

    | fun main() { 160 }
    = 160

The function named `main` is the one that is evaluated when the
program is run.

    | fun foobar(a, b, c) { 100 }
    | fun main() { 120 }
    | fun f() { 140 }
    = 120

`main` should have no formal arguments.

    | fun main(a, b, c) {
    |   120
    | }
    ? type mismatch

But other functions may.

    | fun foobar(a, b) { b }
    | fun main() { foobar(100, 200) }
    = 200

Defined function names must be unique.

    | fun dup() { 1 }
    | fun dup() { 2 }
    ? duplicate

Formal argument names must be unique.

    | fun f(g, g) {}
    | fun main() { 1 }
    ? defined

Functions must be defined before they are referenced.

    | fun main() { f(7) }
    | fun f(g) { g }
    ? undefined

Either that, or forward-declared.

    | f : integer -> integer
    | fun main() { f(7) }
    | fun f(g) { g * 2 }
    = 14

If forward-declared, types must match.

    | f : integer -> string
    | fun main() { f(7) }
    | fun f(g) { g * 2 }
    ? type mismatch

Arguments must match...

    | fun f(g, h) { g * 2 + h * 2 }
    | fun main() { f(7) }
    ? argument mismatch

    | fun f(g, h) { g * 2 + h * 2 }
    | fun main() { f(7,8,9) }
    ? argument mismatch

### Statements ###

Statements are commands that have the type void and are executed for their
side-effects.  So, in general, statements may not be expressions.  The
exception is that the last statement in a block may be an expression; the
result of that expression is the value of the block.

    | fun main() {
    |   20 * 8
    | }
    = 160

    | fun main() {
    |   20 + 3 * 8;
    |   20 * 8
    | }
    ? type mismatch

An `if`/`else` lets you make decisions.

    | fun main() {
    |   a = 0;
    |   if 3 > 2 {
    |     a = 70
    |   } else {
    |     a = 80
    |   }
    |   a
    | }
    = 70

An `if` need not have an `else`.

    | fun main() {
    |   a = 60
    |   if 3 > 2 {
    |     a = 70
    |   }
    |   a
    | }
    = 70

`if` always typechecks to void, one branch or two.

    | fun main() {
    |   a = 60
    |   if 3 > 2 {
    |     a = 70
    |   }
    | }
    = 

    | fun main() {
    |   a = 60
    |   if 3 > 2 {
    |     a = 70
    |   } else {
    |     a = 90
    |   }
    | }
    = 

If an `if` does have an `else`, the part after `else` must be either a block
(already shown) or another `if`.

    | fun main() {
    |   if 2 > 3 {
    |     return 60
    |   } else if 4 > 5 {
    |     return 0
    |   } else {
    |     return 1
    |   }
    | }
    = 1

No dangling else problem.

    | fun main() {
    |   if 2 > 3 {
    |     return 60
    |   } else if 4 < 5 {
    |     return 99
    |   } else {
    |     return 1
    |   }
    | }
    = 99

`while` loops.

    | fun main() {
    |   a = 0 b = 4
    |   while b > 0 {
    |     a = a + b
    |     b = b - 1
    |   }
    |   a
    | }
    = 10

A `while` itself has void type.

    | fun main() {
    |   a = 0; b = 4;
    |   while b > 0 {
    |     a = a + b;
    |     b = b - 1;
    |   }
    | }
    = 

`break` may be used to prematurely exit a `while`.

    | fun main() {
    |   a = 0; b = 0;
    |   while true {
    |     a = a + b;
    |     b = b + 1;
    |     if (b > 4) { break; }
    |   }
    |   a
    | }
    = 10

### Expressions ###

Precedence.

    | fun main() {
    |   2 + 3 * 4  /* not 20 */
    | }
    = 14

Unary negation.

    | fun main() {
    |   -3
    | }
    = -3

    | fun main() {
    |   2+-5
    | }
    = -3

Minus sign must be right in front of a number.

    | fun main() {
    |   -(4)
    | }
    ? Expected

Unary not.

    | fun main() {
    |   not (4 > 3)
    | }
    = False

Precedence of unary not.

    | fun main() {
    |   not true or true
    | }
    = True

    | fun main() {
    |   not 3 > 4
    | }
    = True

### Local Variables ###

Local variables.

    | fun main() {
    |   a = 6;
    |   b = 7;
    |   a + b
    | }
    = 13

Local variables can be assigned functions.

    | fun ancillary(x) { x * 2 }
    | fun main() {
    |   a = ancillary;
    |   a(7)
    | }
    = 14

Local variables can be assigned.

    | fun main() {
    |   a = 6;
    |   a = a + 12;
    |   a
    | }
    = 18

    | fun main() {
    |   a = 6;
    |   z = 99;
    |   a
    | }
    = 6

    | fun main() {
    |   z = 6;
    |   a
    | }
    ? undefined

Local variables cannot occur in expressions until they are defined by an
initial assignment.

    | fun main() {
    |   z = a * 10;
    |   a = 10;
    |   z
    | }
    ? undefined

A local variables may not be defined inside an `if` or `while` or `typecase`
block, as it might not be executed.

    | fun main() {
    |   if (4 > 5) {
    |     a = 10;
    |   } else {
    |     b = 11;
    |   }
    |   b
    | }
    ? within control

    | fun main() {
    |   b = false;
    |   while b {
    |     a = 10;
    |   }
    |   a
    | }
    ? within control

    | fun main() {
    |   a = 55 as integer|string;
    |   typecase a is string {
    |     b = 7
    |   }
    |   a
    | }
    ? within control

Assignment, though it syntactically may occur in expressions, has a type of
void, so it can only really happen at the statement level.

    | fun main() {
    |   a = 0; b = 0;
    |   a = b = 9;
    | }
    ? type mismatch

Variables in upper scopes may be modified.

    | fun main() {
    |   a = 0
    |   if 3 > 2 {
    |     a = 4;
    |   }
    |   a
    | }
    = 4

### Non-local Values ###

Literals may appear at the toplevel.  Semicolons are optional at toplevel.

    | factor = 5;
    | fun main() {
    |   6 * factor
    | }
    = 30

Toplevel literals may not be updated.  Thus, the following looks like it
is defining a local with the same name as a toplevel, which is not permitted.

    | factor = 5
    | fun main() {
    |   factor = 7
    | }
    ? shadows

Toplevel literals may be function literals (the syntax we've been using is just sugar.)

    | main = fun() {
    |   7
    | }
    = 7

Truth and falsehood are builtin toplevels.

    | fun main() {
    |   true or false
    | }
    = True

    | fun main() {
    |   false and true
    | }
    = False

So is `null`, which is the single value of `void` type.

    | fun wat(x: void) { 3 }
    | fun main() {
    |   wat(null)
    | }
    = 3

### More on Functions ###

Function arguments may not be updated.

    | fun foo(x) {
    |   x = x + 14;
    |   x
    | }
    | fun main() {
    |   foo(7)
    | }
    ? shadows

Factorial can be computed.

    | factorial : integer -> integer
    | fun factorial(a) {
    |   if a == 0 {
    |     return 1
    |   } else {
    |     return a * factorial(a - 1)
    |   }
    | }
    | fun main() {
    |   factorial(6)
    | }
    = 720

Literal functions.

    | fun main() {
    |   inc = fun(x) { x + 1 };
    |   inc(7)
    | }
    = 8

    | fun main() {
    |   fun(x){ x + 1 }(9)
    | }
    = 10

    | fun main() {
    |   a = 99;
    |   a = fun(x){ x + 1 }(9);
    |   a
    | }
    = 10

Literal functions can have local variables, loops, etc.

    | fun main() {
    |   z = 99;
    |   z = fun(x) {
    |     a = x;  b = x;
    |     while a > 0 {
    |       b = b + a; a = a - 1;
    |     }
    |     return b
    |   }(9);
    |   z
    | }
    = 54

Literal functions can define other literal functions...

    | fun main() {
    |   fun(x){ fun(y){ fun(z){ z + 1 } } }(4)(4)(10)
    | }
    = 11

Literal functions can access globals.

    | oid = 19
    | fun main() {
    |   fun(x){ x + oid }(11);
    | }
    = 30

Literal functions cannot access variables declared in enclosing scopes.

    | fun main() {
    |   oid = 19;
    |   fun(x){ x + oid }(11);
    | }
    ? undefined

Literal functions cannot access arguments declared in enclosing scopes.

    | fun main() {
    |   fun(x){ fun(y){ fun(z){ y + 1 } } }(4)(4)(10)
    | }
    ? undefined

Functions can be passed to functions and returned from functions.

    | fun doubble(x) { x * 2 }
    | fun triple(x) { x * 3 }
    | fun apply_and_add_one(f: (integer -> integer), x) { f(x) + 1 }
    | fun sellect(a) { if a > 10 { return doubble } else { return triple } }
    | fun main() {
    |   t = sellect(5);
    |   d = sellect(15);
    |   p = t(10);
    |   apply_and_add_one(d, p)
    | }
    = 61

To overcome the syntactic ambiguity with commas, function types
in function definitions must be in parens.

    | fun add(x, y) { x + y }
    | fun mul(x, y) { x * y }
    | fun do_it(f: (integer, integer -> integer), g) {
    |   f(3, g)
    | }
    | fun main() {
    |   do_it(mul, 4) - do_it(add, 4)
    | }
    = 5

`return` may be used to prematurely return a value from a function.

    | fun foo(y) {
    |   x = y
    |   while x > 0 {
    |     if x < 5 {
    |       return x;
    |     }
    |     x = x - 1;
    |   }
    |   17
    | }
    | fun main() {
    |   foo(10) + foo(0)
    | }
    = 21

Type of value returned must jibe with value of function's block.

    | fun foo(x) {
    |   return "string";
    |   17
    | }
    | fun main() {
    |   foo(10) + foo(0)
    | }
    ? type mismatch

Type of value returned must jibe with other return statements.

    | fun foo(x) {
    |   if x > 0 {
    |     return "string";
    |   } else {
    |     return 17
    |   }
    | }
    | fun main() {
    |   foo(10) + foo(0)
    | }
    ? type mismatch

### Equality ###

Equality, inequality, boolean operators.

    | fun main() {
    |   if 15 == 15 and ((15 != 14) or false) {
    |     print("struth")
    |   }
    | }
    = struth

    | fun main() {
    |   if "five" == "five" and (("six" != "seven") or false) {
    |     print("struth")
    |   }
    | }
    = struth

Equality cannot be checked between two values of different types.

    | fun main() {
    |   if 15 == "fifteen" {
    |     print("wat")
    |   }
    | }
    ? mismatch

    | fun main() {
    |   if 15 != "fifteen" {
    |     print("wat")
    |   }
    | }
    ? mismatch

Equality can be checked between unions, as long as they are
unions entirely of simple (non-struct) types.

    | fun main() {
    |   a = 40 as string|integer
    |   b = 40 as string|integer
    |   if a == b {
    |     print("it is")
    |   }
    | }
    = it is

    | fun main() {
    |   a = 40 as string|integer
    |   b = "beep" as string|integer
    |   if a != b {
    |     print("correct")
    |   }
    | }
    = correct

Equality cannot be tested between two disjoint unions.

    | fun main() {
    |   a = 40 as string|integer
    |   b = 40 as integer|void
    |   if a == b {
    |     print("correct")
    |   }
    | }
    ? mismatch

Equality cannot be tested between values of a union type
that contains a struct type as one of its members.

    | struct person { name: string; age: integer }
    | fun main() {
    |   a = 40 as person|integer
    |   b = 40 as person|integer
    |   if a == b {
    |     print("it is")
    |   }
    | }
    ? struct

### Builtins ###

The usual.

    | fun main() {
    |   print("Hello, world!")
    | }
    = Hello, world!

Some standard functions are builtin and available as toplevels.

    | fun main() {
    |   a = "hello";
    |   b = len(a);
    |   while b > 0 {
    |     print(a);
    |     b = b - 1;
    |     a = substr(a, 1, b)
    |   }
    | }
    = hello
    = ello
    = llo
    = lo
    = o

The `+` operator is not string concatenation.  `concat` is.

    | fun main() {
    |   print("hello " + "world")
    | }
    ? type mismatch

    | fun main() {
    |   print(concat("hello ", "world"))
    | }
    = hello world

The builtin toplevels are functions and functions need parens.

    | fun main() {
    |   print "hi"
    | }
    ? type mismatch

Note that the above was the motivation for requiring statements to have void
type; if non-void exprs could be used anywhere, that would just throw away
the function value `print` (b/c semicolons are optional) and return 'hi'.

### Struct Types ###

Record types.  You can define them:

    | struct person { name: string; age: integer }
    | main = fun() {}
    = 

And make them.

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   print("ok")
    | }
    = ok

And extract the fields from them.

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   print(j.name)
    |   if j.age > 20 {
    |     print("Older than twenty")
    |   } else {
    |     print("Underage")
    |   }
    | }
    = Jake
    = Older than twenty

Structs must be defined somewhere.

    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   j
    | }
    ? undefined

Structs need not be defined before use.

    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   j.age
    | }
    | struct person { name: string; age: integer }
    = 23

Structs may not contain structs which don't exist.

    | struct person { name: string; age: foobar }
    | main = fun() { 333 }
    ? undefined

Types must match when making a struct.

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(name:"Jake", age:"Old enough to know better");
    |   j.age
    | }
    ? type mismatch

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(name:"Jake");
    |   j.age
    | }
    ? argument mismatch

    | struct person { name: string }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   j.age
    | }
    ? argument mismatch

Order of field initialization when making a struct doesn't matter.

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   j.age
    | }
    = 23

Structs cannot be tested for equality with the `==` or `!==`
operators.

    | struct person { name: string; age: integer }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   k = make person(name:"Jake", age: 23);
    |   j == k
    | }
    ? structs cannot be compared

    | struct person { age: integer; name: string }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   k = make person(age: 21, name:"Jake");
    |   j != k
    | }
    ? structs cannot be compared

Structs of two different types cannot be tested for equality.

    | struct person { age: integer; name: string }
    | struct individual { age: integer; name: string }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   k = make individual(age: 23, name:"Jake");
    |   j == k
    | }
    ? mismatch

If you really want to compare two structs for equality, you'll
have to write the equality predicate function yourself.

    | struct person { name: string; age: integer }
    | equ_person = fun(a: person, b: person) {
    |   a.age == b.age and a.name == b.name
    | }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   k = make person(name:"Jake", age: 23);
    |   equ_person(j, k)
    | }
    = True

Structs cannot be compared for ordering.

    | struct person { age: integer; name: string }
    | main = fun() {
    |   j = make person(age: 23, name:"Jake");
    |   k = make person(age: 21, name:"Jake");
    |   j > k
    | }
    ? structs cannot be compared

Structs can be passed to functions.

    | struct person { name: string; age: integer }
    | fun wat(bouncer: person) { bouncer.age }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   wat(j)
    | }
    = 23

Structs have name equivalence, not structural.

    | struct person { name: string; age: integer }
    | struct city { name: string; population: integer }
    | fun wat(hometown: city) { hometown }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   wat(j)
    | }
    ? type mismatch

Struct fields must all be unique.

    | struct person { name: string; name: string }
    | main = fun() {
    |   j = make person(name:"Jake", name:"Smith");
    | }
    ? defined

Values can be retrieved from structs.

    | struct person { name: string; age: integer }
    | fun age(bouncer: person) { bouncer.age }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   age(j)
    | }
    = 23

    | struct person { name: string }
    | fun age(bouncer: person) { bouncer.age }
    | main = fun() {
    |   j = make person(name:"Jake");
    |   age(j)
    | }
    ? undefined

Different structs may have the same field name in different positions.

    | struct person { name: string; age: integer }
    | struct city { population: integer; name: string }
    | main = fun() {
    |   j = make person(name:"Jake", age:23);
    |   w = make city(population:600000, name:"Winnipeg");
    |   print(j.name)
    |   print(w.name)
    | }
    = Jake
    = Winnipeg

Can't define the same struct multiple times.

    | struct person { name: string; age: integer }
    | struct person { name: string; age: string }
    | fun main() { 333 }
    ? duplicate

Structs may refer to themselves.

    | struct recursive {
    |   next: recursive;
    | }
    | fun main() { 333 }
    = 333

    | struct odd {
    |   next: even;
    | }
    | struct even {
    |   next: odd;
    | }
    | fun main() { 333 }
    = 333

But you can't actually make one of these infinite structs.

    | struct recursive {
    |   next: recursive;
    | }
    | fun main() { make recursive(next:make recursive(next:"nooo")) }
    ? type mismatch

### Union Types ###

Values of union type are created with the type promotion operator,
`as ...`.  Type promotion has a very low precedence, and can be
applied to any expression.

The type after the `as` must be a union.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   a + b as integer
    | }
    ? bad cast

The type of the value being cast by the `as` must be one of the types in the union.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   a + b as string|void
    | }
    ? bad cast

The type after the `as` must be the type of the expression.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   c = a + b as integer|string
    |   print("ok")
    | }
    = ok

Each of the individual types named in the union type must be unique.

    | fun foo(a, b: integer|string) {
    |   print("ok")
    | }
    | fun main() {
    |   a = 20;
    |   b = 30;
    |   c = a + b as integer|integer|string
    |   foo(a, c)
    | }
    ? bad union type

One can, vacuously, promote a union type to itself.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   c = a + b as integer|string
    |   d = c as integer|string
    |   print("ok")
    | }
    = ok

One can promote a union type to another union type, so long as it is a superset.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   c = a + b as integer|string
    |   d = c as integer|string|void
    |   print("ok")
    | }
    = ok

One cannot promote a union type to a union type that is not a superset.

    | fun main() {
    |   a = 20;
    |   b = 30;
    |   c = a + b as integer|string
    |   d = c as integer|void
    |   print("ok")
    | }
    ? bad cast

Values of union type can be passed to functions.

    | fun foo(a, b: integer|string) {
    |   a + 1
    | }
    | main = fun() {
    |   a = 0;
    |   a = foo(a, 333 as integer|string);
    |   a = foo(a, "hiya" as integer|string);
    |   a
    | }
    = 2

Order of types in a union doesn't matter.

    | fun foo(a, b: integer|string) {
    |   a + 1
    | }
    | main = fun() {
    |   a = 0;
    |   a = foo(a, 333 as integer|string);
    |   a = foo(a, "hiya" as string|integer);
    |   a
    | }
    = 2

Trivial use of `typecase`.

    | main = fun() {
    |   a = 333 as integer|string;
    |   typecase a is integer {
    |     print("int")
    |   };
    | }
    = int

Inside a `typecase` the variable can be used as a value of
the determined type.

    | main = fun() {
    |   a = 333 as integer|string;
    |   typecase a is integer {
    |     print(str(a))
    |   };
    |   typecase a is string {
    |     print(a)
    |   };
    | }
    = 333

The `typecase` construct can operate on the "right" type of a union.

    | fun foo(a, b: integer|string) {
    |   r = a;
    |   typecase b is integer {
    |     r = r + b;
    |   };
    |   typecase b is string {
    |     r = r + len(b);
    |   };
    |   r
    | }
    | main = fun() {
    |   a = 0;
    |   a = foo(a, 333 as integer|string);
    |   a = foo(a, "hiya" as integer|string);
    |   a
    | }
    = 337

The expression in a `typecase` must be a variable.

    | main = fun() {
    |   a = 333 as integer|string;
    |   typecase 333 is integer {
    |     print("what?")
    |   };
    | }
    ? identifier

The expression in a `typecase` can be an argument to the function in
which the `typecase` occurs.

    | fun wat(j: integer|string) {
    |   typecase j is integer {
    |     print("integer")
    |   };
    | }
    | main = fun() {
    |   wat(444 as integer|string)
    | }
    = integer

The expression in a `typecase` cannot effectively be a global, as globals
must be literals and there is no way (right now) to make a literal of union
type.

Inside a `typecase` the variable cannot be updated.

    | main = fun() {
    |   a = 333 as integer|string;
    |   typecase a is integer {
    |     a = 700;
    |   };
    | }
    ? cannot assign

The union can include void.

    | main = fun() {
    |   j = null as void|integer;
    |   typecase j is void {
    |     print("nothing there")
    |   };
    | }
    = nothing there

### Struct Types + Union Types ###

Union types may be used to make fields of a struct "nullable", so that
you can in actuality create recursive, but finite, data structures.

    | struct list {
    |   value: string;
    |   next: list|integer;
    | }
    | main = fun() {
    |   l = make list(
    |     value: "first",
    |     next: make list(
    |       value: "second",
    |       next:0 as list|integer
    |     ) as list|integer)
    |   s = l.next
    |   typecase s is list {
    |     print(s.value)
    |   }
    | }
    = second

You may want to use helper functions to hide this ugliness.

    | struct list {
    |   value: string;
    |   next: list|void;
    | }
    | 
    | fun empty() {
    |   return null as list|void
    | }
    | 
    | fun cons(v: string, l: list|void) {
    |   make list(value:v, next:l) as list|void
    | }
    | 
    | fun nth(n, l: list|void) {
    |   u = l;
    |   v = u;
    |   k = n;
    |   while k > 1 {
    |     typecase u is void { break; }
    |     typecase u is list { v = u.next; }
    |     u = v;
    |     k = k - 1;
    |   }
    |   return u
    | }
    | 
    | main = fun() {
    |   l = cons("first", cons("second", cons("third", empty())));
    |   h = nth(2, l);
    |   typecase h is list { print(h.value); }
    | }
    = second

And in fact, you can restrict the union types to smaller sets to
better indicate the allowable types of the functions.  For example,
`cons` always returns a list, so that should be its return type,
not `list|void`.  Likewise, `nth` requires a list.  In this way we
can implement some of the "Parse, don't Validate" approach.

    | struct list {
    |   value: string;
    |   next: list|void;
    | }
    | 
    | fun cons(v: string, l: list) {
    |   make list(value:v, next:l as list|void)
    | }
    | 
    | fun singleton(v: string) {
    |   make list(value:v, next:null as list|void)
    | }
    | 
    | fun nth(n, l: list) {
    |   u = l as list|void;
    |   v = u;
    |   k = n;
    |   while k > 1 {
    |     typecase u is void { break; }
    |     typecase u is list { v = u.next; }
    |     u = v;
    |     k = k - 1;
    |   }
    |   return u
    | }
    | 
    | main = fun() {
    |   l = cons("first", cons("second", singleton("third")));
    |   h = nth(2, l);
    |   typecase h is list { print(h.value); }
    | }
    = second

Structs may be empty.

    | struct red { }
    | fun show(color: red) {
    |   print("hi")
    | }
    | main = fun() {
    |   show(make red());
    | }
    = hi

In combination with unions, this lets us create "typed enums".

    | struct red { }
    | struct green { }
    | struct blue { }
    | fun show(color: red|green|blue) {
    |   typecase color is red { print("red"); }
    |   typecase color is green { print("green"); }
    |   typecase color is blue { print("blue"); }
    | }
    | main = fun() {
    |   show(make red() as red|green|blue);
    |   show(make blue() as red|green|blue);
    | }
    = red
    = blue

### Scoped Structs ###

When a `struct` is declared, it may be associated with a set of identifiers.
Functions with these global names are the only function definitions which
can `make` such a struct, or see that it has fields; to all other functions,
these operations will not be available.  It is in this way that encapsulation
is accomplished.

    | struct list {
    |   value: string;
    |   next: list|void;
    | } for (cons, singleton, length)
    | 
    | fun cons(v: string, l: list) {
    |   make list(value:v, next:l as list|void)
    | }
    | 
    | fun singleton(v: string) {
    |   make list(value:v, next:null as list|void)
    | }
    | 
    | length : list|void -> integer
    | fun length(l: list|void) {
    |   typecase l is void { return 0 }
    |   typecase l is list { return 1 + length(l.next) }
    | }
    | 
    | fun main() {
    |   l = cons("first", cons("second", singleton("third")));
    |   print(str(length(l as list|void)));
    | }
    = 3

    | struct list {
    |   value: string;
    |   next: list|void;
    | } for (cons, singleton, length)
    | 
    | fun cons(v: string, l: list) {
    |   make list(value:v, next:l as list|void)
    | }
    | 
    | fun singleton(v: string) {
    |   make list(value:v, next:null as list|void)
    | }
    | 
    | length : list|void -> integer
    | fun length(l: list|void) {
    |   typecase l is void { return 0 }
    |   typecase l is list { return 1 + length(l.next) }
    | }
    | 
    | fun main() {
    |   l = make list(value:"first", next:null as list|void);
    |   print(str(length(l as list|void)));
    | }
    ? make

    | struct list {
    |   value: string;
    |   next: list|void;
    | } for (cons, singleton, length)
    | 
    | fun cons(v: string, l: list) {
    |   make list(value:v, next:l as list|void)
    | }
    | 
    | fun singleton(v: string) {
    |   make list(value:v, next:null as list|void)
    | }
    | 
    | fun main() {
    |   l = cons("first", cons("second", singleton("third")));
    |   print(l.value);
    | }
    ? struct

One can use this facility to implement abstract data types.

    | struct assoc {
    |   key: string;
    |   value: string;
    |   next: assoc|void;
    | } for (singleton, update, lookup, remove)
    | 
    | fun singleton(k: string, v: string) {
    |   make assoc(key:k, value:v, next:null as assoc|void)
    | }
    | 
    | fun update(k: string, v: string, a: assoc) {
    |   make assoc(key:k, value:v, next:a as assoc|void)
    | }
    | 
    | lookup : assoc, string -> string|void
    | fun lookup(a: assoc, k: string) {
    |   if a.key == k {
    |     return a.value as string|void
    |   }
    |   n = a.next
    |   typecase n is void {
    |     return null as string|void
    |   }
    |   typecase n is assoc {
    |     return lookup(n, k)
    |   }
    | }
    | 
    | fun main() {
    |   a = update("1", "first", update("2", "second", singleton("3", "third")));
    |   r = lookup(a, "2");
    |   print("um");
    |   typecase r is void { print("NOT FOUND"); }
    |   typecase r is string { print(r); }
    |   print("ya");
    | }
    = um
    = second
    = ya

This program should work even with a redundant upcast in it.

    | struct assoc {
    |   key: string;
    |   value: string;
    |   next: assoc|void;
    | } for (singleton, update, lookup, remove)
    | 
    | fun singleton(k: string, v: string) {
    |   make assoc(key:k, value:v, next:null as assoc|void)
    | }
    | 
    | fun update(k: string, v: string, a: assoc) {
    |   make assoc(key:k, value:v, next:a as assoc|void)
    | }
    | 
    | lookup : assoc, string -> string|void
    | fun lookup(a: assoc, k: string) {
    |   if a.key == k {
    |     return a.value as string|void
    |   }
    |   n = a.next
    |   typecase n is void {
    |     return null as string|void
    |   }
    |   typecase n is assoc {
    |     return lookup(n, k) as string|void
    |   }
    | }
    | 
    | fun main() {
    |   a = update("1", "first", update("2", "second", singleton("3", "third")));
    |   r = lookup(a, "2");
    |   print("um");
    |   typecase r is void { print("NOT FOUND"); }
    |   typecase r is string { print(r); }
    |   print("ya");
    | }
    = um
    = second
    = ya