Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Decouple LAD optimization from RxOp
  • Loading branch information
sorear committed Sep 13, 2010
1 parent 94cf523 commit 1bf77fa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
26 changes: 15 additions & 11 deletions lib/Cursor.cs
Expand Up @@ -634,29 +634,33 @@ public class LADPlus : LAD {
}

public class LADSequence : LAD {
public readonly LAD fst;
public readonly LAD snd;
public LADSequence(LAD fst, LAD snd) { this.fst = fst; this.snd = snd; }
public readonly LAD[] args;
public LADSequence(LAD[] args) { this.args = args; }

public override void QueryLiteral(NFA pad, out int len, out bool cont) {
fst.QueryLiteral(pad, out len, out cont);
if (cont) {
int i = 0;
len = 0; cont = true;
while (cont && i < args.Length) {
int l1 = len;
snd.QueryLiteral(pad, out len, out cont);
args[i].QueryLiteral(pad, out len, out cont);
len += l1;
i++;
}
}

public override void ToNFA(NFA pad, int from, int to) {
int knot = pad.AddNode();
fst.ToNFA(pad, from, knot);
snd.ToNFA(pad, knot, to);
for (int i = 0; i < args.Length; i++) {
int knot = (i == args.Length - 1) ? to : pad.AddNode();
args[i].ToNFA(pad, from, knot);
from = knot;
}
if (from != to) pad.AddEdge(from, to, null);
}

public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "seq:");
fst.Dump(indent + 4);
snd.Dump(indent + 4);
foreach (LAD l in args)
l.Dump(indent + 4);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CgOp.pm
Expand Up @@ -690,7 +690,7 @@ use warnings;
$body->to_cgop,
(!$body->ltm ? () : (
CgOp::Primitive->new(op => [ 'set_ltm', $body->csname ],
zyg => [ $body->ltm ]))),
zyg => [ RxOp::lad2cgop($body->ltm) ]))),
CgOp::Primitive->new(op => [ 'close_sub', $body, ($body->class ne 'Sub') ], zyg => ($body->class eq 'Sub' ? [] : [ fetch(scopedlex($body->class)) ])))
}

Expand Down
3 changes: 2 additions & 1 deletion src/Niecza/Actions.pm
Expand Up @@ -373,13 +373,14 @@ sub regex_def { my ($cl, $M) = @_;
}

local $::symtext = $symtext;
my $lad = Optimizer::RxSimple::run_lad($ast->lad);
$ast = Optimizer::RxSimple::run($ast);
$M->{_ast} = Op::SubDef->new(
var => $var,
method_too => ($scope eq 'has' ? $name : undef),
proto_too => ($scope eq 'has' ? $unsymtext : undef),
body => Body->new(
ltm => $ast->lad,
ltm => $lad,
class => 'Regex',
type => 'regex',
signature => $sig->for_regex,
Expand Down
11 changes: 11 additions & 0 deletions src/Optimizer/RxSimple.pm
Expand Up @@ -8,6 +8,10 @@ sub run {
$_[0]->rxsimp(0);
}

sub run_lad {
$_[0];
}

# XXX should use a multi sub.
sub RxOp::rxsimp { my ($self, $cut) = @_;
my $selfp = bless { %$self }, ref($self);
Expand Down Expand Up @@ -38,6 +42,13 @@ sub RxOp::Sequence::mayback { my ($self) = @_;
return 0;
}

sub RxOp::Alt::rxsimp { my ($self, $cut) = @_;
my @kids = map { $_->rxsimp($cut) } @{ $self->zyg };
return RxOp::Alt->new(
lads => [ map { Optimizer::RxSimple::run_lad($_) } @{ $self->lads } ],
zyg => \@kids);
}

sub RxOp::Cut::rxsimp { my ($self, $cut) = @_;
my $kid = $self->zyg->[0]->rxsimp(1);
return $kid unless $kid->mayback;
Expand Down
67 changes: 40 additions & 27 deletions src/RxOp.pm
Expand Up @@ -28,6 +28,23 @@ use CgOp;
my $nlabel = 0;
sub label { "b" . ($nlabel++) }

sub lad2cgop {
my ($l) = @_;
my $r = ref $l;
if (!$r) {
return CgOp::clr_string($l);
} elsif ($r eq 'ARRAY') {
if (!@$l || ref ($l->[0])) {
return CgOp::rawnewarr('LAD', map { lad2cgop($_) } @$l);
} else {
my ($h,@r) = @$l;
return CgOp::rawnew("LAD$h", map { lad2cgop($_) } @r);
}
} else {
return $l;
}
}

__PACKAGE__->meta->make_immutable;
no Moose;
}
Expand All @@ -51,7 +68,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADStr', CgOp::clr_string($self->text));
[ 'Str', $self->text ];
}

__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -98,17 +115,17 @@ use CgOp;

sub lad {
my ($self) = @_;
if ($self->minimal) { return CgOp::rawnew('LADImp'); }
if ($self->minimal) { return [ 'Imp' ]; }
my ($mi,$ma) = ($self->min, $self->max // -1);
my $str;
if ($mi == 0 && $ma == -1) { $str = 'Star' }
if ($mi == 1 && $ma == -1) { $str = 'Plus' }
if ($mi == 0 && $ma == 1) { $str = 'Opt' }

if ($str) {
CgOp::rawnew("LAD$str", $self->zyg->[0]->lad);
[ $str, $self->zyg->[0]->lad ];
} else {
CgOp::rawnew("LADImp");
[ 'Imp' ];
}
}

Expand All @@ -132,11 +149,7 @@ use CgOp;
sub lad {
my ($self) = @_;
my @z = map { $_->lad } @{ $self->zyg };
while (@z >= 2) {
my $x = pop @z;
$z[-1] = CgOp::rawnew('LADSequence', $z[-1], $x);
}
$z[0] // CgOp::rawnew('LADNull');
[ 'Sequence', \@z ];
}


Expand Down Expand Up @@ -170,7 +183,7 @@ use CgOp;
@code;
}

sub lad { CgOp::rawnew('LADImp') }
sub lad { [ 'Imp' ] }

__PACKAGE__->meta->make_immutable;
no Moose;
Expand Down Expand Up @@ -255,8 +268,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADSequence', $self->zyg->[0]->lad,
CgOp::rawnew('LADImp'));
[ 'Sequence', [ $self->zyg->[0]->lad, [ 'Imp' ] ] ];
}

