public static UnifiedIf CreateIf(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "if"); return(UnifiedIf.Create( CreateExpresion(node.NthElement(0)), CreateBlock(node.NthElement(1)), CreateBlock(node.NthElement(2)))); }
public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if ("); ifStatement.Condition.TryAccept(this, arg); Writer.Write(")"); ifStatement.Body.TryAccept(this, arg.Set(ForBlock)); ifStatement.ElseBody.TryAccept( this, arg.Set(ForBlock), GetIndent(arg) + "else"); return(false); }
private static IEnumerable <UnifiedExpression> CreateIfStatement(IfStatement statement) { var ifs = statement.CaseList.Select( c => Tuple.Create( CreatePhrase(c.Condition), CreateStatementCollection(c.Statements))); var els = statement.ElseCase != null?CreateStatementCollection(statement.ElseCase.Statements) : null; yield return(UnifiedIf.Create(ifs, els)); }
// If文(UnifiedIf) public override bool Visit(UnifiedIf element, VisitorArgument arg) { Writer.Write("if ("); element.Condition.TryAccept(this, arg); Writer.Write(")"); element.Body.TryAccept(this, arg.Set(ForBlock)); if (element.ElseBody != null) { Writer.WriteLine(); //if(cond){ }の後の改行 WriteIndent(arg); Writer.Write("else"); element.ElseBody.TryAccept(this, arg.Set(ForBlock)); } return false; }
//if文 public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if ("); ifStatement.Condition.TryAccept(this, arg.Set(CommaDelimiter)); Writer.Write(")"); ifStatement.Body.TryAccept(this, arg.Set(ForBlock)); if (ifStatement.ElseBody != null) { WriteIndent(arg.IndentDepth); Writer.WriteLine("else"); ifStatement.ElseBody.TryAccept(this, arg.Set(ForBlock)); } return(false); }
public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if "); ifStatement.Condition.TryAccept(this, arg); Writer.WriteLine(":"); ifStatement.Body.TryAccept(this, arg.IncrementDepth()); if (ifStatement.ElseBody != null) { WriteIndent(arg.IndentDepth); Writer.WriteLine("else:"); ifStatement.ElseBody.TryAccept(this, arg.IncrementDepth()); } return(false); }
public UnifiedElement VisitIfElseStatement( IfElseStatement stmt, object data) { var cond = stmt.Condition.TryAcceptForExpression(this); var trueBlock = stmt.TrueStatement.TryAcceptForExpression(this).ToBlock(); var nElseStmt = stmt.FalseStatement; if (nElseStmt == null) { return(UnifiedIf.Create(cond, trueBlock)); } var falseBlock = nElseStmt.TryAcceptForExpression(this).ToBlock(); return(UnifiedIf.Create(cond, trueBlock, falseBlock)); }
public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if ("); ifStatement.Condition.TryAccept(this, arg); Writer.Write(")"); ifStatement.Body.TryAccept(this, arg.Set(ForBlock)); ifStatement.ElseBody.TryAccept( this, arg.Set(ForBlock), GetIndent(arg) + "else"); return false; }
//if文 public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if ("); ifStatement.Condition.TryAccept(this, arg.Set(CommaDelimiter)); Writer.Write(")"); ifStatement.Body.TryAccept(this, arg.Set(ForBlock)); if (ifStatement.ElseBody != null) { WriteIndent(arg.IndentDepth); Writer.WriteLine("else"); ifStatement.ElseBody.TryAccept(this, arg.Set(ForBlock)); } return false; }
public override bool Visit(UnifiedIf ifStatement, VisitorArgument arg) { Writer.Write("if "); ifStatement.Condition.TryAccept(this, arg); Writer.WriteLine(":"); ifStatement.Body.TryAccept(this, arg.IncrementDepth()); if (ifStatement.ElseBody != null) { WriteIndent(arg.IndentDepth); Writer.WriteLine("else:"); ifStatement.ElseBody.TryAccept(this, arg.IncrementDepth()); } return false; }
public static UnifiedExpression CreateSelectionStatement(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "selection_statement"); /* * selection_statement * : 'if' '(' expression ')' statement (options {k=1; backtrack=false;}:'else' statement)? | 'switch' '(' expression ')' statement */ // TODO switch文のstatementについて、{}がないと単一のstatementしか取得できないため対応を考える // TODO switchNest.c switch (node.FirstElement().Value) { case "if": var statements = node.Elements("statement"); var trueBlock = CreateStatement(statements.ElementAt(0)); // statementが2つある場合はelse文がある if (statements.Count() == 2) { return (UnifiedIf.Create( CreateExpression(node.NthElement(2)).First(), trueBlock.ToBlock(), CreateStatement(statements.ElementAt(1)). ToBlock())); } return (UnifiedIf.Create( CreateExpression(node.NthElement(2)).First(), trueBlock.ToBlock())); case "switch": // statementの中身を解析して、この関数内で直接UnifiedCaseを生成します。 // labeled_statementから辿って、このノードに到達するまでにlabeled_statementがなければ、 // そのlabeled_statementはこのノードのケース文です var cases = UnifiedSet <UnifiedCase> .Create(); var labels = node.DescendantsAndSelf("labeled_statement") // Ignore label statements for goto .Where(e => e.FirstElement().Name != "IDENTIFIER") // Ignore label statements of nested switches .Where( e => !e.AncestorsUntil(node).Any( e2 => e2.Name == "selection_statement" && e2.Value.StartsWith("switch"))); foreach (var e in labels) { cases.Add(CreateCaseOrDefault(e)); } return (UnifiedSwitch.Create( CreateExpression(node.NthElement(2)).First(), cases)); default: throw new InvalidOperationException(); } }