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; }
public BoundUnaryExpression(SyntaxNode syntax, BoundUnaryOperator op, BoundExpression operand) : base(syntax) { Op = op; Operand = operand; ConstantValue = ConstantFolding.Fold(op, operand); }
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)); }
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)); }
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)); }
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)); }
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)); }
public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand) { Op = op; Operand = operand; }
public BoundUnaryExpression(BoundUnaryOperator op, BoundExpression operand) { Op = op; Operand = operand; ConstantValue = ConstantFolding.ComputeConstant(op, operand); }
public BoundUnaryExpression(BoundUnaryOperator @operator, BoundExpression operand) { Operator = @operator; Operand = operand; }