public CodeUnaryOperatorExpression(CodeUnaryOperatorType op, CodeExpression operand) { Ensure.That(nameof(operand)).IsNotNull(operand); Operator = op; Operand = operand; }
/// <summary> /// Returns the symbol for the provided operator, and sets <paramref name="isOperatorAfterExpression"/> /// </summary> /// <param name="operatorType"></param> /// <param name="isOperatorAfterExpression">will be set to true if the provided operator symbol should be written /// after the expression</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static string UnaryOperatorSymbol(CodeUnaryOperatorType operatorType, out bool isOperatorAfterExpression) { isOperatorAfterExpression = false; switch (operatorType) { case CodeUnaryOperatorType.BitwiseNot: return("~"); case CodeUnaryOperatorType.BooleanNot: return("!"); case CodeUnaryOperatorType.Plus: return("+"); case CodeUnaryOperatorType.Negation: return("-"); case CodeUnaryOperatorType.PostIncrement: isOperatorAfterExpression = true; goto case CodeUnaryOperatorType.PreIncrement; case CodeUnaryOperatorType.PreIncrement: return("++"); case CodeUnaryOperatorType.PostDecrement: isOperatorAfterExpression = true; goto case CodeUnaryOperatorType.PreDecrement; case CodeUnaryOperatorType.PreDecrement: return("--"); default: throw new ArgumentOutOfRangeException(nameof(operatorType), operatorType, null); } }
public override CodeExpression CreateUnaryOperatorExpression(CodeUnaryOperatorType operatorType, CodeExpression expression) { StringBuilder sb = new StringBuilder(); switch (operatorType) { case CodeUnaryOperatorType.BooleanNot: sb.Append("(NOT ("); break; case CodeUnaryOperatorType.Negate: sb.Append("(- ("); break; case CodeUnaryOperatorType.Plus: sb.Append("(+ ("); break; } StringWriter writer = new StringWriter(sb); Generator.GenerateCodeFromExpression(expression, writer, null); sb.Append("))"); return(new CodeSnippetExpression(sb.ToString())); }
/// <summary> /// Returns the symbol for the provided operator /// </summary> /// <param name="operator"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static string UnaryOperatorSymbol(CodeUnaryOperatorType @operator) { switch (@operator) { case CodeUnaryOperatorType.BitwiseNot: case CodeUnaryOperatorType.BooleanNot: return("Not "); case CodeUnaryOperatorType.Plus: return("+"); case CodeUnaryOperatorType.Negation: return("-"); case CodeUnaryOperatorType.PreIncrement: case CodeUnaryOperatorType.PostIncrement: case CodeUnaryOperatorType.PreDecrement: case CodeUnaryOperatorType.PostDecrement: return(null); default: throw new ArgumentOutOfRangeException(nameof(@operator), @operator, null); } }
/// <summary> /// Constructor /// </summary> /// <param name="expression"></param> /// <param name="op"></param> public CodeUnaryOperatorExpression(CodeExpression expression, CodeUnaryOperatorType op) { Expression = expression; Operator = op; }
public abstract CodeExpression CreateUnaryOperatorExpression(CodeUnaryOperatorType operatorType, CodeExpression expression);
public static CodeUnaryOperatorExpression UnaryOp(this CodeExpression operand, CodeUnaryOperatorType op) => new CodeUnaryOperatorExpression(op, operand);