[svn:parrot] r41578 - in branches/pct-rx: compilers/pct/src/PAST examples/regex
pmichaud at svn.parrot.org
pmichaud at svn.parrot.org
Wed Sep 30 14:45:24 UTC 2009
Author: pmichaud
Date: Wed Sep 30 14:45:23 2009
New Revision: 41578
URL: https://trac.parrot.org/parrot/changeset/41578
Log:
[pct-rx]: Add 'alt_longest' node type, peeks for it.
Modified:
branches/pct-rx/compilers/pct/src/PAST/Compiler-Regex.pir
branches/pct-rx/compilers/pct/src/PAST/Regex.pir
branches/pct-rx/examples/regex/05-alt-past.nqp
Modified: branches/pct-rx/compilers/pct/src/PAST/Compiler-Regex.pir
==============================================================================
--- branches/pct-rx/compilers/pct/src/PAST/Compiler-Regex.pir Wed Sep 30 14:19:01 2009 (r41577)
+++ branches/pct-rx/compilers/pct/src/PAST/Compiler-Regex.pir Wed Sep 30 14:45:23 2009 (r41578)
@@ -234,6 +234,19 @@
.end
+=item alt_longest(PAST::Regex node)
+
+Same as 'alt' above, but use declarative/LTM semantics.
+(Currently we cheat and just use 'alt' above.)
+
+=cut
+
+.sub 'alt_longest' :method
+ .param pmc node
+ .tailcall self.'alt'(node)
+.end
+
+
=item concat(PAST::Regex node)
Handle a concatenation of regexes.
Modified: branches/pct-rx/compilers/pct/src/PAST/Regex.pir
==============================================================================
--- branches/pct-rx/compilers/pct/src/PAST/Regex.pir Wed Sep 30 14:19:01 2009 (r41577)
+++ branches/pct-rx/compilers/pct/src/PAST/Regex.pir Wed Sep 30 14:45:23 2009 (r41578)
@@ -49,18 +49,22 @@
=cut
.sub 'peek' :method
+ .local pmc list
+ list = new ['ResizablePMCArray']
+
$I0 = isa self, ['PAST';'Regex']
unless $I0 goto peek_stop
- .local pmc child_it, list
+ .local pmc child_it
child_it = self.'iterator'()
- list = new ['ResizablePMCArray']
.local string pasttype
pasttype = self.'pasttype'()
- if pasttype == 'literal' goto literal
if pasttype == 'concat' goto concat
if pasttype == '' goto concat
+ if pasttype == 'literal' goto literal
+ if pasttype == 'alt' goto alt
+ if pasttype == 'alt_longest' goto alt_longest
peek_stop:
list = 0
@@ -71,6 +75,23 @@
list[0] = ''
.return (list)
+ # temporal alternation returns the prefixes of its first child
+ alt:
+ unless child_it goto peek_stop
+ $P0 = shift child_it
+ .tailcall 'peek'($P0)
+
+ # declarative alternation returns prefixes of all children
+ alt_longest:
+ unless child_it goto alt_longest_done
+ $P0 = shift child_it
+ $P1 = 'peek'($P0)
+ $I0 = elements list
+ splice list, $P1, $I0, 0
+ goto alt_longest
+ alt_longest_done:
+ .return (list)
+
concat:
unless child_it goto peek_zero
$P0 = shift child_it
Modified: branches/pct-rx/examples/regex/05-alt-past.nqp
==============================================================================
--- branches/pct-rx/examples/regex/05-alt-past.nqp Wed Sep 30 14:19:01 2009 (r41577)
+++ branches/pct-rx/examples/regex/05-alt-past.nqp Wed Sep 30 14:45:23 2009 (r41578)
@@ -1,7 +1,7 @@
# nqp
-say("# regex { 'foo' | 'bar' | 'baz' }");
+say("# regex { 'foo' || 'bar' || 'baz' }");
my $past :=
PAST::Regex.new(
PAST::Regex.new(
@@ -14,4 +14,21 @@
);
say(PAST::Compiler.compile($past, :target('pir')));
+for $past.peek { say($_); }
+
+say("# regex { ['foo' | 'bar' | 'baz'] 'xyz' }");
+my $past :=
+ PAST::Regex.new(
+ PAST::Regex.new(
+ PAST::Regex.new('foo', :pasttype('literal')),
+ PAST::Regex.new('bar', :pasttype('literal')),
+ PAST::Regex.new('baz', :pasttype('literal')),
+ :pasttype('alt_longest')
+ ),
+ PAST::Regex.new('xyz', :pasttype('literal')),
+ PAST::Regex.new(:pasttype('pass')),
+ );
+
+say(PAST::Compiler.compile($past, :target('pir')));
+for $past.peek { say($_); }
More information about the parrot-commits
mailing list