示例#1
0
        public static BoundConstant?ComputeConstant(BoundUnaryOperator op, BoundExpression operand)
        {
            if (operand.ConstantValue != null)
            {
                switch (op.Kind)
                {
                case BoundUnaryOperatorKind.Identity:
                    return(new BoundConstant((int)operand.ConstantValue.Value));

                case BoundUnaryOperatorKind.Negation:
                    return(new BoundConstant(-(int)operand.ConstantValue.Value));

                case BoundUnaryOperatorKind.LogicalNegation:
                    return(new BoundConstant(!(bool)operand.ConstantValue.Value));

                case BoundUnaryOperatorKind.OnesComplement:
                    return(new BoundConstant(~(int)operand.ConstantValue.Value));

                default:
                    throw new Exception($"Unexpected unary operator {op.Kind}");
                }
            }

            return(null);
        }
        public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand)
        {
            Op      = op;
            Operand = operand;

            Operand.Parent = this;
        }
示例#3
0
 public BoundUnaryExpression(SyntaxNode syntax, BoundUnaryOperator op, BoundExpression operand)
     : base(syntax)
 {
     Op            = op;
     Operand       = operand;
     ConstantValue = ConstantFolding.Fold(op, operand);
 }
示例#4
0
        public static BoundUnaryExpression Not(SyntaxNode syntax, BoundExpression condition)
        {
            Debug.Assert(condition.Type == TypeSymbol.Bool);

            var op = BoundUnaryOperator.Bind(SyntaxKind.BangToken, TypeSymbol.Bool);

            Debug.Assert(op != null);
            return(new BoundUnaryExpression(syntax, op, condition));
        }
示例#5
0
            private BoundExpression Negate(BoundExpression condition)
            {
                if (condition is BoundLiteralExpression literal)
                {
                    var value = (bool)literal.Value;
                    return(new BoundLiteralExpression(!value));
                }

                var op = BoundUnaryOperator.Bind(SyntaxKind.BangToken, TypeSymbol.Bool);

                return(new BoundUnaryExpression(op, condition));
            }
示例#6
0
        private BoundExpression BindUnaryExpression(UnaryExpressionSyntax syntax)
        {
            var boundOperand  = BindExpression(syntax.Operand);
            var boundOperator = BoundUnaryOperator.Bind(syntax.OperatorToken.Kind, boundOperand.Type);

            if (boundOperator == null)
            {
                _diagnostics.ReportUndefinedUnaryOperator(syntax.OperatorToken.Span, syntax.OperatorToken.Text, boundOperand.Type);
                return(boundOperand);
            }
            return(new BoundUnaryExpression(boundOperator, boundOperand));
        }
示例#7
0
        private BoundExpression Negate(BoundExpression expression)
        {
            if (expression is BoundLiteralExpression literal)
            {
                var value = (bool)literal.Value;
                return(new BoundLiteralExpression(!value));
            }

            var negate = BoundUnaryOperator.Bind(TokenKind.Bang, TypeSymbol.Bool);

            return(new BoundUnaryExpression(negate, expression));
        }
示例#8
0
        private BoundExpression BindUnaryExpression(UnaryExpressionSyntax syntax)
        {
            var boundOperand  = BindExpression(syntax.Operand);
            var boundOperator = BoundUnaryOperator.Bind(syntax.OperatorToken.Kind, boundOperand.Type);

            if (boundOperator == null)
            {
                _diagnostics.Add($"Unary operator '{syntax.OperatorToken.Text}' is not defined for type {boundOperand.Type}.");
                return(boundOperand);
            }

            return(new BoundUnaryExpression(boundOperator, boundOperand));
        }
示例#9
0
 public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand)
 {
     Op      = op;
     Operand = operand;
 }
示例#10
0
 public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand)
 {
     Op            = op;
     Operand       = operand;
     ConstantValue = ConstantFolding.ComputeConstant(op, operand);
 }
示例#11
0
 public BoundUnaryExpression(BoundUnaryOperator @operator, BoundExpression operand)
 {
     Operator = @operator;
     Operand  = operand;
 }