private static ExpressionNode ParseLiteral(RuleContext rule)
        {
            RuleContext realLiteral = (RuleContext)rule.GetChild(0);

            switch (realLiteral.RuleIndex)
            {
            case CrawlParser.RULE_string_literal:
                return(CrawlSyntaxNode.StringLiteral(realLiteral.SourceInterval, CrawlType.UnspecifiedType, Unescape(realLiteral.GetText())));

            case CrawlParser.RULE_integer_literal:
                return(CrawlSyntaxNode.IntegerLiteral(realLiteral.SourceInterval, CrawlType.UnspecifiedType, int.Parse(realLiteral.GetText())));

            case CrawlParser.RULE_boolean_literal:
                return(CrawlSyntaxNode.BooleanLiteral(realLiteral.SourceInterval, CrawlType.UnspecifiedType, (realLiteral.GetText()) == "true"));

            case CrawlParser.RULE_real_literal:
                return(CrawlSyntaxNode.RealLiteral(realLiteral.SourceInterval, CrawlType.UnspecifiedType, double.Parse(realLiteral.GetText())));

            case CrawlParser.RULE_null_literal:
                return(CrawlSyntaxNode.NullLiteral(realLiteral.SourceInterval, CrawlSimpleType.Tom));

            default:
                throw new NotImplementedException("Strange literal type");
            }
        }
Example #2
0
        private static LiteralNode FoldBinary(LiteralNode op1, ExpressionType expressionType, LiteralNode op2)
        {
            var val1 = GetNumber(op1);
            var val2 = GetNumber(op2);

            bool result;

            switch (expressionType)
            {
            case ExpressionType.Greater:
                result = val1 > val2;
                break;

            case ExpressionType.GreaterEqual:
                result = val1 >= val2;
                break;

            case ExpressionType.Less:
                result = val1 < val2;
                break;

            case ExpressionType.LessEqual:
                result = val1 <= val2;
                break;

            case ExpressionType.Equal:
                result = Math.Abs(val1 - val2) < Double.Epsilon;
                break;

            case ExpressionType.NotEqual:
                result = Math.Abs(val1 - val2) > Double.Epsilon;
                break;

            default:
                throw new Exception("Wait this isn't a binary operator!");
            }

            Interval interval = new Interval(op1.Interval.a, op2.Interval.b);

            return(CrawlSyntaxNode.BooleanLiteral(interval, CrawlSimpleType.Bool, result));
        }
Example #3
0
 private LiteralNode FoldUnary(ExpressionNode target, ExpressionType expressionType)
 {
     if (expressionType == ExpressionType.Negate)
     {
         var asReal = target as RealLiteralNode;
         if (asReal != null)
         {
             double resultValue = -asReal.Value;
             return(CrawlSyntaxNode.RealLiteral(target.Interval, CrawlSimpleType.Kommatal, resultValue));
         }
         var asInt = target as IntegerLiteralNode;
         if (asInt != null)
         {
             int resultValue = -asInt.Value;
             return(CrawlSyntaxNode.IntegerLiteral(target.Interval, CrawlSimpleType.Tal, resultValue));
         }
     }
     //else if (expressionType == ExpressionType.Not)
     {
         var  asBool      = (BooleanLiteralNode)target;
         bool resultValue = !asBool.Value;
         return(CrawlSyntaxNode.BooleanLiteral(target.Interval, CrawlSimpleType.Bool, resultValue));
     }
 }
Example #4
0
        private static LiteralNode FoldMultuPair(LiteralNode op1, ExpressionType expressionType, LiteralNode op2)
        {
            if (expressionType == ExpressionType.Add)
            {
                return(FoldTheDreadedAddition(op1, op2));
            }

            Interval interval = new Interval(op1.Interval.a, op2.Interval.b);

            IntegerLiteralNode int1 = op1 as IntegerLiteralNode;
            IntegerLiteralNode int2 = op2 as IntegerLiteralNode;

            if (int1 != null && int2 != null)
            {
                int result;
                switch (expressionType)
                {
                case ExpressionType.Divide:
                    result = int1.Value / int2.Value;
                    break;

                case ExpressionType.Modulous:
                    result = int1.Value % int2.Value;
                    break;

                case ExpressionType.Multiply:
                    result = int1.Value * int2.Value;
                    break;

                case ExpressionType.Power:
                    result = (int)Math.Pow(int1.Value, int2.Value);
                    break;

                default:
                    throw new Exception("Operator and/or type entirely unexpected!");
                }
                return(CrawlSyntaxNode.IntegerLiteral(interval, CrawlSimpleType.Tal, result));
            }
            BooleanLiteralNode boo1 = op1 as BooleanLiteralNode;
            BooleanLiteralNode boo2 = op2 as BooleanLiteralNode;

            if (boo1 != null && boo2 != null)
            {
                bool result;
                switch (expressionType)
                {
                case ExpressionType.ShortCircuitAnd:
                    result = boo1.Value && boo2.Value;
                    break;

                case ExpressionType.ShortCircuitOr:
                    result = boo1.Value || boo2.Value;
                    break;

                default:
                    throw new Exception("Operator and/or type entirely unexpected!");
                }

                return(CrawlSyntaxNode.BooleanLiteral(interval, CrawlSimpleType.Bool, result));
            }

            //Else one or both operands are kommatal, so both are casted to that.
            double rea1 = GetNumber(op1);
            double rea2 = GetNumber(op2);
            double res;

            switch (expressionType)
            {
            case ExpressionType.Divide:
                res = rea1 / rea2;
                break;

            case ExpressionType.Modulous:
                res = rea1 % rea2;
                break;

            case ExpressionType.Multiply:
                res = rea1 * rea2;
                break;

            case ExpressionType.Power:
                res = Math.Pow(rea1, rea2);
                break;

            default:
                throw new Exception("Operator and/or type entirely unexpected!");
            }
            return(CrawlSyntaxNode.RealLiteral(interval, CrawlSimpleType.Tal, res));
        }