~ubuntu-branches/ubuntu/precise/rakudo/precise

« back to all changes in this revision

Viewing changes to src/setting/Operators.pm

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-17 11:31:09 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517113109-rmfir654u1axbpt4
Tags: 0.1~2011.04-1
* New upstream release (Closes: #601862, #585762, #577502)
* New maintainer
* Switch to 3.0 (quilt) format
* Update dependencies (Closes: #584498)
* Update debian/copyright to lastest DEP5 revision
* Do not generate/install perl6 manpage (now done by the build system)
* Enable tests
* Bump Standards-Version to 3.9.2 (no changes needed)
* Do not install extra LICENSE files and duplicated docs
* Remove debian/clean (no more needed)
* Add Vcs-* fields in debian/control
* Rewrite (short) description
* Update upstream copyright years
* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# operators defined in the setting
2
 
 
3
 
multi sub infix:<...> (@lhs, @rhs) {
4
 
    if @rhs == 2 && @rhs[0] ~~ Code {
5
 
        &infix:<...>(@lhs, @rhs[0], :limit(@rhs[1]));
6
 
    } else {
7
 
        die "don't know how to handle a right-hand side of"
8
 
            ~ @rhs.perl
9
 
            ~ "in series operator";
10
 
    }
11
 
}
12
 
 
13
 
multi sub infix:<...>($lhs, @rhs) {
14
 
    my @a = $lhs;
15
 
    &infix:<...>(@a, @rhs);
16
 
}
17
 
 
18
 
multi sub infix:<...>($lhs, Whatever $) {
19
 
    die 'Sorry, lazy lists and infinite ranges are not yet implemented';
20
 
}
21
 
 
22
 
multi sub infix:<...>(@lhs, Whatever $) {
23
 
    die 'Sorry, lazy lists and infinite ranges are not yet implemented';
24
 
}
25
 
 
26
 
multi sub infix:<...>($lhs, Code $generator) {
27
 
    my @a = $lhs;
28
 
    &infix:<...>(@a, $generator);
29
 
}
30
 
 
31
 
multi sub infix:<...> (@lhs, Code $generator, :$limit) {
32
 
    my $c = $generator.count;
33
 
    if $c > @lhs {
34
 
        fail 'the closure wants more parameters than given on the LHS';
35
 
    }
36
 
    my @result = @lhs;
37
 
    my @r;
38
 
    my $argument-indexes;
39
 
    # WhateverCode objects don't have a signature yet (RT #69362),
40
 
    # and we can't simply use a try { ... } block because its result
41
 
    # throws a "Null PMC access in get_bool()" when used in boolean context.
42
 
    # we have to use an ugly special case here.
43
 
    # and we can't even used !~~ for that (RT #69364)
44
 
    if !$generator.^isa(WhateverCode) and any( $generator.signature.params>>.slurpy ) {
45
 
        $argument-indexes = 0..*-1;
46
 
    } else {
47
 
        $argument-indexes = *-$c .. *-1;
48
 
    }
49
 
 
50
 
    # XXX work around http://rt.perl.org/rt3/Ticket/Display.html?id=66824
51
 
    # this is a bit ugly.. since @a[1..1] returns a single item and not
52
 
    # an array, |@result[$one-item-range] throws the error
53
 
    # "argument doesn't array"
54
 
    my $comp;
55
 
    if defined($limit) {
56
 
        $comp = @lhs[*-1] cmp $limit;
57
 
    }
58
 
 
59
 
    while @r = $generator(|@(@result[$argument-indexes])) {
60
 
        if (defined($limit)) {
61
 
            if (@r[*-1] cmp $limit) == 0 {
62
 
                @result.push: @r;
63
 
                last;
64
 
            } elsif (@r[*-1] cmp $limit) != $comp {
65
 
                last;
66
 
            }
67
 
        }
68
 
 
69
 
        @result.push: @r;
70
 
    }
71
 
    @result;
72
 
}
73
 
 
74
 
# the magic one that handles stuff like
75
 
# 'a' ... 'z' and 'z' ... 'a'
76
 
multi sub infix:<...>($lhs, $rhs where { !($_ ~~ Code|Whatever) }) {
77
 
    gather {
78
 
        take $lhs;
79
 
        if ($lhs cmp $rhs) == 1 {
80
 
            my $x = $lhs;
81
 
            # since my $a = 'a'; $a-- gives
82
 
            # "Decrement out of range" we can't easily
83
 
            # decrement over our target, which is why the
84
 
            # case of going backwards is slighly more complicated
85
 
            # than going forward
86
 
            while (--$x cmp $rhs) == 1 {
87
 
                # need to make a fresh copy here because of RT #62178
88
 
                my $y = $x;
89
 
                take $y;
90
 
            }
91
 
            take $x if ($x cmp $rhs) == 0;
92
 
        } elsif ($lhs cmp $rhs) == -1 {
93
 
            my $x = $lhs;
94
 
            while (++$x cmp $rhs) <= 0 {
95
 
                my $y = $x;
96
 
                take $y;
97
 
            }
98
 
        }
99
 
    }
100
 
}
101
 
 
102
 
multi sub infix:<eqv> (Num $a, Num $b) { $a === $b }
103
 
multi sub infix:<eqv> (Str $a, Str $b) { $a === $b }
104
 
multi sub infix:<eqv> (Code $a, Code $b) { $a === $b }
105
 
multi sub infix:<eqv> (Bool $a, Bool $b) { $a === $b }
106
 
multi sub infix:<eqv> (Rat $a, Rat $b) {
107
 
    $a.numerator === $b.numerator && $a.denominator == $b.denominator
108
 
};
109
 
multi sub infix:<eqv> (Positional $a, Positional $b) {
110
 
    return Bool::False unless $a.WHAT === $b.WHAT;
111
 
    return Bool::False unless $a.elems == $b.elems;
112
 
    for @($a) Z @($b) -> $x, $y {
113
 
        return Bool::False unless $x eqv $y;
114
 
    }
115
 
    Bool::True
116
 
}
117
 
 
118
 
multi sub infix:<eqv>(Pair $a, Pair $b) {
119
 
    $a.key eqv $b.key && $a.value eqv $b.value;
120
 
}
121
 
 
122
 
multi sub infix:<eqv>(Mapping $a, Mapping $b) {
123
 
    return Bool::False if +$a != +$b;
124
 
    for $a.kv -> $k, $v {
125
 
        return Bool::False unless $b.exists($k);
126
 
        return Bool::False unless $b.{$k} eqv $v;
127
 
    }
128
 
    return Bool::True;
129
 
}
130
 
 
131
 
multi sub infix:<eqv>(Failure $a, Failure $b) {
132
 
    # do we have different values of undef yet?
133
 
    # if so, how do I detect them?
134
 
    Bool::True;
135
 
}
136
 
 
137
 
multi sub infix:<eqv> ($a, $b) {
138
 
    return Bool::False unless $a.WHAT === $b.WHAT;
139
 
    return Bool::True  if     $a      === $b;
140
 
    die "infix:<eqv> is only implemented for certain special cases yet."
141
 
        ~"\n You tried to compare two objects of type " ~ $a.WHAT.perl;
142
 
}
143
 
 
144
 
multi sub infix:<minmax>(@a, @b) {
145
 
    (@a[0] min @b[0], @a[1] max @b[1]);
146
 
}
147
 
 
148
 
multi sub infix:<leg>($a, $b) {
149
 
    ~$a cmp ~$b;
150
 
}
151
 
 
152
 
sub prefix:<[//]>(*@a) {
153
 
    for @a -> $item {
154
 
        $item // next;
155
 
        return $item;
156
 
    }
157
 
    return ();
158
 
}
159
 
 
160
 
sub prefix:<[||]>(*@a) {
161
 
    for @a -> $item {
162
 
        $item || next;
163
 
        return $item;
164
 
    }
165
 
    return ();
166
 
}
167
 
 
168
 
multi sub infix:<!%>($a, $b) { ! ($a % $b) }
169
 
 
170
 
 
171
 
multi sub infix:<+>($a, $b) {
172
 
    Q:PIR {
173
 
        $P0 = find_lex '$a'
174
 
        $N0 = $P0
175
 
        $P1 = find_lex '$b'
176
 
        $N1 = $P1
177
 
        $N2 = $N0 + $N1
178
 
        %r = box $N2
179
 
    }
180
 
}
181
 
 
182
 
multi sub infix:<->($a, $b) {
183
 
    Q:PIR {
184
 
        $P0 = find_lex '$a'
185
 
        $N0 = $P0
186
 
        $P1 = find_lex '$b'
187
 
        $N1 = $P1
188
 
        $N2 = $N0 - $N1
189
 
        %r = box $N2
190
 
    }
191
 
}
192
 
 
193
 
multi sub infix:<*>($a, $b) {
194
 
    Q:PIR {
195
 
        $P0 = find_lex '$a'
196
 
        $N0 = $P0
197
 
        $P1 = find_lex '$b'
198
 
        $N1 = $P1
199
 
        $N2 = $N0 * $N1
200
 
        %r = box $N2
201
 
    }
202
 
}
203
 
 
204
 
multi sub infix:</>($a, $b) {
205
 
    Q:PIR {
206
 
        $P0 = find_lex '$a'
207
 
        $N0 = $P0
208
 
        $P1 = find_lex '$b'
209
 
        $N1 = $P1
210
 
        $N2 = $N0 / $N1
211
 
        %r = box $N2
212
 
    }
213
 
}
214
 
 
215
 
multi sub infix:<%>($a, $b) {
216
 
    Q:PIR {
217
 
        $P0 = find_lex '$a'
218
 
        $N0 = $P0
219
 
        $P1 = find_lex '$b'
220
 
        $N1 = $P1
221
 
        $N2 = mod $N0, $N1
222
 
        %r = box $N2
223
 
    }
224
 
}
225
 
 
226
 
multi sub infix:<**>($a, $b) {
227
 
    Q:PIR {
228
 
        $P0 = find_lex '$a'
229
 
        $N0 = $P0
230
 
        $P1 = find_lex '$b'
231
 
        $N1 = $P1
232
 
        $N2 = pow $N0, $N1
233
 
        %r = box $N2
234
 
    }
235
 
}
236
 
 
237
 
multi sub prefix:<->($a) {
238
 
    Q:PIR {
239
 
        $P0 = find_lex '$a'
240
 
        $N0 = $P0
241
 
        $N0 = neg $N0
242
 
        %r = box $N0
243
 
    }
244
 
}
245
 
 
246
 
 
247
 
multi sub prefix:<~>(Object $a) {
248
 
    Q:PIR {
249
 
        $P0 = find_lex '$a'
250
 
        $S0 = $P0
251
 
        %r = new ['Str']
252
 
        assign %r, $S0
253
 
    }
254
 
}
255
 
 
256
 
 
257
 
multi sub prefix:<~>(Multi $a) { $a.name }
258
 
 
259
 
multi sub infix:<!=>($a, $b)  { !($a == $b) }
260
 
multi sub infix:<!==>($a, $b) { !($a == $b) }
261
 
multi sub infix:<ne>($a, $b)  { !($a eq $b) }
262
 
multi sub infix:<!eq>($a, $b) { !($a eq $b) }
263
 
 
264
 
multi sub infix:<< < >>($a, $b) {
265
 
    ? Q:PIR {
266
 
        $P0 = find_lex '$a'
267
 
        $N0 = $P0
268
 
        $P1 = find_lex '$b'
269
 
        $N1 = $P1
270
 
        $I0 = islt $N0, $N1
271
 
        %r = box $I0
272
 
    }
273
 
}
274
 
 
275
 
multi sub infix:<< > >>($a, $b) {
276
 
    ? Q:PIR {
277
 
        $P0 = find_lex '$a'
278
 
        $N0 = $P0
279
 
        $P1 = find_lex '$b'
280
 
        $N1 = $P1
281
 
        $I0 = isgt $N0, $N1
282
 
        %r = box $I0
283
 
    }
284
 
}
285
 
 
286
 
multi sub infix:<< <= >>($a, $b) {
287
 
    ? Q:PIR {
288
 
        $P0 = find_lex '$a'
289
 
        $N0 = $P0
290
 
        $P1 = find_lex '$b'
291
 
        $N1 = $P1
292
 
        $I0 = isle $N0, $N1
293
 
        %r = box $I0
294
 
    }
295
 
}
296
 
 
297
 
multi sub infix:<< >= >>($a, $b) {
298
 
    ? Q:PIR {
299
 
        $P0 = find_lex '$a'
300
 
        $N0 = $P0
301
 
        $P1 = find_lex '$b'
302
 
        $N1 = $P1
303
 
        $I0 = isge $N0, $N1
304
 
        %r = box $I0
305
 
    }
306
 
}
307
 
 
308
 
multi sub infix:<< < >>(Whatever $a, $b) {
309
 
    Q:PIR {
310
 
        $P0 = find_lex '$a'
311
 
        $P1 = find_lex '$b'
312
 
        .tailcall 'WhateverCodeX'('infix:<', $P0, $P1)
313
 
    }
314
 
}
315
 
 
316
 
multi sub infix:<< < >>($a, Whatever $b) {
317
 
    Q:PIR {
318
 
        $P0 = find_lex '$a'
319
 
        $P1 = find_lex '$b'
320
 
        .tailcall 'WhateverCodeX'('infix:<', $P0, $P1)
321
 
    }
322
 
}
323
 
 
324
 
multi sub infix:<< > >>(Whatever $a, $b) {
325
 
    Q:PIR {
326
 
        $P0 = find_lex '$a'
327
 
        $P1 = find_lex '$b'
328
 
        .tailcall 'WhateverCodeX'('infix:>', $P0, $P1)
329
 
    }
330
 
}
331
 
 
332
 
multi sub infix:<< > >>($a, Whatever $b) {
333
 
    Q:PIR {
334
 
        $P0 = find_lex '$a'
335
 
        $P1 = find_lex '$b'
336
 
        .tailcall 'WhateverCodeX'('infix:>', $P0, $P1)
337
 
    }
338
 
}
339
 
 
340
 
multi sub infix:<< <= >>(Whatever $a, $b) {
341
 
    Q:PIR {
342
 
        $P0 = find_lex '$a'
343
 
        $P1 = find_lex '$b'
344
 
        .tailcall 'WhateverCodeX'('infix:<=', $P0, $P1)
345
 
    }
346
 
}
347
 
 
348
 
multi sub infix:<< <= >>($a, Whatever $b) {
349
 
    Q:PIR {
350
 
        $P0 = find_lex '$a'
351
 
        $P1 = find_lex '$b'
352
 
        .tailcall 'WhateverCodeX'('infix:<=', $P0, $P1)
353
 
    }
354
 
}
355
 
 
356
 
multi sub infix:<< >= >>(Whatever $a, $b) {
357
 
    Q:PIR {
358
 
        $P0 = find_lex '$a'
359
 
        $P1 = find_lex '$b'
360
 
        .tailcall 'WhateverCodeX'('infix:>=', $P0, $P1)
361
 
    }
362
 
}
363
 
 
364
 
multi sub infix:<< >= >>($a, Whatever $b) {
365
 
    Q:PIR {
366
 
        $P0 = find_lex '$a'
367
 
        $P1 = find_lex '$b'
368
 
        .tailcall 'WhateverCodeX'('infix:>=', $P0, $P1)
369
 
    }
370
 
}
371
 
 
372
 
# vim: ft=perl6