Exemplo n.º 1
0
        protected virtual BoundExpression RewriteUnaryExpression(BoundUnaryExpression expression)
        {
            var operand = RewriteExpression(expression.Operand);

            if (operand == expression.Operand)
            {
                return(expression);
            }

            return(new BoundUnaryExpression(expression.Op, operand));
        }
Exemplo n.º 2
0
    protected override BoundExpression RewriteUnaryExpression(BoundUnaryExpression node)
    {
        var rewrittenOp = RewriteExpression(node.Operand);
        var operand     = IsSimpleNode(rewrittenOp) ? rewrittenOp : CreateTemporary(rewrittenOp);
        var @operator   = RewriteUnaryOperator(node.Operator);

        if (node.Operand == operand && node.Operator == @operator)
        {
            return(node);
        }

        return(new BoundUnaryExpression(node.Syntax, @operator, operand));
    }
Exemplo n.º 3
0
        private object EvaluateUnaryExpression(BoundUnaryExpression u)
        {
            var operand = EvaluateExpression(u.Operand);

            if (u.Op.Type == null)
            {
                var conversion = Conversion.Classify(u.Operand.Type, u.Type);
                if (!conversion.IsImplicit)
                {
                    throw new Exception($"Can not implicitly cast type {u.Operand.Type} to {u.Type}");
                }
            }

            return(Conversion.Convert(u.Type, u.Op.Operate(Conversion.Convert(u.Type, operand)))); // Convert twice incase C# changes the type after the operation
        }
Exemplo n.º 4
0
        private object EvaluateUnaryExpression(BoundUnaryExpression u)
        {
            var operand = this.EvaluateExpression(u.Expression) !;

            switch (u.Operator.Kind)
            {
            case BoundUnaryOperatorKind.Identity:        return((int)operand);

            case BoundUnaryOperatorKind.Negation:        return(-(int)operand);

            case BoundUnaryOperatorKind.LogicalNegation: return(!(bool)operand);

            default:
                throw new Exception($"Unexpected unary operator {u.Operator}");
            }
        }
Exemplo n.º 5
0
        private void EmitUnaryExpression(BoundUnaryExpression expression)
        {
            switch (expression.Kind)
            {
            case TokenKind.Minus:
            {
                this.EmitExpression(expression.Expression);
                this.instructions.Add(new UnaryMinusInstruction(expression.Syntax.Range));
                break;
            }

            default:
            {
                throw ExceptionUtilities.UnexpectedValue(expression.Kind);
            }
            }
        }
Exemplo n.º 6
0
        private Expression BuildUnaryExpression(BoundUnaryExpression expression)
        {
            var liftedInput        = BuildCachedExpression(expression.Expression);
            var nullableResultType = expression.Type.GetNullableType();
            var signature          = expression.Result.Best.Signature;

            return(Expression.Condition(
                       BuildNullCheck(liftedInput),
                       BuildNullValue(nullableResultType),
                       BuildLiftedExpression(
                           BuildUnaryExpression(
                               signature,
                               BuildLoweredExpression(liftedInput)
                               )
                           )
                       ));
        }
Exemplo n.º 7
0
        private void OutputUnaryExpression(BoundUnaryExpression node, string prefix)
        {
            builder.AddFragment(new OutputFragment(prefix, DefaultColour));
            builder.AddFragment(new OutputFragment(node.Op.TokenText, DefaultColour));
            bool isBinaryExpression = node.Operand is BoundBinaryExpression;

            if (isBinaryExpression)
            {
                builder.AddFragment(new OutputFragment("(", DefaultColour));
            }

            Output(node.Operand, string.Empty);

            if (isBinaryExpression)
            {
                builder.AddFragment(new OutputFragment(")", DefaultColour));
            }
        }
