Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reimplement Bool as an enum
  • Loading branch information
sorear committed Jun 7, 2011
1 parent c757e63 commit 39dde58
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/CLRBackend.cs
Expand Up @@ -2579,7 +2579,6 @@ class ClrNewDataArray : ClrOp {
class ClrWiden : ClrOp {
readonly ClrOp z;
public ClrWiden(Type to, ClrOp z) {
TypeCheck(z.Returns, to);
Returns = to;
this.z = z;
this.Constant = z.Constant;
Expand Down Expand Up @@ -3342,6 +3341,8 @@ class NamProcessor {
return CpsOp.Operator(tty, OpCodes.Conv_R8, z);
} else if (tty == Tokens.Int32 && fty == Tokens.Double) {
return CpsOp.Operator(tty, OpCodes.Conv_I4, z);
} else if (fty == Tokens.Boolean && tty == Tokens.Int32) {
return CpsOp.Widen(tty, z);
} else {
throw new NotImplementedException("cast " + fty + " -> " + tty);
}
Expand Down
18 changes: 3 additions & 15 deletions lib/CORE.setting
Expand Up @@ -388,22 +388,10 @@ my class StrBasedEnum is CommonEnum {
} }
}

my class Bool is Cool {
method Str() { self ?? "Bool::True" !! "Bool::False" }
method Stringy() { self.key }
method perl() { defined(self) ?? ~self !! self.typename }
my enum Bool < False True >;
augment class Bool {
method ACCEPTS(Mu $t) { defined(self) ?? self !! $t.^does(self) }
our constant True = Q:CgOp { (box Bool (bool 1)) };
our constant False = Q:CgOp { (box Bool (bool 0)) };
method succ() { True }
method pred() { False }
method key() { self ?? "True" !! "False" }
method value() { self ?? 1 !! 0 }
method kv() { self.key, self.value }
}
# TODO: import
constant True = Q:CgOp { (box Bool (bool 1)) };
constant False = Q:CgOp { (box Bool (bool 0)) };
}
# }}}
# Fundamental scalar operators {{{
sub infix:<~>(\|$bits) { Q:CgOp {
Expand Down
12 changes: 6 additions & 6 deletions lib/JSYNC.cs
Expand Up @@ -31,7 +31,7 @@ public class JsyncWriter {
} else if (obj.Isa(Kernel.HashMO)) {
WriteHash(obj);
} else if (obj.Isa(Kernel.BoolMO)) {
WriteBool(Kernel.UnboxAny<bool>(obj));
WriteBool(Kernel.UnboxAny<int>(obj) != 0);
} else if (obj.Isa(Kernel.StrMO)) {
WriteStr(true, Kernel.UnboxAny<string>(obj));
} else if (obj.Isa(Kernel.NumMO) || obj.Isa(Kernel.IntMO) || obj.Isa(Kernel.RatMO) || obj.Isa(Kernel.FatRatMO)) {
Expand Down Expand Up @@ -269,10 +269,10 @@ public class JsyncReader {
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.AnyP);
case 't':
SkipToken("true");
return BoxRW<bool>(true, Kernel.BoolMO);
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.TrueV.Fetch());
case 'f':
SkipToken("false");
return BoxRW<bool>(false, Kernel.BoolMO);
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.FalseV.Fetch());
default:
return GetFromNumber();
}
Expand Down Expand Up @@ -379,10 +379,10 @@ public class JsyncReader {
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.AnyP);
} else if (look == 't') {
SkipToken("true");
return BoxRW<bool>(true, Kernel.BoolMO);
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.TrueV.Fetch());
} else if (look == 'f') {
SkipToken("false");
return BoxRW<bool>(false, Kernel.BoolMO);
return Kernel.NewRWScalar(Kernel.AnyMO, Kernel.FalseV.Fetch());
} else {
double d;
string tx = GetJsonNumber();
Expand Down Expand Up @@ -810,7 +810,7 @@ public class JsonWriter {
if (!obj.IsDefined()) {
o.Append("null");
} else if (obj.Isa(Kernel.BoolMO)) {
o.Append(Kernel.UnboxAny<bool>(obj) ? "true" : "false");
o.Append(Kernel.UnboxAny<int>(obj) != 0 ? "true" : "false");
} else if (obj.Isa(Kernel.NumMO) || obj.Isa(Kernel.IntMO) || obj.Isa(Kernel.RatMO) || obj.Isa(Kernel.FatRatMO)) {
o.Append(Utils.N2S(obj.mo.mro_raw_Numeric.Get(Kernel.NewROScalar(obj))));
} else if (obj.Isa(Kernel.StrMO)) {
Expand Down
51 changes: 35 additions & 16 deletions lib/Kernel.cs
Expand Up @@ -1384,6 +1384,15 @@ class CtxCallMethodUnbox<T> : ContextHandler<T> {
return Kernel.UnboxAny<T>(v.Fetch());
}
}
class CtxCallMethodUnboxBool : ContextHandler<bool> {
string method;
public CtxCallMethodUnboxBool(string method) { this.method = method; }

public override bool Get(Variable obj) {
Variable v = Kernel.RunInferior(obj.Fetch().InvokeMethod(Kernel.GetInferiorRoot(), method, new Variable[] { obj }, null));
return Kernel.UnboxAny<int>(v.Fetch()) != 0;
}
}

class CtxCallMethodUnboxNumeric : ContextHandler<double> {
string method;
Expand Down Expand Up @@ -1445,6 +1454,14 @@ class CtxJustUnbox<T> : ContextHandler<T> {
}
}

class CtxBoolUnbox : ContextHandler<bool> {
public override bool Get(Variable obj) {
P6any o = obj.Fetch();
if (!o.IsDefined()) return false;
return Kernel.UnboxAny<int>(o) != 0;
}
}

class CtxReturnSelf : ContextHandler<Variable> {
public override Variable Get(Variable obj) {
return Kernel.NewROScalar(obj.Fetch());
Expand Down Expand Up @@ -1692,13 +1709,6 @@ class CtxBoolNativeDefined : ContextHandler<Variable> {
}
}

class CtxBoolRawNumeric : ContextHandler<double> {
public override double Get(Variable obj) {
P6any o = obj.Fetch();
return (o.IsDefined() && Kernel.UnboxAny<bool>(o)) ? 1 : 0;
}
}

class CtxNumSuccish : ContextHandler<P6any> {
double amt;
public CtxNumSuccish(double amt) { this.amt = amt; }
Expand Down Expand Up @@ -2732,8 +2742,12 @@ public class MMDCandidateLongname {
info.code = SaferTrap;
}
public static Variable BoxAny<T>(T v, P6any proto) {
if (proto == BoolMO.typeObject)
return ((bool) (object) v) ? TrueV : FalseV;
if (proto == BoolMO.typeObject) {
if (v is bool)
return ((bool) (object) v) ? TrueV : FalseV;
else
return ((int) (object) v) != 0 ? TrueV : FalseV;
}
return NewROScalar(new BoxObject<T>(v, ((P6opaque)proto).mo));
}

Expand All @@ -2742,8 +2756,12 @@ public class MMDCandidateLongname {
}

public static Variable BoxAnyMO<T>(T v, STable proto) {
if (proto == BoolMO)
return ((bool) (object) v) ? TrueV : FalseV;
if (proto == BoolMO) {
if (v is bool)
return ((bool) (object) v) ? TrueV : FalseV;
else
return ((int) (object) v) != 0 ? TrueV : FalseV;
}
return NewROScalar(new BoxObject<T>(v, proto));
}

Expand Down Expand Up @@ -3478,12 +3496,11 @@ class LastFrameNode {

BoolMO = new STable("Bool");
Handler_Vonly(BoolMO, "Bool", new CtxReturnSelf(),
new CtxJustUnbox<bool>(false));
Handler_PandBoxInty(BoolMO, "Numeric", new CtxBoolRawNumeric());
BoolMO.FillProtoClass(new string[] { });
new CtxBoolUnbox());
BoolMO.FillProtoClass(new string[] { "index" });
BoolMO.Invalidate();
TrueV = NewROScalar(BoxRaw<bool>(true, BoolMO));
FalseV = NewROScalar(BoxRaw<bool>(false, BoolMO));
TrueV = NewROScalar(BoxRaw<int>(1, BoolMO));
FalseV = NewROScalar(BoxRaw<int>(0, BoolMO));

StrMO = new STable("Str");
Handler_Vonly(StrMO, "Str", new CtxReturnSelf(),
Expand Down Expand Up @@ -3523,6 +3540,8 @@ class LastFrameNode {
Handler_PandCont(IntMO, "pred", new CtxIntSuccish(-1));
IntMO.FillProtoClass(new string[] { });
IntMO.Invalidate();
FalseV.Fetch().SetSlot("index", BoxAnyMO(0, IntMO));
TrueV.Fetch().SetSlot("index", BoxAnyMO(1, IntMO));

RatMO = new STable("Rat");
Handler_Vonly(RatMO, "Numeric", new CtxReturnSelf(),
Expand Down
4 changes: 2 additions & 2 deletions lib/ObjModel.cs
Expand Up @@ -479,11 +479,11 @@ public class STable {
public static readonly ContextHandler<string> RawCallStr
= new CtxCallMethodUnbox<string>("Str");
public static readonly ContextHandler<bool> RawCallBool
= new CtxCallMethodUnbox<bool>("Bool");
= new CtxCallMethodUnboxBool("Bool");
public static readonly ContextHandler<double> RawCallNumeric
= new CtxCallMethodUnboxNumeric("Numeric");
public static readonly ContextHandler<bool> RawCallDefined
= new CtxCallMethodUnbox<bool>("defined");
= new CtxCallMethodUnboxBool("defined");
public static readonly ContextHandler<VarDeque> RawCallIterator
= new CtxCallMethodUnbox<VarDeque>("iterator");
public static readonly ContextHandler<Variable[]> RawCallReify
Expand Down

0 comments on commit 39dde58

Please sign in to comment.