// Emits the codedom statement for an if conditional block. public static CodeStatement Emit(IfBlock ifBlock) { // Create the codedom if statement. var i = new CodeConditionStatement(); // Emit the conditional statements for the if block. i.Condition = CodeDomEmitter.EmitCodeExpression(ifBlock.Conditional.ChildExpressions[0]); // Emit the lists of statements for the true and false bodies of the if block. // Comments need to be added in case the bodies are empty: these two properties // of the CodeConditionStatement can't be null. i.FalseStatements.Add(new CodeCommentStatement("If condition is false, execute these statements.")); i.TrueStatements.Add(new CodeCommentStatement("If condition is true, execute these statements.")); // Emit the statements for the true block foreach (var e in ifBlock.TrueBlock.ChildExpressions) i.TrueStatements.Add(CodeDomEmitter.EmitCodeStatement(e)); // Emit the statements for the false block. foreach (var e in ifBlock.FalseBlock.ChildExpressions) i.FalseStatements.Add(CodeDomEmitter.EmitCodeStatement(e)); return i; }
// Build an if conditional statement. public static void BuildIfBlock(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { var i = new IfBlock(parentExpression, currentNode.FindToken().Convert()); parentExpression.ChildExpressions.Add(i); int c = 1; // Build the conditional expression parser.ConsumeParseTree(root, i.Conditional, currentNode.ChildNodes[c]); c++; // Build the true block parser.ConsumeParseTree(root, i.TrueBlock, currentNode.ChildNodes[c]); c++; // Build the false block if one exists. if(currentNode.ChildNodes[c].ChildNodes.Count != 0) parser.ConsumeParseTree(root, i.FalseBlock, currentNode.ChildNodes[c]); }