Exemplo n.º 8
0
 protected override object EvaluateExpression(BoundExpression node)
 {
     try
     {
         BoundLiteralExpression boundLiteralExpression = (BoundLiteralExpression)node;
         return(EvaluateLiteralExpression(boundLiteralExpression));
     }
     catch
     {
         try
         {
             BoundVariableExpression boundVariableExpression = (BoundVariableExpression)node;
             return(EvaluateVariableExpression(boundVariableExpression));
         }
         catch
         {
             try
             {
                 BoundAssignmentExpression boundAssignmentExpression = (BoundAssignmentExpression)node;
                 return(EvaluateAssignmentExpression(boundAssignmentExpression));
             }
             catch
             {
                 try
                 {
                     BoundUnaryExpression boundUnaryExpression = (BoundUnaryExpression)node;
                     return(EvaluateUnaryExpression(boundUnaryExpression));
                 }
                 catch
                 {
                     try
                     {
                         BoundBinaryExpression boundBinaryExpression = (BoundBinaryExpression)node;
                         return(EvaluateBinaryExpression(boundBinaryExpression));
                     }
                     catch
                     {
                         return(null);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 9
0
        private object Evaluate(BoundUnaryExpression unaryExpression)
        {
            var operand = EvaluateExpression(unaryExpression.Operand);

            switch (unaryExpression.Op.Kind)
            {
            case BoundUnaryOperatorKind.Negation:
                return(-(int)operand);

            case BoundUnaryOperatorKind.Identity:
                return((int)operand);

            case BoundUnaryOperatorKind.LogicalNegation:
                return(!(bool)operand);

            case BoundUnaryOperatorKind.BitwiseNegation:
                return(~(int)operand);

            default:
                throw new ArgumentOutOfRangeException(nameof(unaryExpression.Op), unaryExpression.Op, null);
            }
        }
Exemplo n.º 10
0
        private object EvaluateUnaryExpression(BoundUnaryExpression us)
        {
            var operand = EvaluateExpression(us.Operand);

            switch (us.Op.Kind)
            {
            case BoundUnaryOperatorKind.Negation:
                return(-(int)operand);

            case BoundUnaryOperatorKind.Identity:
                return((int)operand);

            case BoundUnaryOperatorKind.LogicalNegation:
                return(!(bool)operand);

            case BoundUnaryOperatorKind.OnesComplement:
                return(~(int)operand);

            default:
                throw new Exception($"Unexpected unary operator {us.Kind}");
            }
        }
Exemplo n.º 11
0
        private object EvaluateUnaryExpression(BoundUnaryExpression u)
        {
            var operand = EvaluateExpression(u.Operand);

            switch (u.Op.Kind)
            {
            case BoundUnaryOperatorKind.Identity:
                return((int)operand);

            case BoundUnaryOperatorKind.Negation:
                return(-(int)operand);

            case BoundUnaryOperatorKind.LogicalNegation:
                return(!(bool)operand);

            case BoundUnaryOperatorKind.OnesComplement:
                return(~(int)operand);

            // For now we don't support DereferenceOf or ReferenceOf.
            default:
                throw new Exception($"Unexpected unary operator {u.Op}");
            }
        }
Exemplo n.º 12
0
    public override BoundConstant?VisitUnaryExpression(BoundUnaryExpression node)
    {
        var @operator       = node.Operator;
        var operandConstant = node.Operand.Accept(this);

        if (operandConstant == null)
        {
            return(null);
        }

        var constant = operandConstant.Value;

        return(@operator.Kind switch
        {
            BoundUnaryOperatorKind.Identity => operandConstant,
            BoundUnaryOperatorKind.Negation => new BoundConstant(-(int)constant),
            BoundUnaryOperatorKind.LogicalNegation => new BoundConstant(!(bool)constant),
            BoundUnaryOperatorKind.BitwiseNegation => new BoundConstant(~(int)constant),
            _
            => throw new ArgumentOutOfRangeException(
                nameof(node),
                $"Unknown operator kind '{@operator.Kind}'"
                )
        });
Exemplo n.º 13
0
 private void EmitUnaryExpression(ILProcessor ilProcessor, BoundUnaryExpression node)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 14
0
 private static void WriteBoundUnaryExpression(this IndentedTextWriter writer, BoundUnaryExpression node)
 {
     writer.ColorWrite("(");
     writer.Write(node.Op.GetText());
     writer.WriteBoundNode(node.Expression);
     writer.ColorWrite(")");
 }
Exemplo n.º 15
0
 protected virtual void VisitUnaryExpression(BoundUnaryExpression node)
 {
     VisitExpression(node.Expression);
 }
Exemplo n.º 16
0
 public virtual void VisitUnaryExpression(BoundUnaryExpression node) =>
 this.DefaultVisit(node);
Exemplo n.º 17
0
        private object EvaluateUnaryExpression(BoundUnaryExpression expression)
        {
            var operand = EvaluateExpression(expression.Operand);

            switch (expression.Operator.Kind)
            {
            case BoundUnaryOperatorKind.Negation:
                return(-(int)operand);

            case BoundUnaryOperatorKind.Identity:
                return((int)operand);

            case BoundUnaryOperatorKind.LogicalNegation:
                return(!(bool)operand);

            case BoundUnaryOperatorKind.PrefixIncrement:
                var prefixIncrementValue = (int)operand + 1;

                if (expression.Operand.Kind == BoundNodeKind.VariableExpression)
                {
                    var variableExpression = (BoundVariableExpression)expression.Operand;

                    _variables[variableExpression.Variable] = prefixIncrementValue;
                }

                return(prefixIncrementValue);

            case BoundUnaryOperatorKind.PrefixDecrement:
                var prefixDecrementValue = (int)operand - 1;

                if (expression.Operand.Kind == BoundNodeKind.VariableExpression)
                {
                    var variableExpression = (BoundVariableExpression)expression.Operand;

                    _variables[variableExpression.Variable] = prefixDecrementValue;
                }

                return(prefixDecrementValue);

            case BoundUnaryOperatorKind.PostfixIncrement:
                var postfixIncrementValue = (int)operand + 1;

                if (expression.Operand.Kind == BoundNodeKind.VariableExpression)
                {
                    var variableExpression = (BoundVariableExpression)expression.Operand;

                    _variables[variableExpression.Variable] = postfixIncrementValue;

                    return(operand);
                }

                return(postfixIncrementValue);

            case BoundUnaryOperatorKind.PostfixDecrement:
                var postfixDecrementValue = (int)operand - 1;

                if (expression.Operand.Kind == BoundNodeKind.VariableExpression)
                {
                    var variableExpression = (BoundVariableExpression)expression.Operand;

                    _variables[variableExpression.Variable] = postfixDecrementValue;

                    return(operand);
                }

                return(postfixDecrementValue);

            case BoundUnaryOperatorKind.OnesCompliment:
                return(~(int)operand);

            default:
                throw new Exception($"Unsupported unary operator {expression.Operator.Kind}.");
            }
        }