public UnifiedElement VisitSwitchStatement( SwitchStatement stmt, object data) { var uExpr = stmt.Expression.TryAcceptForExpression(this); var caseCollection = UnifiedSet <UnifiedCase> .Create(); foreach (var sec in stmt.SwitchSections) { var body = sec.Statements .Select(s => s.TryAcceptForExpression(this)) .ToBlock(); var lastIx = sec.CaseLabels.Count - 1; Func <UnifiedExpression, int, UnifiedCase> func = (expr, ix) => { return((ix == lastIx) ? UnifiedCase.Create(expr, body) : UnifiedCase.Create(expr)); }; var cases = sec.CaseLabels .Select(lbl => lbl.Expression.TryAcceptForExpression(this)) .Select(func); foreach (var c in cases) { caseCollection.Add(c); } } return(UnifiedSwitch.Create(uExpr, caseCollection)); }
private static IEnumerable <UnifiedCase> CreateWhenAndDefault( XElement node) { Contract.Requires(node != null); if (node.Name() == "nil") { yield break; } if (node.Name() != "when") { yield return(UnifiedCase.CreateDefault(CreateSmartBlock(node))); } else { var first = node.FirstElement(); var caseConds = first.Elements() .Select(CreateExpresion) .ToList(); int i; for (i = 0; i < caseConds.Count - 1; i++) { yield return(UnifiedCase.Create(caseConds[i])); } yield return (UnifiedCase.Create( caseConds[i], CreateSmartBlock(node.LastElement()))); } }
private static UnifiedCase CreateCaseOrDefault(XElement node) { Contract.Requires(node != null); Contract.Requires(node.Name() == "labeled_statement"); Contract.Requires(node.FirstElement().Name != "IDENTIFIER"); /* * labeled_statement * : IDENTIFIER ':' statement | 'case' constant_expression ':' statement | 'default' ':' statement */ switch (node.FirstElement().Value) { case "case": return (UnifiedCase.Create( CreateConstantExpression(node.NthElement(1)), UnifiedBlock.Create( CreateStatement(node.NthElement(3))))); case "default": return (UnifiedCase.CreateDefault( UnifiedBlock.Create( CreateStatement(node.NthElement(2))))); default: throw new InvalidOperationException(); } }
private static IEnumerable <UnifiedExpression> CreateSwitchStatement(SwitchStatement statement) { var cases = statement.CodeList.Cast <SwitchCaseBase>().Select( sc => UnifiedCase.Create( CreatePhrase(sc.CaseLabel), CreateStatementCollection(sc.Statements))) .ToSet(); cases.Add(UnifiedCase.CreateDefault(CreateStatementCollection(statement.ElseCase.Statements))); yield return(UnifiedSwitch.Create(CreatePhrase(statement.Expression), cases)); }
public UnifiedElement VisitGotoCaseStatement( GotoCaseStatement gotoCaseStatement, object data) { return(UnifiedGoto.Create(UnifiedCase.Create(gotoCaseStatement.LabelExpression.TryAcceptForExpression(this)))); }