Skip to content

Commit

Permalink
Turn the regex engine into a loadable module
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 24, 2010
1 parent e71aae9 commit 41d943c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -10,6 +10,10 @@ Test.dll
Test.dll.so
Test.cs
Test_ast.store
RegexEngine.dll
RegexEngine.so
RegexEngine.cs
RegexEngine_ast.store
*.swp
*.pmc
syml
Expand Down
2 changes: 1 addition & 1 deletion CompilerDriver.pm
Expand Up @@ -13,7 +13,7 @@ open ::NIECZA_OUT, ">&", \*STDOUT;

BEGIN {
unshift @INC, 'STD_checkout';
$ENV{PERL6LIB} = "STD_checkout:STD_checkout/lib";
$ENV{PERL6LIB} = ".:STD_checkout:STD_checkout/lib";
}

use Body ();
Expand Down
43 changes: 11 additions & 32 deletions rxwork.pl → RegexEngine.pm6
@@ -1,5 +1,6 @@
my module RegexEngine;
# vim: ft=perl6
my class Cursor {
my class Cursor is export {
# has $!str
# has $!from
method str() { $!str }
Expand All @@ -10,7 +11,7 @@
# Inside a regex, a result is a bare iterator, or a double return (if
# ratcheting).

sub _rxexport($cs) {
sub _rxexport($cs) is export {
my class ExportIterator is Iterator {
# $!valid $!value $!next $!fun
method validate() {
Expand All @@ -29,7 +30,7 @@ ($cs)
@l;
}

sub _rxlazymap($cs, $sub) {
sub _rxlazymap($cs, $sub) is export {
my $k = sub { Any };
#say "in rxlazymap (1)";
sub get() {
Expand All @@ -44,7 +45,7 @@ ($cs, $sub)
}
}

sub _rxdisj($cs1, $cs2) {
sub _rxdisj($cs1, $cs2) is export {
my $k1 = $cs1;
my $k2 = $cs2;
sub {
Expand All @@ -58,7 +59,7 @@ ($cs1, $cs2)
}
}

sub _rxone($C) {
sub _rxone($C) is export {
my $k = $C;
sub {
my $x = $k;
Expand All @@ -68,36 +69,25 @@ ($C)
}
}

sub _rxupgrade($sub) {
my $k = $sub;
sub {
$k && do {
my $x = $k();
$k = Any;
$x;
}
}
}
sub _rxnone is export { Any };

my $rxnone = sub { Any };

sub _rxstar($C, $sub) {
sub _rxstar($C, $sub) is export {
#say "in rxstar recursion";
_rxdisj(_rxlazymap($sub($C), sub ($C) { _rxstar($C, $sub) }),
_rxone($C));
}

sub _rxstr($C, $str) {
sub _rxstr($C, $str) is export {
#say "_rxstr : " ~ ($C.str ~ (" @ " ~ ($C.from ~ (" ? " ~ $str))));
if $C.from + $str.chars <= $C.str.chars &&
$C.str.substr($C.from, $str.chars) eq $str {
_rxone(Cursor.RAWCREATE("str", $C.str, "from", $C.from + $str.chars));
} else {
$rxnone;
&_rxnone;
}
}

my class Regex is Sub {
my class Regex is Sub is export {
method ACCEPTS($str) {
my $i = 0;
my $win = 0;
Expand All @@ -111,14 +101,3 @@ ($C, $str)
$win;
}
}

# regex { a b* c }
#my $rx = Regex.bless(sub ($C) { _rxexport(_rxlazymap(_rxstr($C, 'a'), sub ($C) { _rxlazymap(_rxstar($C, sub ($C) { _rxstr($C, 'b') }), sub ($C) { _rxstr($C, 'c') }) })) });
my $rx = /ab*c/;

say "xaaabc" ~ ("xaaabc" ~~ $rx);
say "xbc" ~ ("xbc" ~~ $rx);
say "abbbc" ~ ("abbbc" ~~ $rx);
say "ac" ~ ("ac" ~~ $rx);
say "aabb" ~ ("aabb" ~~ $rx);
say "abx" ~ ("abx" ~~ $rx);

0 comments on commit 41d943c

Please sign in to comment.