__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -285,7 +297,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADNull');
[ 'Null' ];
}

__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -376,7 +388,7 @@ use CgOp;
if (my $true = $self->true) {
return $true->lad;
}
CgOp::rawnew('LADMethod', CgOp::clr_string($self->name));
[ 'Method', $self->name ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -397,7 +409,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADImp');
[ 'Imp' ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -417,7 +429,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADImp'); #special case
[ 'Imp' ]; #special case
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -436,7 +448,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADNull');
[ 'Null' ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -459,6 +471,9 @@ use CgOp;
\%used;
}

has lads => (is => 'ro', isa => 'ArrayRef', lazy => 1,
default => sub { [ map { $_->lad } @{ $_[0]->zyg } ] });

sub code {
my ($self, $body) = @_;
my @ls = map { $self->label } @{ $self->zyg };
Expand All @@ -468,8 +483,7 @@ use CgOp;
push @code, CgOp::rawcall(CgOp::rxframe, "LTMPushAlts",
CgOp::rawscall('Lexer.GetLexer',
CgOp::rawcall(CgOp::rxframe, 'MakeCursor'),
CgOp::const(CgOp::rawnewarr('LAD',
map { $_->lad } @{ $self->zyg })),
CgOp::const(RxOp::lad2cgop($self->lads)),
CgOp::clr_string('')),
CgOp::const(CgOp::rawnewarr('Int32', map { CgOp::labelid($_) } @ls)));
push @code, CgOp::goto('backtrack');
Expand All @@ -485,8 +499,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADAny', CgOp::rawnewarr('LAD',
map { $_->lad } @{ $self->zyg }));
[ 'Any', [ map { $_->lad } @{ $self->zyg } ] ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -509,7 +522,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADImp');
[ 'Imp' ];
}

__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -563,8 +576,8 @@ use CgOp;

sub lad {
my ($self) = @_;
$self->cutltm ? CgOp::rawnew('LADImp') :
CgOp::rawnew('LADProtoRegex', CgOp::clr_string($self->name));
$self->cutltm ? [ 'Imp' ] :
[ 'ProtoRegex', $self->name ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -583,7 +596,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADDot');
[ 'Dot' ];
}

__PACKAGE__->meta->make_immutable;
Expand Down Expand Up @@ -612,7 +625,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADCC', $self->ccop);
[ 'CC', $self->ccop ];
}

__PACKAGE__->meta->make_immutable;
Expand All @@ -631,7 +644,7 @@ use CgOp;

sub lad {
my ($self) = @_;
CgOp::rawnew('LADNone');
[ 'None' ];
}

__PACKAGE__->meta->make_immutable;
Expand Down

0 comments on commit 1bf77fa

Please sign in to comment.