Esempio n. 1
0
 public override Template Visit(Class class_def)
 {
     ClassLevel++;
     var template = base.Visit(class_def);
     ClassLevel--;
     return template;
 }
Esempio n. 2
0
 public override Template Visit(Class class_def)
 {
     this.class_stack.Push(class_def);
     if (class_def.GenericParameter.Count() > 0) GenericCount++;
     var template = base.Visit(class_def);
     if (class_def.GenericParameter.Count() > 0) GenericCount--;
     this.class_stack.Push(class_def);
     return template;
 }
Esempio n. 3
0
        public override Template Visit(Class class_def)
        {
            Template template = template = new Template("<list; separator=\"\n\n\">");

            List<Template> list = new List<Template>();

            EnterNameSpace(class_def.Name);

            if (class_def.Block != null)
            {
                foreach (var node in class_def.Block.List)
                {
                    if (node is FuncDef)
                    {
                        list.Add(node.Accept(this));
                    }
                }
            }

            PopNameSpace();

            template.Add("list", list);
            return template;
        }
Esempio n. 4
0
        public override Template Visit(Class 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)));
            }
            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);
            }
            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    <constructor>");
                    List<Template> nodes = new List<Template>();
                    foreach (var item in class_def.Args)
                    {
                        GlobalAlloc alloc = new GlobalAlloc(item.Type, item.Name, null, null, true);
                        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));
                }

                {
                    Template tmp = new Template("\n    <unapply>");
                    List<Template> nodes = new List<Template>();
                    foreach (var item in class_def.Args)
                    {
                        GlobalAlloc alloc = new GlobalAlloc(item.Type, item.Name, null, null, true);
                        nodes.Add(alloc.Accept(this));
                    }
                    tmp.Add("nodes", nodes);
                    list.Add(tmp);

                    FuncDef func = new FuncDef();
                    Template type = new Template("tuple\\<<types; separator=\", \">>");
                    type.Add("types", class_def.Args.Select(x => x.Type));
                    func.Attribute = new List<Attr>{new Attr{Name = "inline"}};
                    func.Type = type.Render();
                    func.Name = "Unapply";
                    func.Body = new StmtBlock();
                    ExprTuple tuple = new ExprTuple();
                    foreach (var item in class_def.Args)
                    {
                        string name = item.Name.First();
                        tuple.ExprList.Add(new ExprConst(name, ConstType.Ident));
                    }
                    func.Body.StmtList.Add(new StmtReturn(tuple));
                    tmp.Add("unapply", func.Accept(this));
                }

                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);
            return template;
        }
