private static SyntaxNode ProcessMatch(SyntaxNode node, Scope scope) { var switchExpr = node as SwitchStatementSyntax; if (switchExpr == null) { scope.AddError("match01", "malformed match", node); return(node); } //store items to simplify var cases = new List <ExpressionSyntax>(); var statements = new List <StatementSyntax>(); var defaultStatement = null as StatementSyntax; foreach (var section in switchExpr.Sections) { bool isDefault; var expr = caseExpression(section.Labels, switchExpr.Expression, out isDefault); StatementSyntax statement = caseStatement(section.Statements); if (isDefault && section.Labels.Count == 1) { defaultStatement = statement; } else { cases.Add(expr); statements.Add(statement); if (isDefault) { defaultStatement = statement; } } } //convert cases to ifs Debug.Assert(cases.Count == statements.Count); var last = cases.Count - 1; IfStatementSyntax result = CSharp.IfStatement(cases[last], statements[last]); if (defaultStatement != null) { result = result.WithElse(CSharp.ElseClause(defaultStatement)); } for (int i = last - 1; i >= 0; i--) { result = CSharp.IfStatement(cases[i], statements[i]) .WithElse(CSharp.ElseClause(result)); } return(result); }
private static SyntaxNode IfElseStatement(RParser.IfElseStatementContext ifStatement, Func <ParserRuleContext, Scope, SyntaxNode> transform, Scope scope) { if (!topLevel(ifStatement)) { throw new NotImplementedException(); } var exprs = ifStatement.expr(); var cond = (ExpressionSyntax)transform(exprs[0], scope); var @if = parseBlock(exprs[1], transform, scope); var @else = parseBlock(exprs[2], transform, scope); return(@ifelse.Get <IfStatementSyntax>(cond) .WithStatement(@if) .WithElse(CSharp.ElseClause( @else))); }