Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CiParser.cs | 1 +
- CiResolver.cs | 24 ++++++++++++++++++++++++
- CiTree.cs | 17 ++++++++++++++++-
- GenJs.cs | 2 +-
- 4 files changed, 42 insertions(+), 2 deletions(-)
- diff --git a/CiParser.cs b/CiParser.cs
- index 9a595dd..7e6ef1b 100644
- --- a/CiParser.cs
- +++ b/CiParser.cs
- @@ -39,6 +39,7 @@ public partial class CiParser : CiLexer
- 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 });
- diff --git a/CiResolver.cs b/CiResolver.cs
- index 18dfe62..2857c0a 100644
- --- a/CiResolver.cs
- +++ b/CiResolver.cs
- @@ -172,6 +172,14 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
- 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)
- @@ -564,6 +572,18 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
- 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.MyType = CiFloatType.Value;
- + return expr;
- + }
- left = Coerce(left, CiIntType.Value);
- right = Coerce(right, CiIntType.Value);
- if (right is CiConstExpr) {
- @@ -635,6 +655,10 @@ public class CiResolver : ICiSymbolVisitor, ICiTypeVisitor, ICiExprVisitor, ICiS
- 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;
- diff --git a/CiTree.cs b/CiTree.cs
- index 337a92f..a36822b 100644
- --- a/CiTree.cs
- +++ b/CiTree.cs
- @@ -151,6 +151,20 @@ public class CiIntType : CiType
- }
- }
- +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); } }
- @@ -585,7 +599,8 @@ public class CiBinaryExpr : CiExpr
- public CiExpr Left;
- public CiToken Op;
- public CiExpr Right;
- - public override CiType Type { get { return CiIntType.Value; } }
- + public CiType MyType = CiIntType.Value;
- + public override CiType Type { get { return MyType; } }
- public override bool HasSideEffect { get { return this.Left.HasSideEffect || this.Right.HasSideEffect; } }
- public override CiExpr Accept(ICiExprVisitor v) { return v.Visit(this); }
- }
- diff --git a/GenJs.cs b/GenJs.cs
- index ff77b9a..dd95e0a 100644
- --- a/GenJs.cs
- +++ b/GenJs.cs
- @@ -254,7 +254,7 @@ public class GenJs : SourceGenerator
- protected override void Write(CiBinaryExpr expr)
- {
- - if (expr.Op == CiToken.Slash) {
- + if (expr.Op == CiToken.Slash && expr.Type != CiFloatType.Value) {
- Write("Math.floor(");
- WriteChild(3, expr.Left);
- Write(" / ");
Advertisement
Add Comment
Please, Sign In to add comment