Example #1
0
        protected override object DoVisit(BinaryExpression binaryExpression)
        {
            dynamic leftOperand = Visit(binaryExpression.LeftExpression);
            dynamic rightOperand = Visit(binaryExpression.RightExpression);

            if ((object)leftOperand == Error || (object)rightOperand == Error)
            {
                return Error;
            }

            try
            {
                switch (binaryExpression.Operator.Token)
                {
                    case '=':
                        return leftOperand == rightOperand;

                    case '!':
                        return leftOperand != rightOperand;

                    case '>':
                        return leftOperand > rightOperand;

                    case '<':
                        return leftOperand < rightOperand;

                    case ',':
                        return leftOperand || rightOperand;

                    case Token.LessOrEqual:
                        return leftOperand <= rightOperand;

                    case Token.GreaterOrEqual:
                        return leftOperand >= rightOperand;

                    case Token.And:
                        return leftOperand && rightOperand;

                    default:
                        this.errorLogger.LogError(
                            binaryExpression.Operator.Location,
                            Utilities.InvariantFormat("Invalid operator \"{0}\".", binaryExpression.Operator.Text));

                        return Error;
                }
            }
            catch
            {
                this.errorLogger.LogError(
                    binaryExpression.Operator.Location,
                    string.Format(
                        "Operator \"{0}\" cannot be applied to operands of type \"{1}\" and \"{2}\".",
                        binaryExpression.Operator.Text,
                        leftOperand.GetType(),
                        rightOperand.GetType()));

                return Error;
            }
        }
Example #2
0
        private Expression ParseCondition(Expression implicitOperand)
        {
            if (this.nextTokenInfo.Token == Token.Else)
            {
                this.Consume();
                return new ConstantExpression(this.currentTokenInfo.Location, true, "else");
            }

            Expression condition = null;

            do
            {
                Expression andExpression = null;

                do
                {
                    if (this.nextTokenInfo.Token == Token.Else)
                    {
                        throw new FormattingException(this.nextTokenInfo.Location, "\"else\" must be used alone.");
                    }

                    Operator @operator = this.ParseOperator();

                    if ([email protected])
                    {
                        throw new FormattingException(@operator.Location, "Expected binary operator.");
                    }

                    Expression rightOperand = this.ParseUnaryExpression();

                    var expression = new BinaryExpression(
                        Location.FromRange(@operator, rightOperand), @operator, implicitOperand, rightOperand);

                    andExpression = andExpression == null
                                        ? expression
                                        : new BinaryExpression(
                                              Location.FromRange(andExpression, expression),
                                              new Operator(Location.Unknown, Token.And, string.Empty),
                                              andExpression,
                                              expression);
                }
                while (this.nextTokenInfo.Token != ':' && this.nextTokenInfo.Token != ',');

                this.Accept(',');

                condition = condition == null
                                ? andExpression
                                : new BinaryExpression(
                                      Location.FromRange(condition, andExpression),
                                      new Operator(this.currentTokenInfo.Location, ','),
                                      condition,
                                      andExpression);
            }
            while (this.nextTokenInfo.Token != ':');

            return condition;
        }