Skip to content

Commit

Permalink
Thaw code for all LAD, Variable, ViviHook subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Oct 18, 2011
1 parent 19ada5b commit 054561b
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 89 deletions.
97 changes: 53 additions & 44 deletions lib/Builtins.cs
Expand Up @@ -92,6 +92,59 @@ class PosixWrapper {
f_ctime = Stat.GetField("st_ctime"); f_ctime = Stat.GetField("st_ctime");
} }
} }

class SubstrLValue: Variable {
Variable backing;
int from;
int length;

private SubstrLValue() {}
public SubstrLValue(Variable backing, int from, int length) {
this.backing = backing;
this.from = from;
this.length = length;
// XXX Should binding a substr lvalue count as binding the original?
this.whence = null;
this.rw = backing.rw;
this.type = Kernel.StrMO;
}

public override P6any Fetch() {
string str = backing.Fetch().mo.mro_raw_Str.Get(backing);
string sub = Builtins.LaxSubstring2(str, from, length);
return sub == null ? Kernel.StrMO.typeObject :
Kernel.BoxRaw(sub,Kernel.StrMO);
}

public override void Store(P6any v) {
string str = backing.Fetch().mo.mro_raw_Str.Get(backing);
int left = (from < 0) ? 0 : (from > str.Length) ? str.Length : from;
int right = ((length > (str.Length - left)) ? (str.Length - left) :
(length < 0) ? 0 : length) + left;
string lfr = str.Substring(0, left);
string mfr = v.mo.mro_raw_Str.Get(Kernel.NewROScalar(v));
string rfr = str.Substring(right);
backing.Store(Kernel.BoxRaw<string>(lfr + mfr + rfr, Kernel.StrMO));
}

public override Variable GetVar() {
return Kernel.BoxAnyMO<Variable>(this, Kernel.ScalarMO);
}
public override void Freeze(Niecza.Serialization.FreezeBuffer fb) {
fb.Byte((byte)Niecza.Serialization.SerializationCode.SubstrLValue);
fb.ObjRef(backing);
fb.Int(from);
fb.Int(length);
}
internal static object Thaw(Niecza.Serialization.ThawBuffer tb) {
var n = new SubstrLValue();
tb.Register(n);
n.backing = (Variable) tb.ObjRef();
n.from = tb.Int();
n.length = tb.Int();
return n;
}
}
} }


public partial class Builtins { public partial class Builtins {
Expand Down Expand Up @@ -187,50 +240,6 @@ public partial class Builtins {
} }
} }


class SubstrLValue: Variable {
Variable backing;
int from;
int length;

public SubstrLValue(Variable backing, int from, int length) {
this.backing = backing;
this.from = from;
this.length = length;
// XXX Should binding a substr lvalue count as binding the original?
this.whence = null;
this.rw = backing.rw;
this.type = Kernel.StrMO;
}

public override P6any Fetch() {
string str = backing.Fetch().mo.mro_raw_Str.Get(backing);
string sub = Builtins.LaxSubstring2(str, from, length);
return sub == null ? Kernel.StrMO.typeObject :
Kernel.BoxRaw(sub,Kernel.StrMO);
}

public override void Store(P6any v) {
string str = backing.Fetch().mo.mro_raw_Str.Get(backing);
int left = (from < 0) ? 0 : (from > str.Length) ? str.Length : from;
int right = ((length > (str.Length - left)) ? (str.Length - left) :
(length < 0) ? 0 : length) + left;
string lfr = str.Substring(0, left);
string mfr = v.mo.mro_raw_Str.Get(Kernel.NewROScalar(v));
string rfr = str.Substring(right);
backing.Store(Kernel.BoxRaw<string>(lfr + mfr + rfr, Kernel.StrMO));
}

public override Variable GetVar() {
return Kernel.BoxAnyMO<Variable>(this, Kernel.ScalarMO);
}
public override void Freeze(Niecza.Serialization.FreezeBuffer fb) {
fb.Byte((byte)Niecza.Serialization.SerializationCode.SubstrLValue);
fb.ObjRef(backing);
fb.Int(from);
fb.Int(length);
}
}

