-
Notifications
You must be signed in to change notification settings - Fork 14
/
custom-ops.t
730 lines (563 loc) · 15.2 KB
/
custom-ops.t
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
use v6;
use Test;
use _007::Test;
{
my $program = q:to/./;
func infix:<n>(left, right) {
return 20;
}
say(4 * 5);
.
outputs $program, "20\n", "using an operator after defining it works";
}
{
my $program = q:to/./;
say(4 n 5);
.
parse-error $program, X::AdHoc, "infix:n should not be defined unless we define it";
}
{
my $program = q:to/./;
{
func infix:<n>(left, right) {
return 7;
}
}
say(4 n 5);
.
parse-error $program, X::AdHoc, "infix:n should not be usable outside of its scope";
}
{
my $program = q:to/./;
func infix:<+>(left, right) {
return 14;
}
say(1 + 4);
.
outputs $program, "14\n", "can override a built-in operator";
}
{
my $program = q:to/./;
func infix:<~~>(left, right) {
return "wrong";
}
func infix:<~~~>(left, right) {
return "right";
}
say(4 ~~~ 5);
.
outputs $program, "right\n", "longest token wins, not first";
}
{
my $program = q:to/./;
func infix:<***>(left, right) {
return "right";
}
func infix:<**>(left, right) {
return "wrong";
}
say(4 *** 5);
.
outputs $program, "right\n", "longest token wins, not last";
}
{
my $program = q:to/./;
func infix:<!>(left, right) {
say(left ~ " " ~ right);
}
BEGIN { "OH" ! "HAI" }
.
outputs $program, "OH HAI\n", "defined operators work from within BEGIN blocks";
}
{
my $program = q:to/./;
func infix:<*!>(left, right) {
return 10;
}
say(1 + 5 *! 5);
.
outputs $program, "11\n", "new operators bind maximally tightly";
}
{
my $program = q:to/./;
func infix:<~?>(left, right) is looser(infix:<+>) {
return 6;
}
say(1 + 9 ~? 12);
.
outputs $program, "6\n", "can specify trait to bind loose";
}
{
my $program = q:to/./;
func infix:<~?>(left, right) is tighter(infix:<+>) {
return 6;
}
say(1 + 9 ~? 12);
.
outputs $program, "7\n", "can specify trait to bind tight";
}
{
my $program = q:to/./;
func infix:<*>(left, right) {
return 18;
}
func infix:<~@>(left, right) is tighter(infix:<+>) {
return 30;
}
say(1 ~@ 2 * 9);
.
outputs $program, "30\n", "the new op is tighter than +, but not *";
}
{
my $program = q:to/./;
func infix:<!?!>(left, right) is tighter(infix:<+>) is looser(infix:<+>) {
}
.
parse-error $program, X::Trait::Conflict, "can't have both tighter and looser traits";
}
{
my $program = q:to/./;
func infix:<!?!>(left, right) is equiv(infix:<+>) is equiv(infix:<*>) {
}
.
parse-error $program, X::Trait::Duplicate, "can't use the same trait more than once";
}
{
my $program = q:to/./;
func infix:<@>(left, right) {
return "@";
}
func infix:<!>(left, right) is equiv(infix:<@>) {
return "!";
}
say(10 @ 2 ! 3);
say(30 ! 2 @ 14);
.
outputs $program, "!\n@\n", "can specify trait to bind equal";
}
{
my $program = q:to/./;
func infix:<!?!>(left, right) is tighter(infix:<+>) is equiv(infix:<+>) {
}
.
parse-error $program, X::Trait::Conflict, "can't have both tighter and equal traits";
}
{
my $program = q:to/./;
func infix:<!++>(left, right) is looser(infix:<+>) is equiv(infix:<+>) {
}
.
parse-error $program, X::Trait::Conflict, "can't have both looser and equal traits";
}
{
my $program = q:to/./;
func infix:<@>(left, right) is assoc("right") {
return "(" ~ left ~ ", " ~ right ~ ")";
}
say("A" @ "B" @ "C");
.
outputs $program, "(A, (B, C))\n", "associativity means we can control the shape of the expr tree";
}
{
my $program = q:to/./;
func infix:<%>(left, right) is assoc("left") {
return "(" ~ left ~ ", " ~ right ~ ")";
}
say("A" % "B" % "C");
.
outputs $program, "((A, B), C)\n", "left associativity can be specified";
}
{
my $program = q:to/./;
func infix:</>(left, right) {
return "(" ~ left ~ ", " ~ right ~ ")";
}
say("x" / "y" / "z");
.
outputs $program, "((x, y), z)\n", "left associativity is the default";
}
{
my $program = q:to/./;
func infix:<!>(left, right) is assoc("non") {
return "oh, James";
}
say(0 ! 7);
.
outputs $program, "oh, James\n", "can define an operator to be non-associative";
}
{
my $program = q:to/./;
func infix:<!>(left, right) is assoc("non") {
return "oh, James";
}
say(0 ! 0 ! 7);
.
parse-error $program, X::Op::Nonassociative, "a non-associative operator can't associate";
}
{
my $program = q:to/./;
func infix:<&-&>(left, right) is assoc("salamander") {
}
.
parse-error $program, X::Trait::IllegalValue, "you can't just put any old value in an assoc trait";
}
{
my $program = q:to/./;
func infix:<@>(left, right) is assoc("right") {
}
func infix:<@@>(left, right) is equiv(infix:<@>) {
return "(" ~ left ~ ", " ~ right ~ ")";
}
say("A" @@ "B" @@ "C");
.
outputs $program, "(A, (B, C))\n", "right associativity inherits through the 'is equiv' trait";
}
{
my $program = q:to/./;
func infix:<@>(left, right) is assoc("non") {
}
func infix:<@@>(left, right) is equiv(infix:<@>) {
return "(" ~ left ~ ", " ~ right ~ ")";
}
say("A" @@ "B" @@ "C");
.
parse-error $program, X::Op::Nonassociative, "non-associativity inherits through the 'is equiv' trait";
}
{
my $program = q:to/./;
func infix:<%>(left, right) is assoc("left") {
}
func infix:<%%>(left, right) is equiv(infix:<%>) is assoc("right") {
}
.
parse-error $program, X::Associativity::Conflict,
"if you're using the 'is equiv' trait, you can't contradict the associativity";
}
{
my $program = q:to/./;
func prefix:<¿>(term) {
return 42;
}
say(¿"forty-two");
.
outputs $program, "42\n", "declaring and using a prefix op works";
}
{
my $program = q:to/./;
func postfix:<!>(term) {
return 1;
}
say([]!);
.
outputs $program, "1\n", "declaring and using a postfix op works";
}
{
my $program = q:to/./;
func prefix:<¿>(term) {
return "prefix is looser";
}
func postfix:<!>(term) {
return "postfix is looser";
}
func postfix:<$>(term) {
return "postfix is looser";
}
func prefix:<%>(term) {
return "prefix is looser";
}
say(¿[]!);
say(%[]$);
.
outputs $program, "prefix is looser\n" x 2, "prefix is looser by default";
}
{
my $program = q:to/./;
func prefix:<¿>(term) {
return "prefix is looser";
}
func postfix:<!>(term) is looser(prefix:<¿>) {
return "postfix is looser";
}
func postfix:<$>(term) {
return "postfix is looser";
}
func prefix:<%>(term) is tighter(postfix:<$>) {
return "prefix is looser";
}
say(¿[]!);
say(%[]$);
.
outputs $program, "postfix is looser\n" x 2, "postfixes can be made looser with traits";
}
{
my $program = q:to/./;
func postfix:<!>(term) {
return "postfix is looser";
}
func prefix:<¿>(term) is tighter(postfix:<!>) {
return "prefix is looser";
}
func prefix:<%>(term) {
return "prefix is looser";
}
func postfix:<$>(term) is looser(prefix:<%>) {
return "postfix is looser";
}
say(¿[]!);
say(%[]$);
.
outputs $program, "postfix is looser\n" x 2, "prefixes can be made tighter with traits";
}
{
my $program = q:to/./;
func postfix:<¡>(term) is assoc("right") {
return "postfix is looser";
}
func prefix:<¿>(term) is equiv(postfix:<¡>) {
return "prefix is looser";
}
func prefix:<%>(term) is assoc("left") {
return "prefix is looser";
}
func postfix:<$>(term) is equiv(prefix:<%>) {
return "postfix is looser";
}
say(¿[]¡);
say(%[]$);
.
outputs $program, "prefix is looser\npostfix is looser\n",
"associativity works between pre- and postfixes";
}
{
my $program = q:to/./;
func prefix:<¿>(left, right) is assoc("non") {
}
func postfix:<!>(left, right) is equiv(prefix:<¿>) {
}
say(¿0!);
.
parse-error $program, X::Op::Nonassociative, "non-associativity inherits through the 'is equiv' trait";
}
{
my $program = q:to/./;
func postfix:<!>(left, right) is tighter(infix:<+>) {
}
.
parse-error $program, X::Precedence::Incompatible, "can't cross the infix/prepostfix prec barrier (I)";
}
{
my $program = q:to/./;
func infix:<!>(left, right) is tighter(prefix:<->) {
}
.
parse-error $program, X::Precedence::Incompatible, "can't cross the infix/prepostfix prec barrier (II)";
}
{
my $program = q:to/./;
func infix:«!»(l, r) {
return "Mr. Bond";
}
say(-13 ! none);
.
outputs $program, "Mr. Bond\n", "can declare an operator with infix:«...»";
}
{
my $program = q:to/./;
func infix:<\>>(l, r) {
return "James";
}
say(0 > 7);
.
outputs $program, "James\n", "can declare an operator with a backslash in the name";
}
{
my $program = q:to/./;
func postfix:<‡>(x) is looser(prefix:<^>) {
return [];
}
func prefix:<$>(x) {
return x.size();
}
say($^5‡);
.
outputs $program, "[]\n", "Prepostfix boundaries are respected";
}
{
my $program = q:to/./;
func prefix:<&>(x) {
return x ~ " prefix:<&>";
}
func postfix:<‡>(x) is looser(prefix:<&>) {
return x ~ " postfix:<‡>";
}
{
func prefix:<$>(x) {
return x ~ " prefix:<$>";
}
say($&"application order:"‡);
}
.
outputs $program, "application order: prefix:<&> prefix:<\$> postfix:<‡>\n", "Prepostfix boundaries are respected, #2";
}
{
my $program = q:to/./;
func postfix:<&>(x) {
return 1;
}
func prefix:<&>(x) {
return 2;
}
func prefix:<@>(x) {
return 3;
}
# "I'm reminded of the day my daughter came in, looked over my
# shoulder at some Perl 4 code, and said, 'What is that, swearing?'"
# -- Larry Wall, Usenet article
say(@0&);
.
outputs $program, "3\n",
"a postfix is looser than a prefix, even when it has a prefix of the same name (#190)";
}
{
my $program = q:to/./;
func infix:«->»(lhs, rhs) {
return "Bond";
}
say(1 -> 2);
.
outputs $program, "Bond\n",
"defining infix:«->» correctly installs a -> operator (#175)";
}
{
my $program = q:to/./;
my infix:<?> = func (lhs, rhs) {
return "?";
};
say(1 ? 2);
.
outputs $program, "?\n",
"installation of custom operators sits on the right peg (#173) (my)";
}
{
my $program = q:to/./;
func foo(x, y, infix:<+>) {
return x + y;
}
say(foo(1, 2, func(l, r) { return l ~ r }));
.
outputs $program, "12\n",
"installation of custom operators sits on the right peg (#173) (parameter I)";
}
{
my $program = q:to/./;
func foo(x, y, infix:<?>) {
return x ? y;
}
say(foo(1, 2, func(l, r) { return l ~ r }));
.
outputs $program, "12\n",
"installation of custom operators sits on the right peg (#173) (parameter II)";
}
{
my $program = q:to/./;
my fns = [func(l, r) { return l ~ r }, func(l, r) { return l * r }];
for fns -> infix:<op> {
say(20 op 5);
}
.
outputs $program, "205\n100\n",
"installation of custom operators sits on the right peg (#173) ('for' loop parameter)";
}
{
my $program = q:to/./;
if func(l, r) { return l * r } -> infix:<op> {
say(20 op 5);
}
.
outputs $program, "100\n",
"installation of custom operators sits on the right peg (#173) ('if' statement parameter)";
}
{
my $program = q:to/./;
my c = 5;
my d = fn;
func fn(l, r) {
c = c - 1;
if !c {
return none;
}
return fn;
}
while d -> infix:<op> {
d = 1 op 2;
say(c);
}
.
outputs $program, "4\n3\n2\n1\n0\n",
"installation of custom operators sits on the right peg (#173) ('while' statement parameter)";
}
{
my $program = q:to/./;
func infix:<@->(a, b) is looser(infix:<+>) {
if b == 0 {
return a;
}
return a-1 @- b-1;
}
say(10 @- 3);
.
outputs $program, "7\n",
"can use custom operators already inside the body of the custom operator";
}
{
my $program = q:to/./;
func postfix:<++>(x) {
x + 1
}
my y = 41;
say(y ++)
.
outputs $program, "42\n",
"can have whitespace before a postfix";
}
{
my $program = q:to/./;
say(+ 7);
.
outputs $program, "7\n",
"can have whitespace after a prefix";
}
{
my $program = q:to/./;
func infix:<+++>(lhs, rhs) {}
func postfix:<+++>(term) {}
.
parse-error $program, X::Redeclaration, "can't declare an infix and a postfix with the same name";
}
{
my $program = q:to/./;
{
func postfix:<!>(t) is assoc("right") {
"postfix:<!>(" ~ t ~ ")";
}
func prefix:<?>(t) is equiv(postfix:<!>) {
"prefix:<?>(" ~ t ~ ")";
}
say(?"term"!);
}
{
func prefix:<?>(t) is assoc("right") {
"prefix:<?>(" ~ t ~ ")";
}
func postfix:<!>(t) is equiv(prefix:<?>) {
"postfix:<!>(" ~ t ~ ")";
}
say(?"term"!);
}
.
outputs
$program,
"prefix:<?>(postfix:<!>(term))\nprefix:<?>(postfix:<!>(term))\n",
"with same-precedence right-associative prefix/postfix ops, the postfix evaluates first (no matter the order declared) (#372)";
}
done-testing;