コード例 #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
ファイル: BoundUnaryExpression.cs プロジェクト: CBenghi/minsk
 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;
 }