-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
operators.rakutest
510 lines (433 loc) Β· 13.8 KB
/
operators.rakutest
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
use v6.e.PREVIEW;
use Test;
plan 31;
my $ast;
my $deparsed;
my @type = |<AST Str> xx *;
sub ast(RakuAST::Node:D $node --> Nil) {
$ast := $node;
$deparsed := $node.DEPARSE;
diag $deparsed.chomp;
}
subtest 'Application of an infix operator on two literals' => {
# 44 + 22
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(44),
infix => RakuAST::Infix.new('+'),
right => RakuAST::IntLiteral.new(22)
);
is-deeply $deparsed, '44 + 22', 'deparse';
is-deeply $_, 66, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form || operator works (1)' => {
# 22 || 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(22),
infix => RakuAST::Infix.new('||'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '22 || 44', 'deparse';
is-deeply $_, 22, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form || operator works (2)' => {
# 0 || 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(0),
infix => RakuAST::Infix.new('||'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '0 || 44', 'deparse';
is-deeply $_, 44, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form or operator works (1)' => {
# 22 or 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(22),
infix => RakuAST::Infix.new('or'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '22 or 44', 'deparse';
is-deeply $_, 22, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form or operator works (2)' => {
# 0 or 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(0),
infix => RakuAST::Infix.new('or'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '0 or 44', 'deparse';
is-deeply $_, 44, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form && operator works (1)' => {
# 22 && 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(22),
infix => RakuAST::Infix.new('&&'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '22 && 44', 'deparse';
is-deeply $_, 44, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form && operator works (2)' => {
# 0 && 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(0),
infix => RakuAST::Infix.new('&&'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '0 && 44', 'deparse';
is-deeply $_, 0, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form and operator works (1)' => {
# 22 and 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(22),
infix => RakuAST::Infix.new('and'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '22 and 44', 'deparse';
is-deeply $_, 44, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'The special form and operator works (2)' => {
# 0 and 44
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(0),
infix => RakuAST::Infix.new('and'),
right => RakuAST::IntLiteral.new(44)
);
is-deeply $deparsed, '0 and 44', 'deparse';
is-deeply $_, 0, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a prefix operator to a literal (1)' => {
# ?2
ast RakuAST::ApplyPrefix.new(
prefix => RakuAST::Prefix.new('?'),
operand => RakuAST::IntLiteral.new(2)
);
is-deeply $deparsed, '?2', 'deparse';
is-deeply $_, True, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a prefix operator to a literal (2)' => {
# ?0
ast RakuAST::ApplyPrefix.new(
prefix => RakuAST::Prefix.new('?'),
operand => RakuAST::IntLiteral.new(0)
);
is-deeply $deparsed, '?0', 'deparse';
is-deeply $_, False, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a positive power postfix operator' => {
# 3Β²Β²
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::IntLiteral.new(3),
postfix => RakuAST::Postfix::Power.new(22)
);
is-deeply $deparsed, '3Β²Β²', 'deparse';
is-deeply $_, 31381059609, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a negative power postfix operator' => {
# 4β»Β²
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::IntLiteral.new(4),
postfix => RakuAST::Postfix::Power.new(-2)
);
is-deeply $deparsed, '4β»Β²', 'deparse';
is-deeply $_, 0.0625, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a (user-defined) postfix operator to a literal' => {
sub postfix:<!>($n) {
[*] 1..$n
}
# 4!
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::IntLiteral.new(4),
postfix => RakuAST::Postfix.new('!'),
);
is-deeply $deparsed, '4!', 'deparse';
is-deeply $_, 24, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Basic assignment to a Scalar container' => {
my $a = 1;
# $a = 4
ast RakuAST::ApplyInfix.new(
left => RakuAST::Var::Lexical.new('$a'),
infix => RakuAST::Infix.new('='),
right => RakuAST::IntLiteral.new(4)
);
is-deeply $deparsed, '$a = 4', 'deparse';
is-deeply EVAL($ast), 4, @type[$++];
$a = 666;
is-deeply EVAL($deparsed), 4, @type[$++];
}
subtest 'Basic single-dimension array index' => {
my @a = 10..20;
# @a[5]
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('@a'),
postfix => RakuAST::Postcircumfix::ArrayIndex.new(
index => RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::IntLiteral.new(5)
)
)
)
);
is-deeply $deparsed, '@a[5]', 'deparse';
is-deeply $_, 15, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Zen array slice' => {
my @a = 10..20;
# @a[]
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('@a'),
postfix => RakuAST::Postcircumfix::ArrayIndex.new(
index => RakuAST::SemiList.new
)
);
is-deeply $deparsed, '@a[]', 'deparse';
is-deeply $_, @a, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Multi-dimensional array indexing' => {
my @a[3;3] = <a b c>, <d e f>, <g h i>;
# @a[2;1]
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('@a'),
postfix => RakuAST::Postcircumfix::ArrayIndex.new(
index => RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::IntLiteral.new(2)
),
RakuAST::Statement::Expression.new(
expression => RakuAST::IntLiteral.new(1)
)
)
)
);
is-deeply $deparsed, '@a[2; 1]', 'deparse';
is-deeply $_, 'h', @type[$++]
for EVAL($ast), EVAL($deparsed);
}
{
my %h = a => 'add', s => 'subtract';
subtest 'Basic single-dimension hash index' => {
# %h{"s")}
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::HashIndex.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::StrLiteral.new('s')
)
)
)
);
is-deeply $deparsed, '%h{"s"}', 'deparse';
is-deeply $_, 'subtract', @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Zen hash slice' => {
# %h{}
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::HashIndex.new(
RakuAST::SemiList.new
)
);
is-deeply $deparsed, '%h{}', 'deparse';
is-deeply $_, %h, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Basic literal hash index' => {
# %h<s>
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::LiteralHashIndex.new(
index => RakuAST::QuotedString.new(
segments => [RakuAST::StrLiteral.new('s')],
processors => ['words','val']
)
)
);
is-deeply $deparsed, '%h<s>', 'deparse';
is-deeply $_, 'subtract', @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Literal hash index with multiple keys' => {
# %h<s a>
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::LiteralHashIndex.new(
index => RakuAST::QuotedString.new(
segments => [RakuAST::StrLiteral.new('s a')],
processors => ['words','val']
)
)
);
is-deeply $deparsed, '%h<s a>', 'deparse';
is-deeply $_, ('subtract', 'add'), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Empty literal hash index works as zen slice' => {
# %h<>
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::LiteralHashIndex.new(
index => RakuAST::QuotedString.new(
segments => [RakuAST::StrLiteral.new('')],
processors => ['words','val']
)
)
);
is-deeply $deparsed, '%h<>', 'deparse';
is-deeply $_, %h, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
}
subtest 'Multi-dimensional hash indexing' => {
my %h = x => { :1a, :2b }, y => { :3a, :4b };
# %h{'y';'a'}
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('%h'),
postfix => RakuAST::Postcircumfix::HashIndex.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::StrLiteral.new('y')
),
RakuAST::Statement::Expression.new(
expression => RakuAST::StrLiteral.new('a')
)
)
)
);
is-deeply $deparsed, '%h{"y"; "a"}', 'deparse';
is-deeply $_, 3, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a list infix operator on three operands' => {
# 10, 11, 12
ast RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(','),
operands => (
RakuAST::IntLiteral.new(10),
RakuAST::IntLiteral.new(11),
RakuAST::IntLiteral.new(12),
)
);
is-deeply $deparsed, '10, 11, 12', 'deparse';
is-deeply $_, (10, 11, 12), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of a list infix operator on no operands' => {
# ()
ast RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(','),
operands => ()
)
)
)
);
is-deeply $deparsed, '()', 'deparse';
is-deeply $_, (), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Chaining operator has correct outcome' => {
my $x = 4;
# 5 > $x++ > 3
ast RakuAST::ApplyInfix.new(
left => RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(5),
infix => RakuAST::Infix.new('>'),
right => RakuAST::ApplyPostfix.new(
postfix => RakuAST::Postfix.new('++'),
operand => RakuAST::Var::Lexical.new('$x')
)
),
infix => RakuAST::Infix.new('>'),
right => RakuAST::IntLiteral.new(3)
);
is-deeply $deparsed, '5 > $x++ > 3', 'deparse';
is-deeply EVAL($ast), True, @type[$++];
is-deeply $x, 5;
$x = 4;
is-deeply EVAL($deparsed), True, @type[$++];
is-deeply $x, 5;
}
subtest 'Correct outcome of ternary operator' => {
# $a ?? 22 !! 33
ast RakuAST::Ternary.new(
condition => RakuAST::Var::Lexical.new('$a'),
then => RakuAST::IntLiteral.new(22),
else => RakuAST::IntLiteral.new(33)
);
is-deeply $deparsed, '$a ?? 22 !! 33', 'deparse';
my $a = 1;
is-deeply $_, 22, @type[$++]
for EVAL($ast), EVAL($deparsed);
$a = 0;
is-deeply $_, 33, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of dotty infix `.`' => {
# "foo" .uc()
ast RakuAST::ApplyDottyInfix.new(
left => RakuAST::StrLiteral.new('foo'),
infix => RakuAST::DottyInfix::Call.new(),
right => RakuAST::Call::Method.new(
name => RakuAST::Name.from-identifier('uc')
)
);
is-deeply $deparsed, '"foo" .uc()', 'deparse';
is-deeply $_, 'FOO', @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Application of dotty infix `.=` evaluates to expected value' => {
my $var = 'foo';
# $var .= tc()
ast RakuAST::ApplyDottyInfix.new(
left => RakuAST::Var::Lexical.new('$var'),
infix => RakuAST::DottyInfix::CallAssign.new(),
right => RakuAST::Call::Method.new(
name => RakuAST::Name.from-identifier('tc')
)
);
is-deeply $deparsed, '$var .= tc()', 'deparse';
is-deeply EVAL($ast), 'Foo', @type[$++];
is-deeply $var, 'Foo';
$var = 'foo';
is-deeply EVAL($deparsed), 'Foo', @type[$++];
is-deeply $var, 'Foo';
}
subtest 'Application of a function infix on two literals' => {
# 44 [&sum] 22
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(44),
infix => RakuAST::FunctionInfix.new(
RakuAST::Var::Lexical.new('&sum')
),
right => RakuAST::IntLiteral.new(22)
);
is-deeply $deparsed, '44 [&sum] 22', 'deparse';
is-deeply $_, 66, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
# vim: expandtab shiftwidth=4