Esempio n. 5
0
	private Class class_def()
	{
		EnterRule_class_def();
		EnterRule("class_def", 13);
		TraceIn("class_def", 13);
		Class value = default(Class);


		List<Attr> attr = default(List<Attr>);
		string a = default(string);
		List<string> b = default(List<string>);
		List<ExprAlloc> c = default(List<ExprAlloc>);
		List<string> d = default(List<string>);
		GlobalBlock e = default(GlobalBlock);

		try { DebugEnterRule(GrammarFileName, "class_def");
		DebugLocation(156, 1);
		try
		{
			// SugarWalker.g:157:2: ( ^( Class (attr= attribute )? a= ident (b= generic_parameter )? (c= func_args )? (d= ident_list )? (e= global_block )? ) )
			DebugEnterAlt(1);
			// SugarWalker.g:157:4: ^( Class (attr= attribute )? a= ident (b= generic_parameter )? (c= func_args )? (d= ident_list )? (e= global_block )? )
			{
			DebugLocation(157, 4);
			DebugLocation(157, 6);
			Match(input,Class,Follow._Class_in_class_def665); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(157, 12);
			// SugarWalker.g:157:12: (attr= attribute )?
			int alt20=2;
			try { DebugEnterSubRule(20);
			try { DebugEnterDecision(20, false);
			int LA20_0 = input.LA(1);

			if ((LA20_0==Attribute))
			{
				alt20 = 1;
			}
			} finally { DebugExitDecision(20); }
			switch (alt20)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:157:13: attr= attribute
				{
				DebugLocation(157, 17);
				PushFollow(Follow._attribute_in_class_def670);
				attr=attribute();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(20); }

			DebugLocation(157, 31);
			PushFollow(Follow._ident_in_class_def676);
			a=ident();
			PopFollow();

			DebugLocation(157, 38);
			// SugarWalker.g:157:38: (b= generic_parameter )?
			int alt21=2;
			try { DebugEnterSubRule(21);
			try { DebugEnterDecision(21, false);
			int LA21_0 = input.LA(1);

			if ((LA21_0==Generic_Patameters))
			{
				alt21 = 1;
			}
			} finally { DebugExitDecision(21); }
			switch (alt21)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:157:39: b= generic_parameter
				{
				DebugLocation(157, 40);
				PushFollow(Follow._generic_parameter_in_class_def681);
				b=generic_parameter();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(21); }

			DebugLocation(157, 61);
			// SugarWalker.g:157:61: (c= func_args )?
			int alt22=2;
			try { DebugEnterSubRule(22);
			try { DebugEnterDecision(22, false);
			int LA22_0 = input.LA(1);

			if ((LA22_0==Func_Args))
			{
				alt22 = 1;
			}
			} finally { DebugExitDecision(22); }
			switch (alt22)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:157:62: c= func_args
				{
				DebugLocation(157, 63);
				PushFollow(Follow._func_args_in_class_def688);
				c=func_args();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(22); }

			DebugLocation(157, 76);
			// SugarWalker.g:157:76: (d= ident_list )?
			int alt23=2;
			try { DebugEnterSubRule(23);
			try { DebugEnterDecision(23, false);
			int LA23_0 = input.LA(1);

			if ((LA23_0==Ident_List))
			{
				alt23 = 1;
			}
			} finally { DebugExitDecision(23); }
			switch (alt23)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:157:77: d= ident_list
				{
				DebugLocation(157, 78);
				PushFollow(Follow._ident_list_in_class_def695);
				d=ident_list();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(23); }

			DebugLocation(157, 92);
			// SugarWalker.g:157:92: (e= global_block )?
			int alt24=2;
			try { DebugEnterSubRule(24);
			try { DebugEnterDecision(24, false);
			int LA24_0 = input.LA(1);

			if ((LA24_0==Global_Block))
			{
				alt24 = 1;
			}
			} finally { DebugExitDecision(24); }
			switch (alt24)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:157:93: e= global_block
				{
				DebugLocation(157, 94);
				PushFollow(Follow._global_block_in_class_def702);
				e=global_block();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(24); }


			Match(input, TokenTypes.Up, null); 

			DebugLocation(158, 2);

					value = new Class(a, b, c, d, e, attr);
				

			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("class_def", 13);
			LeaveRule("class_def", 13);
			LeaveRule_class_def();
		}
		DebugLocation(161, 1);
		} finally { DebugExitRule(GrammarFileName, "class_def"); }
		return value;

	}
Esempio n. 6
0
 public abstract Template Visit(Class class_def);
        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;
        }
Esempio n. 8
0
	private Class class_def()
	{
		EnterRule_class_def();
		EnterRule("class_def", 13);
		TraceIn("class_def", 13);
		Class value = default(Class);


		CommonTree case_class = default(CommonTree);
		CommonTree pub = default(CommonTree);
		List<Attr> attr = default(List<Attr>);
		string a = default(string);
		List<SugarType> b = default(List<SugarType>);
		List<ExprAlloc> c = default(List<ExprAlloc>);
		List<string> d = default(List<string>);
		GlobalBlock e = default(GlobalBlock);

		try { DebugEnterRule(GrammarFileName, "class_def");
		DebugLocation(184, 1);
		try
		{
			// SugarWalker.g:185:2: ( ^( Class (case_class= 'case' )? (pub= 'public' )? (attr= attribute )? a= ident (b= generic_parameter )? (c= func_args )? (d= ident_list )? (e= global_block )? ) )
			DebugEnterAlt(1);
			// SugarWalker.g:185:4: ^( Class (case_class= 'case' )? (pub= 'public' )? (attr= attribute )? a= ident (b= generic_parameter )? (c= func_args )? (d= ident_list )? (e= global_block )? )
			{
			DebugLocation(185, 4);
			DebugLocation(185, 6);
			Match(input,Class,Follow._Class_in_class_def697); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(185, 12);
			// SugarWalker.g:185:12: (case_class= 'case' )?
			int alt21=2;
			try { DebugEnterSubRule(21);
			try { DebugEnterDecision(21, false);
			int LA21_0 = input.LA(1);

			if ((LA21_0==139))
			{
				alt21 = 1;
			}
			} finally { DebugExitDecision(21); }
			switch (alt21)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:13: case_class= 'case'
				{
				DebugLocation(185, 23);
				case_class=(CommonTree)Match(input,139,Follow._139_in_class_def702); 

				}
				break;

			}
			} finally { DebugExitSubRule(21); }

			DebugLocation(185, 33);
			// SugarWalker.g:185:33: (pub= 'public' )?
			int alt22=2;
			try { DebugEnterSubRule(22);
			try { DebugEnterDecision(22, false);
			int LA22_0 = input.LA(1);

			if ((LA22_0==163))
			{
				alt22 = 1;
			}
			} finally { DebugExitDecision(22); }
			switch (alt22)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:34: pub= 'public'
				{
				DebugLocation(185, 37);
				pub=(CommonTree)Match(input,163,Follow._163_in_class_def709); 

				}
				break;

			}
			} finally { DebugExitSubRule(22); }

			DebugLocation(185, 49);
			// SugarWalker.g:185:49: (attr= attribute )?
			int alt23=2;
			try { DebugEnterSubRule(23);
			try { DebugEnterDecision(23, false);
			int LA23_0 = input.LA(1);

			if ((LA23_0==Attribute))
			{
				alt23 = 1;
			}
			} finally { DebugExitDecision(23); }
			switch (alt23)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:50: attr= attribute
				{
				DebugLocation(185, 54);
				PushFollow(Follow._attribute_in_class_def716);
				attr=attribute();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(23); }

			DebugLocation(185, 68);
			PushFollow(Follow._ident_in_class_def722);
			a=ident();
			PopFollow();

			DebugLocation(185, 75);
			// SugarWalker.g:185:75: (b= generic_parameter )?
			int alt24=2;
			try { DebugEnterSubRule(24);
			try { DebugEnterDecision(24, false);
			int LA24_0 = input.LA(1);

			if ((LA24_0==Generic_Patameters))
			{
				alt24 = 1;
			}
			} finally { DebugExitDecision(24); }
			switch (alt24)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:76: b= generic_parameter
				{
				DebugLocation(185, 77);
				PushFollow(Follow._generic_parameter_in_class_def727);
				b=generic_parameter();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(24); }

			DebugLocation(185, 98);
			// SugarWalker.g:185:98: (c= func_args )?
			int alt25=2;
			try { DebugEnterSubRule(25);
			try { DebugEnterDecision(25, false);
			int LA25_0 = input.LA(1);

			if ((LA25_0==Func_Args))
			{
				alt25 = 1;
			}
			} finally { DebugExitDecision(25); }
			switch (alt25)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:99: c= func_args
				{
				DebugLocation(185, 100);
				PushFollow(Follow._func_args_in_class_def734);
				c=func_args();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(25); }

			DebugLocation(185, 113);
			// SugarWalker.g:185:113: (d= ident_list )?
			int alt26=2;
			try { DebugEnterSubRule(26);
			try { DebugEnterDecision(26, false);
			int LA26_0 = input.LA(1);

			if ((LA26_0==Ident_List))
			{
				alt26 = 1;
			}
			} finally { DebugExitDecision(26); }
			switch (alt26)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:114: d= ident_list
				{
				DebugLocation(185, 115);
				PushFollow(Follow._ident_list_in_class_def741);
				d=ident_list();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(26); }

			DebugLocation(185, 129);
			// SugarWalker.g:185:129: (e= global_block )?
			int alt27=2;
			try { DebugEnterSubRule(27);
			try { DebugEnterDecision(27, false);
			int LA27_0 = input.LA(1);

			if ((LA27_0==Global_Block))
			{
				alt27 = 1;
			}
			} finally { DebugExitDecision(27); }
			switch (alt27)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:185:130: e= global_block
				{
				DebugLocation(185, 131);
				PushFollow(Follow._global_block_in_class_def748);
				e=global_block();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(27); }


			Match(input, TokenTypes.Up, null); 

			DebugLocation(186, 2);

					value = new Class(a, b, c, d, e, attr);
					if (pub != null) value.Attribute.Add(new Attr { Name = "public" });
					if (case_class != null) value.Attribute.Add(new Attr { Name = "case" });
				

			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("class_def", 13);
			LeaveRule("class_def", 13);
			LeaveRule_class_def();
		}
		DebugLocation(191, 1);
		} finally { DebugExitRule(GrammarFileName, "class_def"); }
		return value;

	}