예제 #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);
        }
예제 #2
0
        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;
 }