public BoundUnaryExpression(SyntaxNode syntax, BoundUnaryOperator @operator, BoundExpression expression) : base(BoundNodeKind.UnaryExpression, syntax) { this.Operator = @operator; this.Expression = expression; this.ConstantValue = ConstantFolding.Fold(@operator, expression); }
public static BoundUnaryExpression Unary(SyntaxNode syntax, SyntaxKind kind, BoundExpression expr) { var op = BoundUnaryOperator.Bind(kind, expr.Type) !; return(new BoundUnaryExpression(syntax, op, expr)); }
private BoundExpression Negate(BoundExpression condition) { if (condition is BoundLiteralExpression literal) { var value = (bool)literal.Value; return(new BoundLiteralExpression(condition.Syntax, !value)); } var op = BoundUnaryOperator.Bind(SyntaxKind.ExmToken, BuiltinTypes.Bool) !; return(new BoundUnaryExpression(condition.Syntax, op, condition)); }
public static BoundConstant?Fold(BoundUnaryOperator op, BoundExpression rhs) { if (rhs.ConstantValue != null) { var value = rhs.ConstantValue.Value; switch (op.Kind) { case BoundUnaryOperatorKind.Identity: return(new BoundConstant(/*(int)*/ value)); case BoundUnaryOperatorKind.Negation: return(new BoundConstant(-(int)value)); case BoundUnaryOperatorKind.LogicalNegation: return(new BoundConstant(!(bool)value)); default: throw new Exception($"Unexpected unary operator '{op.Kind}' for type '{rhs.Type}'"); } } return(null); }