Ejemplo n.º 1
0
        public void ExitComparison(QueryParser.ComparisonContext context)
        {
            var opToken = context.REL_OP();

            if (opToken == null)
            {
                return;
            }

            var op    = opToken.Symbol;
            var right = _expressions.Pop();
            var left  = _expressions.Pop();

            BinaryOperatorExpression value = null;

            switch (op.Text)
            {
            case "<":
                value = new LessThanOperator(op.Line, op.Column, left, right);
                break;

            case "<=":
                value = new LessThanOrEqualOperator(op.Line, op.Column, left, right);
                break;

            case "<>":
            case "!=":
                value = new NotEqualOperator(op.Line, op.Column, left, right);
                break;

            case "==":
            case "=":
                value = new EqualOperator(op.Line, op.Column, left, right);
                break;

            case ">":
                value = new GreaterThanOperator(op.Line, op.Column, left, right);
                break;

            case ">=":
                value = new GreaterThanOrEqualOperator(op.Line, op.Column, left, right);
                break;
            }

            Trace.Assert(value != null, "Invalid comparison operator " + op.Text);

            _expressions.Push(value);
        }
Ejemplo n.º 2
0
        public void CreateExpressionTest1()
        {
            // Arrange
            Expression parameterExpression = Expression.Parameter(typeof(Employee), "sample");
            Expression expressionProperty  = Expression.Property(parameterExpression, nameof(Employee.Age));
            Expression expressionConstant  = Expression.Constant(20);

            Expression expectedExpression = Expression.GreaterThanOrEqual(expressionProperty, expressionConstant);

            var _operator = new GreaterThanOrEqualOperator();

            // Act
            Expression resultExpression = _operator.CreateExpression(expressionProperty, expressionConstant);

            // Assert
            Assert.AreEqual(expectedExpression.ToString(), resultExpression.ToString(), "Expression not correct");
        }
Ejemplo n.º 3
0
        private static ExpTreeNode GetAsExpressionTree(this ParseTreeNode parsedNode, ExpTreeNode parentExpNode)
        {
            //The current node is a variable / value token. Create a value node and return it back
            if (!parsedNode.ChildNodes.Any())
            {
                switch (parsedNode.Term.Name)
                {
                case "variable":
                {
                    var varName = parsedNode.FindTokenAndGetValue <string>();
                    return(new VariableValue(varName, parentExpNode));
                }

                case "boolean":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <bool>(), parentExpNode));

                case "integer":
                case "decimal":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <double>(), parentExpNode));

                case "SingleQuoteString":
                case "DoubleQuoteString":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <string>(), parentExpNode));

                default:
                    throw parsedNode.GetException($"Invalid token type '{parsedNode.Term.Name}' in expression");
                }
            }

            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 1)
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[0], parentExpNode));
            }

            //Ignore parenthesis, the middle non-terminal is what we want
            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 3 && parsedNode.ChildNodes[0]?.Token?.Text == "(")
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[1], parentExpNode));
            }

            //Binary operator
            else if (parsedNode.ChildNodes.Count == 3)
            {
                BinaryOperatorBase binaryOp;
                var opStr = parsedNode.ChildNodes[1].FindToken().ValueString;
                switch (opStr)
                {
                // Math
                case "+":
                    binaryOp = new AdditionOperator(parentExpNode);
                    break;

                case "-":
                    binaryOp = new SubtractionOperator(parentExpNode);
                    break;

                case "*":
                    binaryOp = new MultiplacationOperator(parentExpNode);
                    break;

                case "/":
                    binaryOp = new DivisionOperator(parentExpNode);
                    break;

                case "%":
                    binaryOp = new ModulusOperator(parentExpNode);
                    break;

                case "^":
                    binaryOp = new PowerOperator(parentExpNode);
                    break;

                // Bool
                case "~==":
                    binaryOp = new LooseEqualsOperator(parentExpNode);
                    break;

                case "~!=":
                    binaryOp = new LooseNotEqualsOperator(parentExpNode);
                    break;

                case "==":
                    binaryOp = new EqualsOperator(parentExpNode);
                    break;

                case "!=":
                    binaryOp = new NotEqualsOperator(parentExpNode);
                    break;

                case "like":
                    binaryOp = new LikeOperator(parentExpNode);
                    break;

                case "match":
                    binaryOp = new MatchOperator(parentExpNode);
                    break;

                case ">":
                    binaryOp = new GreaterThanOperator(parentExpNode);
                    break;

                case ">=":
                    binaryOp = new GreaterThanOrEqualOperator(parentExpNode);
                    break;

                case "<":
                    binaryOp = new LessThanOperator(parentExpNode);
                    break;

                case "<=":
                    binaryOp = new LessThanOrEqualOperator(parentExpNode);
                    break;

                case "&&":
                    binaryOp = new AndOperator(parentExpNode);
                    break;

                case "||":
                    binaryOp = new OrOperator(parentExpNode);
                    break;

                default:
                    throw parsedNode.ChildNodes[1].GetException($"Unrecognized operator '{opStr}'");
                }

                binaryOp.LeftOperand  = GetAsExpressionTree(parsedNode.ChildNodes[0], binaryOp);
                binaryOp.RightOperand = GetAsExpressionTree(parsedNode.ChildNodes[2], binaryOp);

                //Optimize
                if (binaryOp.LeftOperand is LiteralValue && binaryOp.RightOperand is LiteralValue)
                {
                    return(new LiteralValue(binaryOp.Eval(), parentExpNode));
                }

                return(binaryOp);
            }

            // Unary operator
            else if (parsedNode.ChildNodes.Count == 2)
            {
                var opVal = parsedNode.ChildNodes[0].FindToken().Value;
                UnaryOperatorBase unaryOp;
                if (parsedNode.ChildNodes[0].FindToken().Value is TypeCode convertType)
                {
                    unaryOp = new TypeConversionOperator(convertType, parentExpNode);
                }
                else
                {
                    var opStr = opVal.ToString();
                    switch (opStr)
                    {
                    case "!":
                        unaryOp = new NotOperator(parentExpNode);
                        break;

                    //Property Checking
                    case "lengthof":
                        unaryOp = new LengthOperator(parentExpNode);
                        break;

                    case "typeof":
                        unaryOp = new TypeOperator(parentExpNode);
                        break;

                    default:
                        unaryOp = new GenericUnaryMathFunctionOperator(opStr, parentExpNode);
                        break;
                    }
                }

                unaryOp.Operand = GetAsExpressionTree(parsedNode.ChildNodes[1], unaryOp);

                //Optimize
                if (unaryOp.Operand is LiteralValue)
                {
                    return(new LiteralValue(unaryOp.Eval(), parentExpNode));
                }

                return(unaryOp);
            }

            else
            {
                throw parsedNode.GetException($"Invalid number of tokens ({parsedNode.ChildNodes.Count})");
            }
        }
Ejemplo n.º 4
0
 public Bitmap Visit(GreaterThanOrEqualOperator expr)
 {
     return(ComputeFunction(expr));
 }
 public virtual void Visit(GreaterThanOrEqualOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Visit(binaryOperator.Right);
 }
Ejemplo n.º 6
0
 public override void Visit(GreaterThanOrEqualOperator binaryOperator)
 {
     WriteBinaryExpression(binaryOperator, ">=");
 }
 public override void Visit(GreaterThanOrEqualOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Write(" >= ");
     Visit(binaryOperator.Right);
 }