Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 78 lines (70 sloc) 2.441 kB
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
1 class GatherIter is Iterator {
2 has Mu $!coro; # coroutine to execute for more pairs
3 has $!reified; # Parcel of this iterator's results
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
4 has $!infinite; # true if iterator is known infinite
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
5
318767a @pmichaud Fix triangle reductions.
pmichaud authored
6 method new($block, Mu :$infinite) {
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
7 my Mu $coro :=
ab1dda5 @pmichaud Convert some more pir:: opcodes to nqp:: .
pmichaud authored
8 nqp::clone(nqp::getattr(&coro, Code, '$!do'));
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
9 Q:PIR {
10 $P0 = find_lex '$block'
11 $P1 = find_lex '$coro'
12 $P1($P0)
13 };
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
14 pir::setattribute__0PPsP(
0a93f6e @jnthn Improve object creation performance in a few hot-path places (e.g. it…
jnthn authored
15 pir::setattribute__0PPsP(nqp::create(self), GatherIter, '$!coro', $coro),
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
16 GatherIter, '$!infinite', $infinite);
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
17 }
18
2630e16 @pmichaud Improve handling of .reify(*) and infinite lists.
pmichaud authored
19 method reify($n = 1) {
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
20 if !$!reified.defined {
ab1dda5 @pmichaud Convert some more pir:: opcodes to nqp:: .
pmichaud authored
21 my Mu $rpa := nqp::list();
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
22 my Mu $parcel;
f71cff1 @jnthn Avoid a more expensive ! in GatherIter; remove some Q:PIR.
jnthn authored
23 my $end = Bool::False;
2630e16 @pmichaud Improve handling of .reify(*) and infinite lists.
pmichaud authored
24 my $count = nqp::istype($n, Whatever) ?? 1 !! $n;
25 while !$end && $count > 0 {
f71cff1 @jnthn Avoid a more expensive ! in GatherIter; remove some Q:PIR.
jnthn authored
26 $parcel := $!coro();
ab1dda5 @pmichaud Convert some more pir:: opcodes to nqp:: .
pmichaud authored
27 $end = nqp::p6bool(nqp::isnull($parcel));
28 nqp::push($rpa, $parcel) unless $end;
2630e16 @pmichaud Improve handling of .reify(*) and infinite lists.
pmichaud authored
29 $count = $count - 1;
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
30 }
ab1dda5 @pmichaud Convert some more pir:: opcodes to nqp:: .
pmichaud authored
31 nqp::push($rpa,
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
32 pir::setattribute__0PPsP(
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
33 pir::setattribute__0PPsP(
0a93f6e @jnthn Improve object creation performance in a few hot-path places (e.g. it…
jnthn authored
34 nqp::create(self), GatherIter, '$!coro', $!coro),
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
35 GatherIter, '$!infinite', $!infinite))
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
36 unless $end;
e02a620 @pmichaud More pir:: -> nqp:: conversion, spectest.data updates.
pmichaud authored
37 $!reified := nqp::p6parcel($rpa, nqp::null());
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
38 }
39 $!reified
40 }
41
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
42 method infinite() { $!infinite }
43
1f6fe81 @moritz switch Numeric to sigilless params
moritz authored
44 my sub coro(\block) {
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
45 Q:PIR {
46 .local pmc block, handler, taken
1f6fe81 @moritz switch Numeric to sigilless params
moritz authored
47 block = find_lex 'block'
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
48 .yield ()
49 handler = root_new ['parrot';'ExceptionHandler']
50 handler.'handle_types'(.CONTROL_TAKE)
51 set_addr handler, take_handler
52 push_eh handler
27cef7c @pmichaud Gather needs to execute its body in sink context.
pmichaud authored
53 $P0 = block()
54 $P0.'eager'()
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
55 pop_eh
56 gather_done:
57 null taken
58 .yield (taken)
59 goto gather_done
60 take_handler:
61 .local pmc exception, resume
62 .get_results (exception)
63 taken = exception['payload']
64 resume = exception['resume']
65 .yield (taken)
66 resume()
67 goto gather_done # should never get here
63e2719 @jnthn We run into code-gen issues for routines that evaluate to something v…
jnthn authored
68 };
69 True
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
70 }
71 }
72
73
1f6fe81 @moritz switch Numeric to sigilless params
moritz authored
74 sub GATHER(\block, Mu :$infinite) {
75 GatherIter.new( block, :$infinite ).list;
03dfac0 @pmichaud Add a temporary :infinite flag to GATHER, for experimentation
pmichaud authored
76 }
65bec51 @pmichaud Add the first bit of gather/take functionality.
pmichaud authored
77
Something went wrong with that request. Please try again.