Skip to content

Commit

Permalink
Remove capability of rebinding lexical constants (fixes #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Nov 5, 2011
1 parent eb2bc3c commit ccb1914
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
8 changes: 4 additions & 4 deletions lib/CodeGen.cs
Expand Up @@ -3734,8 +3734,8 @@ public class DowncallReceiver : CallReceiver {
string cn = (string)args[2];
while (si != null && !si.dylex.ContainsKey(cn)) si = si.outer;
LexInfo li = si == null ? null : si.dylex[cn];
if (li is LIHint) {
((LIHint)li).var.v = v;
if (li is LIConstant) {
((LIConstant)li).value = v;
} else if (li is LICommon) {
StashEnt hkey = Kernel.currentGlobals[((LICommon)li).hkey];
hkey.constant = true;
Expand Down Expand Up @@ -3858,7 +3858,7 @@ public class DowncallReceiver : CallReceiver {
var llab = li as LILabel;
if (llab != null)
r = new object[] { "label",null,null,null };
var lhint = li as LIHint;
var lhint = li as LIConstant;
if (lhint != null)
r = new object[] { "hint",null,null,null };
var lcomm = li as LICommon;
Expand Down Expand Up @@ -4186,7 +4186,7 @@ public class DowncallReceiver : CallReceiver {

return AddLexical(args, new LISimple(flags, type));
} else if (cmd == "add_hint") {
return AddLexical(args, new LIHint());
return AddLexical(args, new LIConstant());
} else if (cmd == "add_label") {
return AddLexical(args, new LILabel());
} else if (cmd == "add_dispatcher") {
Expand Down
37 changes: 19 additions & 18 deletions lib/Kernel.cs
Expand Up @@ -1091,7 +1091,7 @@ public abstract class LexInfo {

internal abstract void DoFreeze(FreezeBuffer fb);
internal enum LexSerCode {
Simple, Sub, Label, Dispatch, Common, Hint, Package, Alias
Simple, Sub, Label, Dispatch, Common, Constant, Package, Alias
}
}

Expand Down Expand Up @@ -1170,32 +1170,33 @@ public class LICommon : LexInfo {
}
}

public class LIHint : LexInfo {
public StashEnt var;
public LIHint() { }
public LIHint(StashEnt var) { this.var = var; }
public class LIConstant : LexInfo {
public Variable value;
public LIConstant() { this.value = Kernel.AnyMO.typeVar; }
internal LIConstant(Variable value) { this.value = value; }
public override void Init(Frame f) { }
public override void BindFields() {
var = new StashEnt();
}
public override void BindFields() { }

public override object Get(Frame f) { return var.v; }
public override void Set(Frame f, object to) { var.v = (Variable)to; }
public override object Get(Frame f) { return value; }
public override void Set(Frame f, object to) {
throw new NieczaException("Cannot bind to constant " + name);
}

internal override ClrOp GetCode(int up) {
return new ClrGetField(Tokens.StashEnt_v,
EmitUnit.Current.RefConstant(name, "E", var, null).head);
return EmitUnit.Current.RefConstant(name, "", value, typeof(Variable)).head;
}

// XXX should die() with constant improvements
internal override ClrOp SetCode(int up, ClrOp to) {
return new ClrSetField(Tokens.StashEnt_v,
EmitUnit.Current.RefConstant(name, "E", var, null).head, to);
return new ClrOperator(Tokens.Void, OpCodes.Throw, new [] {
new ClrConstructorCall(typeof(NieczaException).
GetConstructor(new [] { typeof(string) }),
new [] { new ClrStringLiteral("Cannot bind to constant " + name) }) });
}

internal override void DoFreeze(FreezeBuffer fb) {
fb.Byte((byte)LexSerCode.Hint);
fb.ObjRef(var);
fb.Byte((byte)LexSerCode.Constant);
fb.ObjRef(value);
}
}

Expand Down Expand Up @@ -2101,8 +2102,8 @@ public class UsedInScopeInfo {
case (int) LexInfo.LexSerCode.Common:
li = new LICommon(tb.String());
break;
case (int) LexInfo.LexSerCode.Hint:
li = new LIHint((StashEnt) tb.ObjRef());
case (int) LexInfo.LexSerCode.Constant:
li = new LIConstant((Variable) tb.ObjRef());
break;
case (int) LexInfo.LexSerCode.Package:
li = new LIPackage((STable) tb.ObjRef());
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialize.cs
Expand Up @@ -64,7 +64,7 @@ struct ObjRef {
internal static HashAlgorithm NewHash() { return new SHA256Managed(); }

static readonly string signature = "Niecza-Serialized-Module";
static readonly int version = 5;
static readonly int version = 6;

// Routines for use by serialization code
public bool CheckWriteObject(SerUnit into, object o,
Expand Down

0 comments on commit ccb1914

Please sign in to comment.