public override Template Visit(StmtIf stmt_if) { if (stmt_if.Else == null) { Template template = new Template("if (<cond>) {\n <body>\n}"); template.Add("cond", stmt_if.Condition.Accept(this)); template.Add("body", stmt_if.Body.Accept(this)); return template; } else { Template template = new Template("if (<cond>) {\n <body1>\n} else {\n <body2>\n}"); template.Add("cond", stmt_if.Condition.Accept(this)); template.Add("body1", stmt_if.Body.Accept(this)); template.Add("body2", stmt_if.Else.Accept(this)); return template; } }
public override Template Visit(StmtForEach stmt_for_each) { if (stmt_for_each.Var is ExprConst) { ExprConst expr = (ExprConst) stmt_for_each.Var; Template template = new Template("for (auto <var> : <expr>) {\n <body>\n}"); template.Add("var", expr.Text); template.Add("expr", stmt_for_each.Target.Accept(this)); template.Add("body", stmt_for_each.Body.Accept(this)); return template; } else if (stmt_for_each.Var is ExprCall) { ExprCall expr = (ExprCall)stmt_for_each.Var; List<Stmt> stmt_list = new List<Stmt>(); List<Expr> condition_list = new List<Expr>(); int i = 0; foreach (var argument in expr.Args) { ExprCall get = new ExprCall(new ExprConst("std::get", ConstType.Ident), new List<string> {i.ToString()}, new List<Expr> {new ExprConst("_t_match", ConstType.Ident)}); i++; if (argument is ExprConst && ((ExprConst)argument).Type == ConstType.Ident && !((ExprConst)argument).Text.StartsWith("@")) { ExprConst const_expr = (ExprConst)argument; if (const_expr.Text == "_") { continue; } stmt_list.Add(new StmtExpr(new ExprAlloc("auto", new List<string> { const_expr.Text }, new List<Expr>{ get }, true))); } else { if (((ExprConst)argument).Text.StartsWith("@")) { ((ExprConst)argument).Text = ((ExprConst)argument).Text.Substring(1); } condition_list.Add(new ExprBin("==", get, argument)); } } StmtBlock block = new StmtBlock(); foreach (var item in stmt_list) { block.StmtList.Add(item); } foreach (var item in stmt_for_each.Body.StmtList) { block.StmtList.Add(item); } if (condition_list.Count() > 0) { StmtBlock if_body = new StmtBlock(); if_body.StmtList.Add(new StmtExpr(new ExprAlloc("auto&&", new List<string> { "_t_match" }, new List<Expr> { new ExprCall(new ExprAccess(new ExprConst("_t_iterator", ConstType.Ident), ".", "Unapply"), null, null) }, true))); Expr condition = null; foreach (var item in condition_list) { if (condition == null) { condition = item; if (condition_list.Count() > 1) { condition = new ExprBracket(condition); } } else { condition = new ExprBin("&&", condition, new ExprBracket(item)); } } StmtIf stmt_if = new StmtIf(condition, block, null); if_body.StmtList.Add(stmt_if); block = if_body; } else { block.StmtList.Insert(0, new StmtExpr(new ExprAlloc("auto&&", new List<string> { "_t_match" }, new List<Expr> { new ExprCall(new ExprAccess(new ExprConst("_t_iterator", ConstType.Ident), ".", "Unapply"), null, null) }, true))); } StmtForEach for_each = new StmtForEach(new ExprConst("_t_iterator", ConstType.Ident), stmt_for_each.Target, block); return for_each.Accept(this); } else if (stmt_for_each.Var is ExprTuple) { ExprTuple expr = (ExprTuple)stmt_for_each.Var; List<Stmt> stmt_list = new List<Stmt>(); List<Expr> condition_list = new List<Expr>(); int i = 0; foreach (var argument in expr.ExprList) { ExprCall get = new ExprCall(new ExprConst("get", ConstType.Ident), new List<string> { i.ToString() }, new List<Expr> { new ExprConst("_t_match", ConstType.Ident) }); i++; if (argument is ExprConst && ((ExprConst)argument).Type == ConstType.Ident) { ExprConst const_expr = (ExprConst)argument; if (const_expr.Text == "_") { continue; } stmt_list.Add(new StmtExpr(new ExprAlloc("auto", new List<string> { const_expr.Text }, new List<Expr> { get }, true))); } else { condition_list.Add(new ExprBin("==", get, argument)); } } StmtBlock block = new StmtBlock(); foreach (var item in stmt_list) { block.StmtList.Add(item); } foreach (var item in stmt_for_each.Body.StmtList) { block.StmtList.Add(item); } if (condition_list.Count() > 0) { StmtBlock if_body = new StmtBlock(); Expr condition = null; foreach (var item in condition_list) { if (condition == null) { condition = item; if (condition_list.Count() > 1) { condition = new ExprBracket(condition); } } else { condition = new ExprBin("&&", condition, new ExprBracket(item)); } } StmtIf stmt_if = new StmtIf(condition, block, null); if_body.StmtList.Add(stmt_if); block = if_body; } StmtForEach for_each = new StmtForEach(new ExprConst("_t_match", ConstType.Ident), stmt_for_each.Target, block); return for_each.Accept(this); } else { throw new Exception(string.Format("Iterators in foreach must be either variable or pattern matching")); } }
private Stmt stmt_if() { EnterRule_stmt_if(); EnterRule("stmt_if", 26); TraceIn("stmt_if", 26); Stmt value = default(Stmt); Expr a = default(Expr); StmtBlock b = default(StmtBlock); StmtBlock c = default(StmtBlock); try { DebugEnterRule(GrammarFileName, "stmt_if"); DebugLocation(341, 1); try { // SugarWalker.g:342:2: ( ^( Stmt_If a= expr b= stmt_block (c= stmt_block )? ) | ^( Stmt_Unless a= expr b= stmt_block (c= stmt_block )? ) ) int alt46=2; try { DebugEnterDecision(46, false); int LA46_0 = input.LA(1); if ((LA46_0==Stmt_If)) { alt46 = 1; } else if ((LA46_0==Stmt_Unless)) { alt46 = 2; } else { NoViableAltException nvae = new NoViableAltException("", 46, 0, input); DebugRecognitionException(nvae); throw nvae; } } finally { DebugExitDecision(46); } switch (alt46) { case 1: DebugEnterAlt(1); // SugarWalker.g:342:4: ^( Stmt_If a= expr b= stmt_block (c= stmt_block )? ) { DebugLocation(342, 4); DebugLocation(342, 6); Match(input,Stmt_If,Follow._Stmt_If_in_stmt_if1343); Match(input, TokenTypes.Down, null); DebugLocation(342, 15); PushFollow(Follow._expr_in_stmt_if1347); a=expr(); PopFollow(); DebugLocation(342, 22); PushFollow(Follow._stmt_block_in_stmt_if1351); b=stmt_block(); PopFollow(); DebugLocation(342, 34); // SugarWalker.g:342:34: (c= stmt_block )? int alt44=2; try { DebugEnterSubRule(44); try { DebugEnterDecision(44, false); int LA44_0 = input.LA(1); if ((LA44_0==Stmt_Block)) { alt44 = 1; } } finally { DebugExitDecision(44); } switch (alt44) { case 1: DebugEnterAlt(1); // SugarWalker.g:342:35: c= stmt_block { DebugLocation(342, 36); PushFollow(Follow._stmt_block_in_stmt_if1356); c=stmt_block(); PopFollow(); } break; } } finally { DebugExitSubRule(44); } Match(input, TokenTypes.Up, null); DebugLocation(343, 2); value = new StmtIf(a, b, c); } break; case 2: DebugEnterAlt(2); // SugarWalker.g:346:4: ^( Stmt_Unless a= expr b= stmt_block (c= stmt_block )? ) { DebugLocation(346, 4); DebugLocation(346, 6); Match(input,Stmt_Unless,Follow._Stmt_Unless_in_stmt_if1368); Match(input, TokenTypes.Down, null); DebugLocation(346, 19); PushFollow(Follow._expr_in_stmt_if1372); a=expr(); PopFollow(); DebugLocation(346, 26); PushFollow(Follow._stmt_block_in_stmt_if1376); b=stmt_block(); PopFollow(); DebugLocation(346, 38); // SugarWalker.g:346:38: (c= stmt_block )? int alt45=2; try { DebugEnterSubRule(45); try { DebugEnterDecision(45, false); int LA45_0 = input.LA(1); if ((LA45_0==Stmt_Block)) { alt45 = 1; } } finally { DebugExitDecision(45); } switch (alt45) { case 1: DebugEnterAlt(1); // SugarWalker.g:346:39: c= stmt_block { DebugLocation(346, 40); PushFollow(Follow._stmt_block_in_stmt_if1381); c=stmt_block(); PopFollow(); } break; } } finally { DebugExitSubRule(45); } Match(input, TokenTypes.Up, null); DebugLocation(347, 2); value = new StmtIf(new ExprPrefix("!", new ExprBracket(a)), b, c); } break; } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("stmt_if", 26); LeaveRule("stmt_if", 26); LeaveRule_stmt_if(); } DebugLocation(350, 1); } finally { DebugExitRule(GrammarFileName, "stmt_if"); } return value; }
private List<Stmt> stmt_translate() { EnterRule_stmt_translate(); EnterRule("stmt_translate", 20); TraceIn("stmt_translate", 20); List<Stmt> value = default(List<Stmt>); Expr a = default(Expr); Expr b = default(Expr); List<string> d = default(List<string>); List<Expr> e = default(List<Expr>); value = new List<Stmt>(); try { DebugEnterRule(GrammarFileName, "stmt_translate"); DebugLocation(281, 1); try { // SugarWalker.g:286:2: ( ^( '?=' a= expr b= expr ) | ^( ':=' d= ident_list e= expr_list ) ) int alt40=2; try { DebugEnterDecision(40, false); int LA40_0 = input.LA(1); if ((LA40_0==112)) { alt40 = 1; } else if ((LA40_0==98)) { alt40 = 2; } else { NoViableAltException nvae = new NoViableAltException("", 40, 0, input); DebugRecognitionException(nvae); throw nvae; } } finally { DebugExitDecision(40); } switch (alt40) { case 1: DebugEnterAlt(1); // SugarWalker.g:286:4: ^( '?=' a= expr b= expr ) { DebugLocation(286, 4); DebugLocation(286, 6); Match(input,112,Follow._112_in_stmt_translate1128); Match(input, TokenTypes.Down, null); DebugLocation(286, 12); PushFollow(Follow._expr_in_stmt_translate1132); a=expr(); PopFollow(); DebugLocation(286, 19); PushFollow(Follow._expr_in_stmt_translate1136); b=expr(); PopFollow(); Match(input, TokenTypes.Up, null); DebugLocation(287, 2); StmtBlock block = new StmtBlock(); block.StmtList.Add(new StmtExpr(new ExprAssign(a, b))); StmtIf stmt_if = new StmtIf(new ExprBin("==", a, new ExprConst("nullptr", ConstType.Ident)), block, null); value.Add(stmt_if); } break; case 2: DebugEnterAlt(2); // SugarWalker.g:293:4: ^( ':=' d= ident_list e= expr_list ) { DebugLocation(293, 4); DebugLocation(293, 6); Match(input,98,Follow._98_in_stmt_translate1146); Match(input, TokenTypes.Down, null); DebugLocation(293, 12); PushFollow(Follow._ident_list_in_stmt_translate1150); d=ident_list(); PopFollow(); DebugLocation(293, 25); PushFollow(Follow._expr_list_in_stmt_translate1154); e=expr_list(); PopFollow(); Match(input, TokenTypes.Up, null); DebugLocation(294, 2); int k = 0; for (int i = 0; i < d.Count(); i++) { value.Add(new StmtExpr(new ExprAlloc("auto", new List<string> { d[i] }, new List<Expr>{ e[k] }, true))); k = (k + 1) % e.Count(); } } break; } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("stmt_translate", 20); LeaveRule("stmt_translate", 20); LeaveRule_stmt_translate(); } DebugLocation(302, 1); } finally { DebugExitRule(GrammarFileName, "stmt_translate"); } return value; }
public abstract Template Visit(StmtIf stmt_if);
private List<Stmt> stmt_translate() { EnterRule_stmt_translate(); EnterRule("stmt_translate", 27); TraceIn("stmt_translate", 27); List<Stmt> value = default(List<Stmt>); Expr a = default(Expr); Expr b = default(Expr); List<string> d = default(List<string>); List<Expr> e = default(List<Expr>); value = new List<Stmt>(); try { DebugEnterRule(GrammarFileName, "stmt_translate"); DebugLocation(390, 1); try { // SugarWalker.g:395:2: ( ^( '?=' a= expr b= expr ) | ^( ':=' d= ident_list e= expr_list ) | ^( '<<' a= expr b= expr ) ) int alt49=3; try { DebugEnterDecision(49, false); switch (input.LA(1)) { case 130: { alt49 = 1; } break; case 113: { alt49 = 2; } break; case 117: { alt49 = 3; } break; default: { NoViableAltException nvae = new NoViableAltException("", 49, 0, input); DebugRecognitionException(nvae); throw nvae; } } } finally { DebugExitDecision(49); } switch (alt49) { case 1: DebugEnterAlt(1); // SugarWalker.g:395:4: ^( '?=' a= expr b= expr ) { DebugLocation(395, 4); DebugLocation(395, 6); Match(input,130,Follow._130_in_stmt_translate1464); Match(input, TokenTypes.Down, null); DebugLocation(395, 12); PushFollow(Follow._expr_in_stmt_translate1468); a=expr(); PopFollow(); DebugLocation(395, 19); PushFollow(Follow._expr_in_stmt_translate1472); b=expr(); PopFollow(); Match(input, TokenTypes.Up, null); DebugLocation(396, 2); StmtBlock block = new StmtBlock(); block.StmtList.Add(new StmtExpr(new ExprAssign(a, b))); StmtIf stmt_if = new StmtIf(new ExprBin("==", a, new ExprConst("nullptr", ConstType.Ident)), block, null); value.Add(stmt_if); } break; case 2: DebugEnterAlt(2); // SugarWalker.g:402:4: ^( ':=' d= ident_list e= expr_list ) { DebugLocation(402, 4); DebugLocation(402, 6); Match(input,113,Follow._113_in_stmt_translate1482); Match(input, TokenTypes.Down, null); DebugLocation(402, 12); PushFollow(Follow._ident_list_in_stmt_translate1486); d=ident_list(); PopFollow(); DebugLocation(402, 25); PushFollow(Follow._expr_list_in_stmt_translate1490); e=expr_list(); PopFollow(); Match(input, TokenTypes.Up, null); DebugLocation(403, 2); int k = 0; for (int i = 0; i < d.Count(); i++) { value.Add(new StmtExpr(new ExprAlloc(new AutoType(), d[i], e[k], AllocType.Equal))); k = (k + 1) % e.Count(); } } break; case 3: DebugEnterAlt(3); // SugarWalker.g:411:4: ^( '<<' a= expr b= expr ) { DebugLocation(411, 4); DebugLocation(411, 6); Match(input,117,Follow._117_in_stmt_translate1500); Match(input, TokenTypes.Down, null); DebugLocation(411, 12); PushFollow(Follow._expr_in_stmt_translate1504); a=expr(); PopFollow(); DebugLocation(411, 19); PushFollow(Follow._expr_in_stmt_translate1508); b=expr(); PopFollow(); Match(input, TokenTypes.Up, null); } break; } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("stmt_translate", 27); LeaveRule("stmt_translate", 27); LeaveRule_stmt_translate(); } DebugLocation(412, 1); } finally { DebugExitRule(GrammarFileName, "stmt_translate"); } return value; }