Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow objects to fill the role of read-only variables
  • Loading branch information
sorear committed May 27, 2012
1 parent f1da062 commit cbe27a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
19 changes: 7 additions & 12 deletions lib/CodeGen.cs
Expand Up @@ -317,7 +317,7 @@ sealed class Tokens {
RxFrame.GetConstructor(new Type[] { String, Cursor, Boolean });
public static readonly ConstructorInfo SV_ctor =
typeof(SimpleVariable).GetConstructor(new Type[] {
Boolean, Boolean, STable, typeof(ViviHook), P6any });
STable, typeof(ViviHook), P6any });
public static readonly ConstructorInfo SubViviHook_ctor =
typeof(SubViviHook).GetConstructor(new Type[] { P6any });
public static readonly ConstructorInfo HashViviHook_ctor =
Expand Down Expand Up @@ -2928,28 +2928,23 @@ class NamProcessor {
return CpsOp.MethodCall(Tokens.Kernel_NewRWScalar,
CpsOp.GetSField(Tokens.Kernel_AnyMO), z[0]); };
thandlers["newvsubvar"] = delegate(CpsOp[] z) {
return CpsOp.ConstructorCall(Tokens.SV_ctor,
CpsOp.BoolLiteral(true), CpsOp.BoolLiteral(false), z[0],
return CpsOp.ConstructorCall(Tokens.SV_ctor, z[0],
CpsOp.ConstructorCall(Tokens.SubViviHook_ctor,
z[1]), z[2]); };
thandlers["newvhashvar"] = delegate(CpsOp[] z) {
return CpsOp.ConstructorCall(Tokens.SV_ctor,
CpsOp.BoolLiteral(true), CpsOp.BoolLiteral(false), z[0],
return CpsOp.ConstructorCall(Tokens.SV_ctor, z[0],
CpsOp.ConstructorCall(Tokens.HashViviHook_ctor,
z[1], z[2]), z[3]); };
thandlers["newvarrayvar"] = delegate(CpsOp[] z) {
return CpsOp.ConstructorCall(Tokens.SV_ctor,
CpsOp.BoolLiteral(true), CpsOp.BoolLiteral(false), z[0],
return CpsOp.ConstructorCall(Tokens.SV_ctor, z[0],
CpsOp.ConstructorCall(Tokens.ArrayViviHook_ctor,
z[1], z[2]), z[3]); };
thandlers["newvnewhashvar"] = delegate(CpsOp[] z) {
return CpsOp.ConstructorCall(Tokens.SV_ctor,
CpsOp.BoolLiteral(true), CpsOp.BoolLiteral(false), z[0],
return CpsOp.ConstructorCall(Tokens.SV_ctor, z[0],
CpsOp.ConstructorCall(Tokens.NewHashViviHook_ctor,
z[1], z[2]), z[3]); };
thandlers["newvnewarrayvar"] = delegate(CpsOp[] z) {
return CpsOp.ConstructorCall(Tokens.SV_ctor,
CpsOp.BoolLiteral(true), CpsOp.BoolLiteral(false), z[0],
return CpsOp.ConstructorCall(Tokens.SV_ctor, z[0],
CpsOp.ConstructorCall(Tokens.NewArrayViviHook_ctor,
z[1], z[2]), z[3]); };
thandlers["strbuf_append"] = delegate(CpsOp[] z) {
Expand Down Expand Up @@ -3676,7 +3671,7 @@ public class DowncallReceiver : CallReceiver {
object o = Handle.Unbox(args[1]);
return (o is SubInfo) ? "sub" : (o is RuntimeUnit) ? "unit" :
(o is STable) ? "type" : (o is Frame) ? "frame" :
(o is Variable) ? "value" : (o is Parameter) ? "param" :
(o is Parameter) ? "param" : (o is Variable) ? "value" :
"unknown";
}
public static object set_binding(object[] args) {
Expand Down
48 changes: 20 additions & 28 deletions lib/Kernel.cs
Expand Up @@ -218,15 +218,12 @@ public sealed class SimpleVariable: Variable {
int mode;

private SimpleVariable() { }
public SimpleVariable(bool rw, bool islist, STable type, ViviHook whence, P6any val) {
public SimpleVariable(STable type, ViviHook whence, P6any val) {
this.val = val; this.whence = whence;
this.type = type;
mode = rw ? RW : islist ? LIST : RO;
}
public SimpleVariable(P6any val) { this.val = val; }
public SimpleVariable(bool islist, P6any val) {
this.mode = islist ? LIST : RO; this.val = val;
mode = RW;
}
public SimpleVariable(P6any val) { this.mode = LIST; this.val = val; }

public override P6any Fetch() { return val; }
public override Variable Assign(Variable inp) {
Expand All @@ -239,7 +236,8 @@ public sealed class SimpleVariable: Variable {
}
public override Variable AssignO(P6any inp, bool listishly) {
if (mode == LIST) {
val.mo.mro_LISTSTORE.Get(this, new SimpleVariable(listishly, inp));
val.mo.mro_LISTSTORE.Get(this, listishly ?
(Variable)new SimpleVariable(inp) : inp);
} else {
Store(inp);
}
Expand Down Expand Up @@ -541,23 +539,22 @@ sealed class EmitUnit {
return RefConstant(s == null ? "" : s.name, "S", s, Tokens.SubInfo);
}
internal CpsOp FrameConstant(Frame s) {
return RefConstant(s.info.name, "P", s, Tokens.Frame);
return RefConstant(s.info.name, "F", s, Tokens.Frame);
}

internal CpsOp RefConstant(string n1, string n2, object val, Type nty) {
if (val == null)
return CpsOp.Null(nty);
FieldInfo fi;
if (!constants.TryGetValue(val, out fi))
constants[val] = fi = NewField(n1, n2,
val is Variable ? typeof(Variable) : val.GetType());
constants[val] = fi = NewField(n1, n2, nty ?? val.GetType());
return CpsOp.IsConst(CpsOp.GetSField(fi));
}

internal CpsOp ValConstant(string key, object val) {
CpsOp r;
if (!val_constants.TryGetValue(key, out r))
val_constants[key] = r = RefConstant(key, "", val, null);
val_constants[key] = r = RefConstant(key, "", val, val is Variable ? typeof(Variable) : null);
return r;
}

Expand Down Expand Up @@ -2838,7 +2835,7 @@ public class Frame: P6any, IFixup {
else {
if (src.Mode == (islist ? Variable.LIST : Variable.RO))
goto bound;
src = new SimpleVariable(islist, srco);
src = islist ? (Variable)new SimpleVariable(srco) : srco;
}
bound: ;
}
Expand Down Expand Up @@ -4047,8 +4044,8 @@ class IxHashAtKey : IndexHandler {
Variable r;
if (h.TryGetValue(kss, out r))
return r;
return new SimpleVariable(true, false, null,
new HashViviHook(os, kss), Kernel.AnyP);
return new SimpleVariable(null, new HashViviHook(os, kss),
Kernel.AnyP);
}
}
class IxHashExistsKey : IndexHandler {
Expand Down Expand Up @@ -4119,7 +4116,7 @@ class IxListAtPos : IndexHandler {
return Kernel.AnyMO.typeVar;
if (items.Count() <= ix) {
if (extend) {
return new SimpleVariable(true, false, null,
return new SimpleVariable(null,
new ArrayViviHook(os, ix), Kernel.AnyP);
} else {
return Kernel.AnyMO.typeVar;
Expand Down Expand Up @@ -5198,7 +5195,7 @@ internal class MMDCandidate : MultiCandidate {

public static Variable Decontainerize(Variable rhs) {
if (!rhs.Rw) return rhs;
return new SimpleVariable(false, rhs.Fetch());
return rhs.Fetch();
}

public const int NBV_RO = 0;
Expand All @@ -5224,36 +5221,31 @@ internal class MMDCandidate : MultiCandidate {
if (omode == (islist ? Variable.LIST : Variable.RO))
return rhs;

return new SimpleVariable(islist, rhso);
return islist ? (Variable)new SimpleVariable(rhso) : rhso;
}

public static Variable Assign(Variable lhs, Variable rhs) {
return lhs.Assign(rhs);
}

// ro, not rebindable
public static Variable NewROScalar(P6any obj) {
return new SimpleVariable(obj);
}

public static Variable NewRWScalar(STable t, P6any obj) {
return new SimpleVariable(true, false, t, null, obj);
return new SimpleVariable(t, null, obj);
}

public static Variable NewMuScalar(P6any obj) {
return new SimpleVariable(true, false, null, null, obj);
return new SimpleVariable(null, null, obj);
}

public static Variable NewTypedScalar(STable t) {
if (t == null)
return new SimpleVariable(true, false, null, null,
AnyMO.typeObject);
return new SimpleVariable(null, null, AnyMO.typeObject);

return new SimpleVariable(true, false, t, null, t.initObject);
return new SimpleVariable(t, null, t.initObject);
}

public static Variable NewROScalar(P6any o) { return o; }
public static Variable NewRWListVar(P6any container) {
return new SimpleVariable(true, container);
return new SimpleVariable(container);
}

public static VarDeque SlurpyHelper(Variable[] pos, int from) {
Expand Down
13 changes: 9 additions & 4 deletions lib/ObjModel.cs
Expand Up @@ -4,10 +4,15 @@
using Niecza.Serialization;

namespace Niecza {
public abstract class P6any: IFreeze {
public abstract class P6any: Variable, IFreeze {
public STable mo;

public abstract void Freeze(FreezeBuffer fb);
public override P6any Fetch() { return this; }
public override void Store(P6any val) {
throw new NieczaException("Writing to readonly scalar");
}
public override Variable GetVar() { return this; }
public override int Mode { get { return RO; } }

public virtual object GetSlot(STable type, string name) {
throw new NieczaException("Representation " + ReprName() + " does not support attributes");
Expand Down Expand Up @@ -108,12 +113,12 @@ public abstract class IndexHandler : ReflectObj {
}

public static Variable ViviHash(Variable obj, Variable key) {
return new SimpleVariable(true, false, Kernel.MuMO,
return new SimpleVariable(Kernel.MuMO,
new NewHashViviHook(obj, key.Fetch().mo.mro_raw_Str.Get(key)),
Kernel.AnyP);
}
public static Variable ViviArray(Variable obj, Variable key) {
return new SimpleVariable(true, false, Kernel.MuMO,
return new SimpleVariable(Kernel.MuMO,
new NewArrayViviHook(obj, (int)key.Fetch().mo.mro_raw_Numeric.Get(key)),
Kernel.AnyP);
}
Expand Down

0 comments on commit cbe27a0

Please sign in to comment.