/// <summary> /// Reads a conditional expression. /// </summary> /// <param name="leftHandSide"> /// The expression on the left hand side of the operator. /// </param> /// <param name="previousPrecedence"> /// The precedence of the expression just before this one. /// </param> /// <param name="unsafeCode"> /// Indicates whether the code being parsed resides in an unsafe code block. /// </param> /// <returns> /// Returns the expression. /// </returns> private ConditionalExpression GetConditionalExpression(Expression leftHandSide, ExpressionPrecedence previousPrecedence, bool unsafeCode) { Param.AssertNotNull(leftHandSide, "leftHandSide"); Param.Ignore(unsafeCode); Param.Ignore(previousPrecedence); ConditionalExpression expression = null; if (CheckPrecedence(previousPrecedence, ExpressionPrecedence.Conditional)) { Reference<ICodePart> expressionReference = new Reference<ICodePart>(); // Get the first operator. this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalQuestionMark, expressionReference)); // Get the expression on the right-hand side of the operator. Expression trueValue = this.GetOperatorRightHandExpression(ExpressionPrecedence.Conditional, expressionReference, unsafeCode); // Get the next operator and make sure it is the correct type. this.tokens.Add(this.GetOperatorToken(OperatorType.ConditionalColon, expressionReference)); // Get the expression on the right-hand side of the operator. Expression falseValue = this.GetOperatorRightHandExpression(ExpressionPrecedence.None, expressionReference, unsafeCode); // Create the partial token list for the expression. CsTokenList partialTokens = new CsTokenList(this.tokens, leftHandSide.Tokens.First, this.tokens.Last); // Create and return the expression. expression = new ConditionalExpression(partialTokens, leftHandSide, trueValue, falseValue); expressionReference.Target = expression; } return expression; }
/// <summary> /// The save. /// </summary> /// <param name="conditionalExpression"> /// The conditional expression. /// </param> private void Save(ConditionalExpression conditionalExpression) { this.cppWriter.Write('('); @switch(conditionalExpression.Condition); this.cppWriter.Write(") ? "); @switch(conditionalExpression.TrueExpression); this.cppWriter.Write(" : "); @switch(conditionalExpression.FalseExpression); }