Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify handling of alternation metadata
  • Loading branch information
sorear committed May 23, 2011
1 parent 77f924c commit f3e0436
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/CLRBackend.cs
Expand Up @@ -3246,8 +3246,6 @@ class NamProcessor {
} else {
Console.WriteLine("odd constant box {0}/{1}", typ, chchh);
}
} else if (chh == "label_table" || chh == "ladconstruct") {
// probably not worth trying to coalesce
} else if (chh == "newcc") {
StringBuilder sb = new StringBuilder("C");
for (int i = 1; i < ch.Length; i++) {
Expand Down Expand Up @@ -3423,8 +3421,14 @@ class NamProcessor {
return CpsOp.MethodCall(Tokens.RxFrame_PushBacktrack,
CpsOp.GetField(Tokens.Frame_rx, CpsOp.CallFrame()),
CpsOp.LabelId(th.cpb.cx, JScalar.S(z[2]))); };
handlers["label_table"] = delegate(NamProcessor th, object[] z) {
return CpsOp.LabelTable(th.cpb.cx, JScalar.SA(1,z)); };
handlers["ltm_push_alts"] = delegate(NamProcessor th, object[] z) {
CpsOp ai = CpsOp.ConstructorCall(typeof(AltInfo).GetConstructor(new Type[] { typeof(LAD[]), typeof(string), typeof(int[]) }),
th.ProcessLADArr(z[1]),
CpsOp.StringLiteral(JScalar.S(z[2])),
CpsOp.LabelTable(th.cpb.cx, JScalar.SA(0,z[3])));
return CpsOp.MethodCall(Tokens.RxFrame.GetMethod("LTMPushAlts"),
CpsOp.GetField(Tokens.Frame_rx, CpsOp.CallFrame()),
CpsOp.CallFrame(), th.Constant(ai)); };
thandlers["popcut"] = RxCall(null, "PopCutGroup");
thandlers["rxend"] = RxCall(Tokens.Void, "End");
thandlers["rxfinalend"] = RxCall(Tokens.Void, "FinalEnd");
Expand All @@ -3439,10 +3443,7 @@ class NamProcessor {
thandlers["rxsetcapsfrom"] = RxCall(null, "SetCapturesFrom");
thandlers["rxgetpos"] = RxCall(null, "GetPos");
thandlers["rxcommitgroup"] = RxCall(null, "CommitGroup");
thandlers["get_lexer"] = Methody(null, typeof(Lexer).GetMethod("GetLexer"));
thandlers["run_dispatch"] = Methody(null, typeof(Lexer).GetMethod("RunDispatch"));
handlers["ladconstruct"] = delegate(NamProcessor th, object[] z) {
return th.ProcessLADArr(z[1]); };
handlers["rawcall"] = delegate(NamProcessor th, object[] z) {
string name = JScalar.S(z[1]);
CpsOp[] rst = JScalar.A<CpsOp>(2, z, th.Scan);
Expand Down
18 changes: 16 additions & 2 deletions lib/Cursor.cs
Expand Up @@ -119,6 +119,19 @@ public sealed class Choice {
}
}

public class AltInfo {
public readonly int[] labels;
public readonly LAD[] prefixes;
public readonly string dba;
//public readonly STable in_class;

public AltInfo(LAD[] prefixes, string dba, int[] labels) {
this.labels = labels;
this.prefixes = prefixes;
this.dba = dba;
}
}

// extends Frame for a time/space tradeoff
// we keep the cursor in exploded form to avoid creating lots and lots of
// cursor objects
Expand Down Expand Up @@ -365,13 +378,14 @@ public sealed class RxFrame {
return (i >= min);
}

public void LTMPushAlts(Lexer lx, int[] addrs) {
public void LTMPushAlts(Frame fr, AltInfo ai) {
Lexer lx = Lexer.GetLexer(fr, GetClass(), ai.prefixes, ai.dba);
if (st.pos > global.highwater)
global.IncHighwater(st.pos);
PushCutGroup("LTM");
int[] cases = lx.Run(global.orig_s, st.pos);
for (int i = cases.Length - 1; i >= 0; i--) {
PushBacktrack(addrs[cases[i]]);
PushBacktrack(ai.labels[cases[i]]);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/niecza
Expand Up @@ -48,6 +48,30 @@ method number($/) {
}
}

augment class RxOp::Alt { #OK exist
method code($body) {
my @ls = map { self.label }, @$.zyg;
my @lads = @( $.optimized_lads // (map { $_.lad }, @$.zyg) );
my $end = self.label;

die "check screwed up" unless defined $.dba;

my @code;
push @code, CgOp.ltm_push_alts([@lads], $.dba, [@ls]);
push @code, CgOp.goto('backtrack');
my $i = 0;
while $i < @ls {
push @code, CgOp.label(@ls[$i]);
push @code, $.zyg[$i].code($body);
push @code, CgOp.goto($end) unless $i == @ls - 1;
$i++;
}
push @code, CgOp.label($end);
push @code, CgOp.popcut;
@code;
}
}

# XXX should there be a different class for this?
augment class Op::Num { #OK exist
method code($) {
Expand All @@ -71,6 +95,8 @@ class RxOp::CutBrack is RxOp {
}

augment class CgOp {
# DELETE label_table, ladconstruct, get_lexer
method ltm_push_alts (*@x) { self._cgop("ltm_push_alts", @x) }
method outerlex ($n) { self._cgop("outerlex", $n) }
method newtypedscalar ($t) { self._cgop("newtypedscalar", $t) }
method bif_rand () { self._cgop("bif_rand") }
Expand Down

0 comments on commit f3e0436

Please sign in to comment.