public static string LaxSubstring(string str, int from) { public static string LaxSubstring(string str, int from) {
if (from < 0 || from > str.Length) if (from < 0 || from > str.Length)
return null; return null;
Expand Down
99 changes: 82 additions & 17 deletions lib/Cursor.cs
Expand Up @@ -957,7 +957,7 @@ public abstract class LAD : IFreeze {
} }


public class LADStr : LAD { public class LADStr : LAD {
public readonly string text; public string text;
public LADStr(string text) { this.text = text; } public LADStr(string text) { this.text = text; }


public override LAD Reify(NFA pad) { return this; } public override LAD Reify(NFA pad) { return this; }
Expand Down Expand Up @@ -985,11 +985,18 @@ public class LADStr : LAD {
fb.Byte((byte) SerializationCode.LADStr); fb.Byte((byte) SerializationCode.LADStr);
fb.String(text); fb.String(text);
} }
internal static LADStr Thaw(ThawBuffer tb) {
LADStr n = new LADStr(null);
tb.Register(n);
n.text = tb.String();
return n;
}
} }


public class LADStrNoCase : LAD { public class LADStrNoCase : LAD {
public readonly string text; public string text;
public LADStrNoCase(string text) { this.text = text; } public LADStrNoCase(string text) { this.text = text; }
private LADStrNoCase() { }


public override LAD Reify(NFA pad) { return this; } public override LAD Reify(NFA pad) { return this; }
public override void QueryLiteral(NFA pad, out int len, out bool cont) { public override void QueryLiteral(NFA pad, out int len, out bool cont) {
Expand All @@ -1016,10 +1023,17 @@ public class LADStrNoCase : LAD {
fb.Byte((byte) SerializationCode.LADStrNoCase); fb.Byte((byte) SerializationCode.LADStrNoCase);
fb.String(text); fb.String(text);
} }
internal static LADStrNoCase Thaw(ThawBuffer tb) {
LADStrNoCase n = new LADStrNoCase();
tb.Register(n);
n.text = tb.String();
return n;
}
} }


public class LADCC : LAD { public class LADCC : LAD {
public readonly CC cc; public CC cc;
private LADCC() { }
public LADCC(CC cc) { this.cc = cc; } public LADCC(CC cc) { this.cc = cc; }


public override LAD Reify(NFA pad) { return this; } public override LAD Reify(NFA pad) { return this; }
Expand All @@ -1032,7 +1046,13 @@ public class LADCC : LAD {
} }
public override void Freeze(FreezeBuffer fb) { public override void Freeze(FreezeBuffer fb) {
fb.Byte((byte) SerializationCode.LADCC); fb.Byte((byte) SerializationCode.LADCC);
fb.Ints(cc.vec); fb.ObjRef(cc);
}
internal static LADCC Thaw(ThawBuffer tb) {
LADCC n = new LADCC();
tb.Register(n);
n.cc = (CC) tb.ObjRef();
return n;
} }
} }


Expand Down Expand Up @@ -1098,8 +1118,9 @@ public class LADDot : LAD {
} }


