Skip to content

Commit c774ec8

Browse files
committed
Update QAST::Compiler's alt handling to use LTM. The NQP it builds will actually parse enough to try an 'aa' ~~ /a|\w+/ style example and get it right! However, it chokes right after a semicolon...thus failing all the tests.
1 parent fbb1811 commit c774ec8

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/QAST/Compiler.nqp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,38 @@ class QAST::Compiler is HLL::Compiler {
101101
}
102102
}
103103

104-
method alt($node) { self.altseq($node) }
104+
method alt($node) {
105+
unless $node.name {
106+
return self.altseq($node);
107+
}
108+
109+
# Calculate all the branches to try, which populates the bstack
110+
# with the options. Then immediately fail to start iterating it.
111+
my $label_list_ops := self.post_new('Ops', :result<$P11>);
112+
$label_list_ops.push_pirop('new', '$P11', '"ResizableIntegerArray"');
113+
my $ops := self.post_new('Ops', :result(%*REG<cur>));
114+
$ops.push($label_list_ops);
115+
$ops.push_pirop('callmethod', '"!alt"', %*REG<cur>, %*REG<pos>,
116+
self.escape($node.name), $label_list_ops.result);
117+
$ops.push_pirop('goto', %*REG<fail>);
118+
119+
# Emit all the possible alternations.
120+
my $prefix := self.unique('alt') ~ '_';
121+
my $altcount := 0;
122+
my $endlabel := self.post_new('Label', :result($prefix ~ 'end'));
123+
my $iter := nqp::iterator($node.list);
124+
while $iter {
125+
my $altlabel := self.post_new('Label', :result($prefix ~ $altcount));
126+
my $apost := self.regex_post(nqp::shift($iter));
127+
$ops.push($altlabel);
128+
$ops.push($apost);
129+
$ops.push_pirop('goto', $endlabel);
130+
$label_list_ops.push_pirop('nqp_push_label', $label_list_ops.result, $altlabel.result);
131+
$altcount++;
132+
}
133+
$ops.push($endlabel);
134+
$ops;
135+
}
105136

106137
method altseq($node) {
107138
my $ops := self.post_new('Ops', :result(%*REG<cur>));

0 commit comments

Comments
 (0)