Skip to content

Commit

Permalink
[types] Float type.
Browse files Browse the repository at this point in the history
  • Loading branch information
exe committed Mar 30, 2014
1 parent 45ba14b commit b8317f0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions CiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CiParser()
globals.Add(CiBoolType.Value);
globals.Add(CiByteType.Value);
globals.Add(CiIntType.Value);
globals.Add(CiFloatType.Value);
globals.Add(CiStringPtrType.Value);
globals.Add(new CiConst { Name = "true", Value = true, Type = CiBoolType.Value });
globals.Add(new CiConst { Name = "false", Value = false, Type = CiBoolType.Value });
Expand Down
26 changes: 24 additions & 2 deletions CiResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ CiMaybeAssign Coerce(CiMaybeAssign expr, CiType expected)
return new CiConstExpr((object) (byte) (int) konst.Value);
return new CiCoercion { ResultType = expected, Inner = expr };
}
if (expected == CiFloatType.Value && got == CiIntType.Value) {
return new CiCoercion { ResultType = expected, Inner = expr };
}
if (expected == CiFloatType.Value && got == CiByteType.Value) {
return new CiCoercion { ResultType = expected, Inner = expr };
}
if (expected == CiStringPtrType.Value && (got == CiType.Null || got is CiStringType))
return expr;
if (expected is CiStringStorageType && got is CiStringType)
Expand Down Expand Up @@ -535,7 +541,12 @@ CiExpr ICiExprVisitor.Visit(CiUnaryExpr expr)
resolved = ResolveLValue(expr.Inner);
else
resolved = Resolve(expr.Inner);
expr.Inner = Coerce(resolved, CiIntType.Value);
if (resolved.Type is CiFloatType) {
expr.Inner = Coerce(resolved, CiFloatType.Value);
}
else {
expr.Inner = Coerce(resolved, CiIntType.Value);
}
if (expr.Op == CiToken.Minus && expr.Inner is CiConstExpr)
return new CiConstExpr(-GetConstInt(expr.Inner));
return expr;
Expand Down Expand Up @@ -564,6 +575,14 @@ CiExpr ICiExprVisitor.Visit(CiBinaryExpr expr)
string b = GetConstString(right);
return new CiConstExpr(a + b);
}
if ((expr.Op == CiToken.Plus || expr.Op == CiToken.Minus
|| expr.Op == CiToken.Asterisk || expr.Op == CiToken.Slash)
&& left.Type is CiFloatType || right.Type is CiFloatType) {
expr.Left = Coerce(left, CiFloatType.Value);
expr.Right = Coerce(right, CiFloatType.Value);
expr.ResultType = CiFloatType.Value;
return expr;
}
left = Coerce(left, CiIntType.Value);
right = Coerce(right, CiIntType.Value);
if (right is CiConstExpr) {
Expand Down Expand Up @@ -635,6 +654,9 @@ CiExpr ICiExprVisitor.Visit(CiBoolBinaryExpr expr)
type = CiIntType.Value;
break;
}
if (left.Type == CiFloatType.Value || right.Type == CiFloatType.Value) {
type = CiFloatType.Value;
}
expr.Left = Coerce(left, type);
expr.Right = Coerce(right, type);
CiConstExpr cleft = expr.Left as CiConstExpr;
Expand Down Expand Up @@ -809,7 +831,7 @@ void ICiStatementVisitor.Visit(CiAssign statement)
CiType type = statement.Target.Type;
CheckCopyPtr(type, source);
statement.Source = Coerce(source, type);
if (statement.Op != CiToken.Assign && type != CiIntType.Value && type != CiByteType.Value) {
if (statement.Op != CiToken.Assign && type != CiIntType.Value && type != CiByteType.Value && type != CiFloatType.Value) {
if (statement.Op == CiToken.AddAssign && type is CiStringStorageType && statement.Source.Type is CiStringType)
{} // OK
else
Expand Down
17 changes: 16 additions & 1 deletion CiTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ public override CiSymbol LookupMember(string name)
}
}

public class CiFloatType : CiType
{
private CiFloatType() { }
public static readonly CiFloatType Value = new CiFloatType { Name = "float" };
public override Type DotNetType { get { return typeof(float); } }
public override CiSymbol LookupMember(string name)
{
switch (name) {
default: throw new ParseException("No member {0} in float", name);
}
}
}


public abstract class CiStringType : CiType
{
public override Type DotNetType { get { return typeof(string); } }
Expand Down Expand Up @@ -605,7 +619,8 @@ public class CiBinaryExpr : CiExpr
public CiExpr Left;
public CiToken Op;
public CiExpr Right;
public override CiType Type { get { return CiIntType.Value; } }
public CiType ResultType = CiIntType.Value;
public override CiType Type { get { return ResultType; } }
public override bool HasSideEffect { get { return this.Left.HasSideEffect || this.Right.HasSideEffect; } }
public override CiExpr Accept(ICiExprVisitor v) { return v.Visit(this); }
}
Expand Down
8 changes: 5 additions & 3 deletions GenAs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ void Write(CiType type)
Write("Boolean");
else if (type == CiByteType.Value || type is CiEnum)
Write("int");
else if (type == CiFloatType.Value)
Write("Number");
else if (type is CiArrayType)
Write(((CiArrayType) type).ElementType == CiByteType.Value ? "ByteArray" : "Array");
Write(((CiArrayType)type).ElementType == CiByteType.Value ? "ByteArray" : "Array");
else if (type is CiDelegate)
Write("Function");
else
Write(type.Name);
Write(type.Name);
}

bool WriteInit(CiType type)
Expand Down Expand Up @@ -311,7 +313,7 @@ protected override void Write(CiMethodCall expr)

protected override void Write(CiBinaryExpr expr)
{
if (expr.Op == CiToken.Slash) {
if (expr.Op == CiToken.Slash && expr.Type != CiFloatType.Value) {
Write("int(");
WriteChild(CiPriority.Multiplicative, expr.Left);
Write(" / ");
Expand Down
2 changes: 1 addition & 1 deletion GenJs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected override void Write(CiMethodCall expr)

protected override void Write(CiBinaryExpr expr)
{
if (expr.Op == CiToken.Slash) {
if (expr.Op == CiToken.Slash && expr.Type != CiFloatType.Value) {
Write("Math.floor(");
WriteChild(CiPriority.Multiplicative, expr.Left);
Write(" / ");
Expand Down
11 changes: 11 additions & 0 deletions GenJsWithTypedArrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ protected override void WriteNew(CiType type)
Write("))");
return;
}
if (arrayType.ElementType == CiFloatType.Value) {
Write("new Float32Array(new ArrayBuffer(");
if (arrayType.LengthExpr != null) {
WriteChild(CiPriority.Shift, arrayType.LengthExpr);
Write(" << 2");
}
else
Write(arrayType.Length << 2);
Write("))");
return;
}
}
base.WriteNew(type);
}
Expand Down

0 comments on commit b8317f0

Please sign in to comment.