public class LADStar : LAD { public class LADStar : LAD {
public readonly LAD child; public LAD child;
public LADStar(LAD child) { this.child = child; } public LADStar(LAD child) { this.child = child; }
private LADStar() {}


public override LAD Reify(NFA pad) { return new LADStar(child.Reify(pad)); } public override LAD Reify(NFA pad) { return new LADStar(child.Reify(pad)); }
public override void ToNFA(NFA pad, int from, int to) { public override void ToNFA(NFA pad, int from, int to) {
Expand All @@ -1117,12 +1138,19 @@ public class LADStar : LAD {
fb.Byte((byte) SerializationCode.LADStar); fb.Byte((byte) SerializationCode.LADStar);
fb.ObjRef(child); fb.ObjRef(child);
} }
internal static object Thaw(ThawBuffer tb) {
var n = new LADStar();
tb.Register(n);
n.child = (LAD) tb.ObjRef();
return n;
}
} }


public class LADOpt : LAD { public class LADOpt : LAD {
public readonly LAD child; public LAD child;
public override LAD Reify(NFA pad) { return new LADOpt(child.Reify(pad)); } public override LAD Reify(NFA pad) { return new LADOpt(child.Reify(pad)); }
public LADOpt(LAD child) { this.child = child; } public LADOpt(LAD child) { this.child = child; }
private LADOpt() {}


public override void ToNFA(NFA pad, int from, int to) { public override void ToNFA(NFA pad, int from, int to) {
pad.AddEdge(from, to, null); pad.AddEdge(from, to, null);
Expand All @@ -1137,12 +1165,19 @@ public class LADOpt : LAD {
fb.Byte((byte) SerializationCode.LADOpt); fb.Byte((byte) SerializationCode.LADOpt);
fb.ObjRef(child); fb.ObjRef(child);
} }
internal static object Thaw(ThawBuffer tb) {
var n = new LADOpt();
tb.Register(n);
n.child = (LAD) tb.ObjRef();
return n;
}
} }


public class LADPlus : LAD { public class LADPlus : LAD {
public readonly LAD child; public LAD child;
public override LAD Reify(NFA pad) { return new LADPlus(child.Reify(pad)); } public override LAD Reify(NFA pad) { return new LADPlus(child.Reify(pad)); }
public LADPlus(LAD child) { this.child = child; } public LADPlus(LAD child) { this.child = child; }
private LADPlus() { }


public override void QueryLiteral(NFA pad, out int len, out bool cont) { public override void QueryLiteral(NFA pad, out int len, out bool cont) {
child.QueryLiteral(pad, out len, out cont); child.QueryLiteral(pad, out len, out cont);
Expand All @@ -1165,11 +1200,18 @@ public class LADPlus : LAD {
fb.Byte((byte) SerializationCode.LADPlus); fb.Byte((byte) SerializationCode.LADPlus);
fb.ObjRef(child); fb.ObjRef(child);
} }
internal static object Thaw(ThawBuffer tb) {
var n = new LADPlus();
tb.Register(n);
n.child = (LAD) tb.ObjRef();
return n;
}
} }


public class LADSequence : LAD { public class LADSequence : LAD {
public readonly LAD[] args; public LAD[] args;
public LADSequence(LAD[] args) { this.args = args; } public LADSequence(LAD[] args) { this.args = args; }
private LADSequence() { }
public override LAD Reify(NFA pad) { public override LAD Reify(NFA pad) {
LAD[] nc = new LAD[args.Length]; LAD[] nc = new LAD[args.Length];
for (int i = 0; i < args.Length; i++) for (int i = 0; i < args.Length; i++)
Expand Down Expand Up @@ -1204,15 +1246,20 @@ public class LADSequence : LAD {
} }
public override void Freeze(FreezeBuffer fb) { public override void Freeze(FreezeBuffer fb) {
fb.Byte((byte) SerializationCode.LADSequence); fb.Byte((byte) SerializationCode.LADSequence);
fb.Int(args.Length); fb.Refs(args);
foreach (LAD l in args) }
fb.ObjRef(l); internal static object Thaw(ThawBuffer tb) {
var n = new LADSequence();
tb.Register(n);
n.args = tb.RefsA<LAD>();
return n;
} }
} }


public class LADAny : LAD { public class LADAny : LAD {
public readonly LAD[] zyg; public LAD[] zyg;
public LADAny(LAD[] zyg) { this.zyg = zyg; } public LADAny(LAD[] zyg) { this.zyg = zyg; }
private LADAny() { }
public override LAD Reify(NFA pad) { public override LAD Reify(NFA pad) {
LAD[] nc = new LAD[zyg.Length]; LAD[] nc = new LAD[zyg.Length];
for (int i = 0; i < zyg.Length; i++) for (int i = 0; i < zyg.Length; i++)
Expand All @@ -1232,14 +1279,19 @@ public class LADAny : LAD {
} }
public override void Freeze(FreezeBuffer fb) { public override void Freeze(FreezeBuffer fb) {
fb.Byte((byte) SerializationCode.LADAny); fb.Byte((byte) SerializationCode.LADAny);
fb.Int(zyg.Length); fb.Refs(zyg);
foreach (LAD l in zyg) }
fb.ObjRef(l); internal static object Thaw(ThawBuffer tb) {
var n = new LADAny();
tb.Register(n);
n.zyg = tb.RefsA<LAD>();
return n;
} }
} }


public class LADParam : LAD { public class LADParam : LAD {
public readonly string name; public string name;
private LADParam() {}
public LADParam(string name) { this.name = name; } public LADParam(string name) { this.name = name; }


public override LAD Reify(NFA pad) { public override LAD Reify(NFA pad) {
Expand Down Expand Up @@ -1303,12 +1355,19 @@ public class LADParam : LAD {
fb.Byte((byte) SerializationCode.LADParam); fb.Byte((byte) SerializationCode.LADParam);
fb.String(name); fb.String(name);
} }
internal static LADParam Thaw(ThawBuffer tb) {
LADParam n = new LADParam();
tb.Register(n);
n.name = tb.String();
return n;
}
} }


public class LADMethod : LAD { public class LADMethod : LAD {
public readonly string name; public string name;


public LADMethod(string name) { this.name = name; } public LADMethod(string name) { this.name = name; }
private LADMethod() {}


public override void ToNFA(NFA pad, int from, int to) { public override void ToNFA(NFA pad, int from, int to) {
throw new InvalidOperationException(); throw new InvalidOperationException();
Expand Down Expand Up @@ -1358,6 +1417,12 @@ public class LADMethod : LAD {
fb.Byte((byte) SerializationCode.LADMethod); fb.Byte((byte) SerializationCode.LADMethod);
fb.String(name); fb.String(name);
} }
internal static LADMethod Thaw(ThawBuffer tb) {
LADMethod n = new LADMethod();
tb.Register(n);
n.name = tb.String();
return n;
}
} }


// Only really makes sense if used in the static scope of a proto // Only really makes sense if used in the static scope of a proto
Expand Down
31 changes: 25 additions & 6 deletions lib/Kernel.cs
Expand Up @@ -93,8 +93,11 @@ public class SubViviHook : ViviHook {
fb.Byte((byte)SerializationCode.SubViviHook); fb.Byte((byte)SerializationCode.SubViviHook);
fb.ObjRef(sub); fb.ObjRef(sub);
} }
internal static SubViviHook Thaw(ThawBuffer tb) { internal static object Thaw(ThawBuffer tb) {
return new SubViviHook((P6any)tb.ObjRef()); var n = new SubViviHook(null);
tb.Register(n);
n.sub = (P6any) tb.ObjRef();
return n;
} }
} }


Expand All @@ -112,7 +115,11 @@ public class HashViviHook : ViviHook {
fb.String(key); fb.String(key);
} }
internal static IFreeze Thaw(ThawBuffer tb) { internal static IFreeze Thaw(ThawBuffer tb) {
return new HashViviHook((P6any)tb.ObjRef(), tb.String()); var n = new HashViviHook(null, null);
tb.Register(n);
n.hash = (P6any) tb.ObjRef();
n.key = tb.String();
return n;
} }
} }


Expand All @@ -131,7 +138,11 @@ public class NewHashViviHook : ViviHook {
fb.String(key); fb.String(key);
} }
internal static IFreeze Thaw(ThawBuffer tb) { internal static IFreeze Thaw(ThawBuffer tb) {
return new NewHashViviHook((Variable)tb.ObjRef(), tb.String()); var n = new NewHashViviHook(null, null);
tb.Register(n);
n.hashv = (Variable) tb.ObjRef();
n.key = tb.String();
return n;
} }
} }


Expand All @@ -151,7 +162,11 @@ public class ArrayViviHook : ViviHook {
fb.Int(key); fb.Int(key);
} }
internal static IFreeze Thaw(ThawBuffer tb) { internal static IFreeze Thaw(ThawBuffer tb) {
return new ArrayViviHook((P6any)tb.ObjRef(), tb.Int()); var n = new ArrayViviHook(null, 0);
tb.Register(n);
n.ary = (P6any) tb.ObjRef();
n.key = tb.Int();
return n;
} }
} }


Expand All @@ -175,7 +190,11 @@ public class NewArrayViviHook : ViviHook {
fb.Int(key); fb.Int(key);
} }
internal static IFreeze Thaw(ThawBuffer tb) { internal static IFreeze Thaw(ThawBuffer tb) {
return new NewArrayViviHook((Variable)tb.ObjRef(), tb.Int()); var n = new NewArrayViviHook(null, 0);
tb.Register(n);
n.ary = (Variable) tb.ObjRef();
n.key = tb.Int();
return n;
} }
} }


Expand Down

0 comments on commit 054561b

Please sign in to comment.