public override Template Visit(StmtReturn expr) { if (expr.Expr != null) { Template template = new Template("return <expr>;"); template.Add("expr", expr.Expr.Accept(this)); return template; } else { Template template = new Template("return;"); return template; } }
private Stmt stmt_return() { EnterRule_stmt_return(); EnterRule("stmt_return", 32); TraceIn("stmt_return", 32); Stmt value = default(Stmt); Expr a = default(Expr); try { DebugEnterRule(GrammarFileName, "stmt_return"); DebugLocation(420, 1); try { // SugarWalker.g:421:2: ( ^( Stmt_Return (a= expr )? ) ) DebugEnterAlt(1); // SugarWalker.g:421:4: ^( Stmt_Return (a= expr )? ) { DebugLocation(421, 4); DebugLocation(421, 6); Match(input,Stmt_Return,Follow._Stmt_Return_in_stmt_return1658); if (input.LA(1) == TokenTypes.Down) { Match(input, TokenTypes.Down, null); DebugLocation(421, 18); // SugarWalker.g:421:18: (a= expr )? int alt53=2; try { DebugEnterSubRule(53); try { DebugEnterDecision(53, false); int LA53_0 = input.LA(1); if (((LA53_0>=Expr_Access && LA53_0<=Expr_Tuple)||LA53_0==IDENT||LA53_0==Match_Tuple||LA53_0==NUMBER||LA53_0==STRING||LA53_0==75||LA53_0==78||LA53_0==82||LA53_0==85||LA53_0==89||LA53_0==95||LA53_0==98||LA53_0==102||LA53_0==104||LA53_0==110||LA53_0==113||LA53_0==117||LA53_0==150||LA53_0==DOUBLE)) { alt53 = 1; } } finally { DebugExitDecision(53); } switch (alt53) { case 1: DebugEnterAlt(1); // SugarWalker.g:421:19: a= expr { DebugLocation(421, 20); PushFollow(Follow._expr_in_stmt_return1663); a=expr(); PopFollow(); } break; } } finally { DebugExitSubRule(53); } Match(input, TokenTypes.Up, null); } DebugLocation(422, 2); value = new StmtReturn(a); } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("stmt_return", 32); LeaveRule("stmt_return", 32); LeaveRule_stmt_return(); } DebugLocation(425, 1); } finally { DebugExitRule(GrammarFileName, "stmt_return"); } return value; }
public override Template Visit(Class class_def) { this.class_stack.Push(class_def); Template template = null; if (class_def.GenericParameter.Count() == 0) { template = new Template("class <name><inherit> {\n<list; separator=\"\n\">\n};"); } else { template = new Template("template \\<<generics; separator=\", \">>\nclass <name><inherit> {\n<list; separator=\"\n\">\n};"); template.Add("generics", class_def.GenericParameter.Select(x => string.Format("typename {0}", x.Accept(this).Render())).ToArray()); } template.Add("name", class_def.Name); if (class_def.Inherit.Count() > 0) { Template tmp = new Template(": <inherit; separator=\", \">"); tmp.Add("inherit", class_def.Inherit.Select(x => string.Format("public {0}", x))); template.Add("inherit", tmp); } else { template.Add("inherit", ""); } List<Template> list = new List<Template>(); bool default_public = class_def.Attribute.Find(x => x.Name == "public") != null; string last = "private"; bool last_flag = false; AstNode last_node = null; // friend class foreach (var attr in class_def.Attribute) { if (attr.Name == "friend") { foreach (var name in attr.Args) { Template friend = new Template(" friend class <name>;"); friend.Add("name", name); list.Add(friend); } } } // Args if (class_def.Args.Count() > 0) { { Template tmp = new Template("\npublic:\n <nodes; separator=\"\n\">\n\n <name>() = default;\n <constructor>"); tmp.Add("name", class_def.Name); List<Template> nodes = new List<Template>(); foreach (var item in class_def.Args) { GlobalAlloc alloc = new GlobalAlloc(item.Type, item.Name, null, null, AllocType.Declare); nodes.Add(alloc.Accept(this)); } tmp.Add("nodes", nodes); list.Add(tmp); { FuncDef func = new FuncDef(); func.Type = null; func.Name = class_def.Name; func.Args = class_def.Args; func.Body = new StmtBlock(); foreach (var item in class_def.Args) { string name = item.Name.First(); ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name), new ExprConst(name, ConstType.Ident)); func.Body.StmtList.Add(new StmtExpr(assign)); } tmp.Add("constructor", func.Accept(this)); } } last = "public"; last_flag = true; } if (class_def.Attribute.Exists(x => x.Name == "case")) { Template tmp = null; if (last != "public") { tmp = new Template("\npublic:\n <get_type>"); } else { tmp = new Template(" <get_type>"); } FuncDef func = new FuncDef(); func.Type = new IdentType("const char*"); func.Name = "GetType"; func.Args = new List<ExprAlloc>(); func.Body = new StmtBlock(); func.Attribute.Add(new Attr { Name = "virtual" }); StmtReturn stmt = new StmtReturn(new ExprConst("\"" + class_def.Name + "\"", ConstType.String)); func.Body.StmtList.Add(stmt); tmp.Add("get_type", func.Accept(this)); list.Add(tmp); last = "public"; last_flag = true; } if (class_def.Block != null) { foreach (var node in class_def.Block.List) { bool current = node is FuncDef || node is Class || node is Enum || node is Import || node is GlobalUsing || node is Namespace; string modifier = null; if (!default_public) { modifier = node.Attribute.Find(x => x.Name == "public") != null ? "public" : "private"; } else { modifier = node.Attribute.Find(x => x.Name == "private") != null ? "private" : "public"; } if (modifier != last) { Template member = new Template("\n<modifier>:\n <expr>"); member.Add("modifier", modifier); member.Add("expr", node.Accept(this)); list.Add(member); } else { if ((last_flag || current) && !(last_node is Import && node is Import)) { Template member = new Template("\n <expr>"); member.Add("expr", node.Accept(this)); list.Add(member); } else { Template member = new Template(" <expr>"); member.Add("expr", node.Accept(this)); list.Add(member); } } last = modifier; last_flag = current; last_node = node; } } template.Add("list", list); this.class_stack.Pop(); return template; }
public override Template Visit(Class class_def) { Template template = new Template("<list; separator=\"\n\n\">"); List <Template> list = new List <Template>(); EnterNameSpace(class_def.Name); this.class_stack.Push(class_def); if (class_def.Args.Count() > 0) { { FuncDef func = new FuncDef(); func.Type = null; func.Name = class_def.Name; func.Args = class_def.Args; func.Body = new StmtBlock(); foreach (var item in class_def.Args) { string name = item.Name.First(); ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name), new ExprConst(name, ConstType.Ident)); func.Body.StmtList.Add(new StmtExpr(assign)); } list.Add(func.Accept(this)); } } if (class_def.Attribute.Exists(x => x.Name == "case")) { FuncDef func = new FuncDef(); func.Type = new IdentType("const char*"); func.Name = "GetType"; func.Args = new List <ExprAlloc>(); func.Body = new StmtBlock(); StmtReturn stmt = new StmtReturn(new ExprConst("\"" + class_def.Name + "\"", ConstType.String)); func.Body.StmtList.Add(stmt); list.Add(func.Accept(this)); } if (class_def.Block != null) { foreach (var node in class_def.Block.List) { if (node is FuncDef) { list.Add(node.Accept(this)); } if (node is GlobalAlloc && node.Attribute.Exists(x => x.Name == "static") && !node.Attribute.Exists(x => x.Name == "const")) { list.Add(node.Accept(this)); } } } this.class_stack.Pop(); PopNameSpace(); template.Add("list", list); return(template); }
public abstract Template Visit(StmtReturn stmt);
public override Template Visit(Class class_def) { Template template = new Template("<list; separator=\"\n\n\">"); List<Template> list = new List<Template>(); EnterNameSpace(class_def.Name); this.class_stack.Push(class_def); if (class_def.Args.Count() > 0) { { FuncDef func = new FuncDef(); func.Type = null; func.Name = class_def.Name; func.Args = class_def.Args; func.Body = new StmtBlock(); foreach (var item in class_def.Args) { string name = item.Name.First(); ExprAssign assign = new ExprAssign(new ExprAccess(new ExprConst("this", ConstType.Ident), "->", name), new ExprConst(name, ConstType.Ident)); func.Body.StmtList.Add(new StmtExpr(assign)); } list.Add(func.Accept(this)); } } if (class_def.Attribute.Exists(x => x.Name == "case")) { FuncDef func = new FuncDef(); func.Type = new IdentType("const char*"); func.Name = "GetType"; func.Args = new List<ExprAlloc>(); func.Body = new StmtBlock(); StmtReturn stmt = new StmtReturn(new ExprConst("\"" + class_def.Name + "\"", ConstType.String)); func.Body.StmtList.Add(stmt); list.Add(func.Accept(this)); } if (class_def.Block != null) { foreach (var node in class_def.Block.List) { if (node is FuncDef) { list.Add(node.Accept(this)); } if (node is GlobalAlloc && node.Attribute.Exists(x => x.Name == "static") && !node.Attribute.Exists(x => x.Name == "const")) { list.Add(node.Accept(this)); } } } this.class_stack.Pop(); PopNameSpace(); template.Add("list", list); return template; }
private Stmt stmt_return() { EnterRule_stmt_return(); EnterRule("stmt_return", 41); TraceIn("stmt_return", 41); Stmt value = default(Stmt); Expr a = default(Expr); try { DebugEnterRule(GrammarFileName, "stmt_return"); DebugLocation(558, 1); try { // SugarWalker.g:559:2: ( ^( Stmt_Return (a= expr )? ) ) DebugEnterAlt(1); // SugarWalker.g:559:4: ^( Stmt_Return (a= expr )? ) { DebugLocation(559, 4); DebugLocation(559, 6); Match(input,Stmt_Return,Follow._Stmt_Return_in_stmt_return2137); if (input.LA(1) == TokenTypes.Down) { Match(input, TokenTypes.Down, null); DebugLocation(559, 18); // SugarWalker.g:559:18: (a= expr )? int alt66=2; try { DebugEnterSubRule(66); try { DebugEnterDecision(66, false); int LA66_0 = input.LA(1); if (((LA66_0>=Expr_Access && LA66_0<=Expr_Alloc_Equal)||(LA66_0>=Expr_Bin && LA66_0<=Expr_Where)||LA66_0==IDENT||LA66_0==Match_Expr||LA66_0==Match_Tuple||LA66_0==NUMBER||LA66_0==STRING||LA66_0==89||LA66_0==92||LA66_0==96||LA66_0==99||LA66_0==104||LA66_0==110||LA66_0==113||LA66_0==118||LA66_0==121||LA66_0==128||LA66_0==131||LA66_0==135||LA66_0==183||LA66_0==DOUBLE)) { alt66 = 1; } } finally { DebugExitDecision(66); } switch (alt66) { case 1: DebugEnterAlt(1); // SugarWalker.g:559:19: a= expr { DebugLocation(559, 20); PushFollow(Follow._expr_in_stmt_return2142); a=expr(); PopFollow(); } break; } } finally { DebugExitSubRule(66); } Match(input, TokenTypes.Up, null); } DebugLocation(560, 2); value = new StmtReturn(a); } } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { TraceOut("stmt_return", 41); LeaveRule("stmt_return", 41); LeaveRule_stmt_return(); } DebugLocation(563, 1); } finally { DebugExitRule(GrammarFileName, "stmt_return"); } return value; }