-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
meta-operators.rakutest
225 lines (200 loc) Β· 6.49 KB
/
meta-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
use v6.e.PREVIEW;
use Test;
plan 8;
my $ast;
my $deparsed;
my $raku;
my @type = <AST Str Raku>;
sub ast(RakuAST::Node:D $node --> Nil) {
$ast := $node;
$deparsed := $node.DEPARSE;
$raku := 'use experimental :rakuast; ' ~ $node.raku;
diag $deparsed.chomp;
}
subtest 'Assignment meta-op evaluates to expected value' => {
my $a = 10;
# $a += 3
ast RakuAST::ApplyInfix.new(
left => RakuAST::Var::Lexical.new('$a'),
infix => RakuAST::MetaInfix::Assign.new(RakuAST::Infix.new('+')),
right => RakuAST::IntLiteral.new(3)
);
is-deeply $deparsed, '$a += 3', 'deparse';
my $expected = 13;
for 'AST', $ast, 'Str', $deparsed, 'Raku', EVAL($raku) -> $type, $it {
is-deeply EVAL($it), $expected, "$type: evaluates to expected value";
is-deeply $a, $expected, "$type: Really did mutate the variable";
$expected += 3;
}
}
subtest 'Reverse meta-op evaluates to expected value' => {
# 42 R- 666
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(42),
infix => RakuAST::MetaInfix::Reverse.new(
RakuAST::Infix.new("-")
),
right => RakuAST::IntLiteral.new(666)
);
is-deeply $deparsed, '42 R- 666', 'deparse';
is-deeply $_, 624, @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
subtest 'Cross meta-op evaluates to expected value' => {
# (1, 2) X+ (1, 3)
ast RakuAST::ApplyListInfix.new(
infix => RakuAST::MetaInfix::Cross.new(
RakuAST::Infix.new("+")
),
operands => (
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(2),
)
)
)
)
),
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(3),
)
)
)
)
),
)
);
is-deeply $deparsed, '(1, 2) X+ (1, 3)', 'deparse';
is-deeply $_, (2,4,3,5), @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
subtest 'Zip meta-op evaluates to expected value' => {
# (1, 2) Z== (1, 3)
ast RakuAST::ApplyListInfix.new(
infix => RakuAST::MetaInfix::Zip.new(
RakuAST::Infix.new("==")
),
operands => (
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(2),
)
)
)
)
),
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(3),
)
)
)
)
),
)
);
is-deeply $deparsed, '(1, 2) Z== (1, 3)', 'deparse';
is-deeply $_, (True, False), @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
subtest 'Assignment meta-op with short-circuit || evaluates to true LHS' => {
my $test = 10;
my $update = 2;
# $test ||= $update++
ast RakuAST::ApplyInfix.new(
left => RakuAST::Var::Lexical.new('$test'),
infix => RakuAST::MetaInfix::Assign.new(
RakuAST::Infix.new('||')
),
right => RakuAST::ApplyPostfix.new(
operand => RakuAST::Var::Lexical.new('$update'),
postfix => RakuAST::Postfix.new('++')
)
);
is-deeply $deparsed, '$test ||= $update++', 'deparse';
my $test-expected = 10;
my $update-expected = 2;
for 'AST', $ast, 'Str', $deparsed, 'Raku', EVAL($raku) -> $type, $it {
is-deeply EVAL($it), $test-expected,
"$type: short-circuit || evaluates to true LHS";
is-deeply $update, $update-expected,
"$type: Really did short-circuit, and not evaluate RHS";
}
is-deeply EVAL($deparsed), 10,
'Str: short-circuit || evaluates to true LHS';
is-deeply $update, 2,
'Str: Really did short-circuit, and not evaluate RHS';
$test = 0;
for 'AST', $ast, 'Str', $deparsed, 'Raku', $raku -> $type, $it {
is-deeply EVAL($ast), 2,
"$type: no short-circuit || evaluates to RHS when LHS false";
is-deeply $update, 3,
"$type: Really did evaluate RHS";
}
}
subtest 'Literal reduce meta-op on left associative operator' => {
# [+] 1, 2, 3
ast RakuAST::Term::Reduce.new(
infix => RakuAST::Infix.new('+'),
args => RakuAST::ArgList.new(
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(2),
RakuAST::IntLiteral.new(3)
)
);
is-deeply $deparsed, '[+] 1, 2, 3', 'deparse';
is-deeply $_, 6, @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
{
my @a = 1,2,3,4;
subtest 'Array reduce meta-op on left associative operator' => {
# [+] @a
ast RakuAST::Term::Reduce.new(
infix => RakuAST::Infix.new('+'),
args => RakuAST::ArgList.new(
RakuAST::Var::Lexical.new('@a')
)
);
is-deeply $deparsed, '[+] @a', 'deparse';
is-deeply $_, 10, @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
}
subtest 'Triangle reduce meta-op on left associative operator' => {
# [\+] 1, 2, 3
ast RakuAST::Term::Reduce.new(
infix => RakuAST::Infix.new('+'),
args => RakuAST::ArgList.new(
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(2),
RakuAST::IntLiteral.new(3)
),
triangle => True
);
is-deeply $deparsed, '[\+] 1, 2, 3', 'deparse';
is-deeply $_, (1,3,6), @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}
# vim: expandtab shiftwidth=4