Guest User

Untitled

a guest
Jun 16th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1.  CiParser.cs   |  1 +
  2.  CiResolver.cs | 24 ++++++++++++++++++++++++
  3.  CiTree.cs     | 17 ++++++++++++++++-
  4.  GenJs.cs      |  2 +-
  5.  4 files changed, 42 insertions(+), 2 deletions(-)
  6.  
  7. diff --git a/CiParser.cs b/CiParser.cs
  8. index 9a595dd..7e6ef1b 100644
  9. --- a/CiParser.cs
  10. +++ b/CiParser.cs
  11. @@ -39,6 +39,7 @@ public partial class CiParser : CiLexer
  12.         globals.Add(CiBoolType.Value);
  13.         globals.Add(CiByteType.Value);
  14.         globals.Add(CiIntType.Value);
  15. +        globals.Add(CiFloatType.Value);
  16.         globals.Add(CiStringPtrType.Value);
  17.         globals.Add(new CiConst { Name = "true", Value = true, Type = CiBoolType.Value });
  18.         globals.Add(new CiConst { Name = "false", Value = false, Type = CiBoolType.Value });
  19. diff --git a/CiResolver.cs b/CiResolver.cs
  20. index 18dfe62..2857c0a 100644
  21. --- a/CiResolver.cs
  22. +++ b/CiResolver.cs
  23. @@ -172,6 +172,14 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
  24.                 return new CiConstExpr((object) (byte) (int) konst.Value);
  25.             return new CiCoercion { ResultType = expected, Inner = expr };
  26.         }
  27. +        if (expected == CiFloatType.Value && got == CiIntType.Value)
  28. +        {
  29. +            return new CiCoercion { ResultType = expected, Inner = expr };
  30. +        }
  31. +        if (expected == CiFloatType.Value && got == CiByteType.Value)
  32. +        {
  33. +            return new CiCoercion { ResultType = expected, Inner = expr };
  34. +        }
  35.         if (expected == CiStringPtrType.Value && (got == CiType.Null || got is CiStringType))
  36.             return expr;
  37.         if (expected is CiStringStorageType && got is CiStringType)
  38. @@ -564,6 +572,18 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
  39.             string b = GetConstString(right);
  40.             return new CiConstExpr(a + b);
  41.         }
  42. +        if ((expr.Op == CiToken.Plus
  43. +            || expr.Op == CiToken.Minus
  44. +            || expr.Op == CiToken.Asterisk
  45. +            || expr.Op == CiToken.Slash
  46. +            )
  47. +            && left.Type is CiFloatType || right.Type is CiFloatType)
  48. +        {
  49. +            expr.Left = Coerce(left, CiFloatType.Value);
  50. +            expr.Right = Coerce(right, CiFloatType.Value);
  51. +            expr.MyType = CiFloatType.Value;
  52. +            return expr;
  53. +        }
  54.         left = Coerce(left, CiIntType.Value);
  55.         right = Coerce(right, CiIntType.Value);
  56.         if (right is CiConstExpr) {
  57. @@ -635,6 +655,10 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
  58.             type = CiIntType.Value;
  59.             break;
  60.         }
  61. +        if (left.Type == CiFloatType.Value || right.Type == CiFloatType.Value)
  62. +        {
  63. +            type = CiFloatType.Value;
  64. +        }
  65.         expr.Left = Coerce(left, type);
  66.         expr.Right = Coerce(right, type);
  67.         CiConstExpr cleft = expr.Left as CiConstExpr;
  68. diff --git a/CiTree.cs b/CiTree.cs
  69. index 337a92f..a36822b 100644
  70. --- a/CiTree.cs
  71. +++ b/CiTree.cs
  72. @@ -151,6 +151,20 @@ public class CiIntType : CiType
  73.     }
  74.  }
  75.  
  76. +public class CiFloatType : CiType
  77. +{
  78. +    private CiFloatType() { }
  79. +    public static readonly CiFloatType Value = new CiFloatType { Name = "float" };
  80. +    public override Type DotNetType { get { return typeof(float); } }
  81. +    public override CiSymbol LookupMember(string name)
  82. +    {
  83. +        switch (name)
  84. +        {
  85. +            default: throw new ParseException("No member {0} in float", name);
  86. +        }
  87. +    }
  88. +}
  89. +
  90.  public abstract class CiStringType : CiType
  91.  {
  92.     public override Type DotNetType { get { return typeof(string); } }
  93. @@ -585,7 +599,8 @@ public class CiBinaryExpr : CiExpr
  94.     public CiExpr Left;
  95.     public CiToken Op;
  96.     public CiExpr Right;
  97. -   public override CiType Type { get { return CiIntType.Value; } }
  98. +    public CiType MyType = CiIntType.Value;
  99. +    public override CiType Type { get { return MyType; } }
  100.     public override bool HasSideEffect { get { return this.Left.HasSideEffect || this.Right.HasSideEffect; } }
  101.     public override CiExpr Accept(ICiExprVisitor v) { return v.Visit(this); }
  102.  }
  103. diff --git a/GenJs.cs b/GenJs.cs
  104. index ff77b9a..dd95e0a 100644
  105. --- a/GenJs.cs
  106. +++ b/GenJs.cs
  107. @@ -254,7 +254,7 @@ public class GenJs : SourceGenerator
  108.  
  109.     protected override void Write(CiBinaryExpr expr)
  110.     {
  111. -       if (expr.Op == CiToken.Slash) {
  112. +       if (expr.Op == CiToken.Slash && expr.Type != CiFloatType.Value) {
  113.             Write("Math.floor(");
  114.             WriteChild(3, expr.Left);
  115.             Write(" / ");
Advertisement
Add Comment
Please, Sign In to add comment