public override Template Visit(FuncDef func_def)
        {
            string prefix = "";
            if (func_def.Attribute.Find(x => x.Name == "inline") != null)
            {
                prefix += "inline ";
            }
            if (func_def.Attribute.Find(x => x.Name == "static") != null)
            {
                prefix += "static ";
            }
            string suffix = "";
            if (func_def.Attribute.Find(x => x.Name == "const") != null)
            {
                suffix += " const";
            }

            Template template = null;
            if (func_def.GenericParameter.Count() == 0)
            {
                if (func_def.Type == null)
                {
                    template = new Template("<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
            }
            else
            {
                if (func_def.Type == null)
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                template.Add("generics", func_def.GenericParameter.Select(x => string.Format("typename {0}", x)));
            }
            template.Add("prefix", prefix);
            template.Add("suffix", suffix);
            template.Add("type", func_def.Type);
            template.Add("name", NameInNameSpace(func_def.Name));
            template.Add("args", func_def.Args.Select(x => x.Accept(this)));
            template.Add("list", func_def.Body.Accept(this));
            return template;
        }
        public override Template Visit(Enum enum_def)
        {
            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc(new IdentType("const " + enum_def.Name + "&"), "_t_value", null, AllocType.Declare));
                List <StmtSwitchItem> switch_list = new List <StmtSwitchItem>();
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    switch_list.Add(new StmtSwitchItem(new List <Expr> {
                        new ExprConst(enum_def.Name + "::" + item, ConstType.Ident)
                    }, block));
                }

                StmtBlock default_block = new StmtBlock();
                {
                    default_block.StmtList.Add(new StmtExpr(new ExprCall(new ExprConst("throw", ConstType.Ident), null, new List <Expr> {
                        new ExprConst("\"Not Found\"", ConstType.String)
                    })));
                }

                StmtSwitch stmt_switch = new StmtSwitch(new ExprConst("_t_value", ConstType.Ident), switch_list, default_block);
                StmtBlock  body        = new StmtBlock();
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                return(func.Accept(this));
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Beispiel #3
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;
        }
Beispiel #4
0
        public override Template Visit(Enum enum_def)
        {
            Template template = new Template("enum <name> {\n    <list; separator=\",\n\">\n};<tostring>");
            template.Add("name", enum_def.Name);
            List<Template> list = new List<Template>();
            bool hasFlagAttribute = enum_def.Attribute.Find(x => x.Name == "FlagAttribute") != null;
            int i = 0;
            foreach (var item in enum_def.Values)
            {
                Template tp = new Template("<node><suffix>");
                tp.Add("node", item);
                if (i == 0 || hasFlagAttribute)
                {
                    tp.Add("suffix", string.Format(" = {0}", i));
                }
                else
                {
                    tp.Add("suffix", "");
                }
                list.Add(tp);
                if (hasFlagAttribute)
                {
                    i = i == 0 ? 1 : i * 2;
                }
                else
                {
                    i = i + 1;
                }
            }
            template.Add("list", list);

            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = "const char*";
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc("const " + enum_def.Name + "&", new List<string> { "a" }, null, true));
                StmtBlock body = new StmtBlock();
                StmtSwitch stmt_switch = new StmtSwitch();
                stmt_switch.Expr = new ExprConst("a", ConstType.Ident);
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    stmt_switch.List.Add(new StmtSwitchItem(new ExprConst(item, ConstType.Ident), block));
                }
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                Template node = new Template("\n\n<stmt>");
                node.Add("stmt", func.Accept(this));
                template.Add("tostring", node);
            }
            else
            {
                template.Add("tostring", "");
            }
            return template;
        }
Beispiel #5
0
	private FuncDef func_def()
	{
		EnterRule_func_def();
		EnterRule("func_def", 17);
		TraceIn("func_def", 17);
		FuncDef value = default(FuncDef);


		CommonTree deconstructor = default(CommonTree);
		List<Attr> attr = default(List<Attr>);
		string a = default(string);
		string b = default(string);
		List<string> x = default(List<string>);
		List<ExprAlloc> args = default(List<ExprAlloc>);
		StmtBlock e = default(StmtBlock);
		Expr f = default(Expr);


			value = new FuncDef();

		try { DebugEnterRule(GrammarFileName, "func_def");
		DebugLocation(210, 1);
		try
		{
			// SugarWalker.g:215:2: ( ^( Func_Def (attr= attribute )? (a= type_name )? (deconstructor= '~' )? b= ident (x= generic_parameter )? (args= func_args )? (e= stmt_block |f= expr ) ) )
			DebugEnterAlt(1);
			// SugarWalker.g:215:4: ^( Func_Def (attr= attribute )? (a= type_name )? (deconstructor= '~' )? b= ident (x= generic_parameter )? (args= func_args )? (e= stmt_block |f= expr ) )
			{
			DebugLocation(215, 4);
			DebugLocation(215, 6);
			Match(input,Func_Def,Follow._Func_Def_in_func_def919); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(215, 15);
			// SugarWalker.g:215:15: (attr= attribute )?
			int alt32=2;
			try { DebugEnterSubRule(32);
			try { DebugEnterDecision(32, false);
			int LA32_0 = input.LA(1);

			if ((LA32_0==Attribute))
			{
				alt32 = 1;
			}
			} finally { DebugExitDecision(32); }
			switch (alt32)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:215:16: attr= attribute
				{
				DebugLocation(215, 20);
				PushFollow(Follow._attribute_in_func_def924);
				attr=attribute();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(32); }

			DebugLocation(215, 33);
			// SugarWalker.g:215:33: (a= type_name )?
			int alt33=2;
			try { DebugEnterSubRule(33);
			try { DebugEnterDecision(33, false);
			int LA33_0 = input.LA(1);

			if ((LA33_0==Type_IDENT))
			{
				alt33 = 1;
			}
			} finally { DebugExitDecision(33); }
			switch (alt33)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:215:34: a= type_name
				{
				DebugLocation(215, 35);
				PushFollow(Follow._type_name_in_func_def931);
				a=type_name();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(33); }

			DebugLocation(215, 48);
			// SugarWalker.g:215:48: (deconstructor= '~' )?
			int alt34=2;
			try { DebugEnterSubRule(34);
			try { DebugEnterDecision(34, false);
			int LA34_0 = input.LA(1);

			if ((LA34_0==152))
			{
				alt34 = 1;
			}
			} finally { DebugExitDecision(34); }
			switch (alt34)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:215:49: deconstructor= '~'
				{
				DebugLocation(215, 62);
				deconstructor=(CommonTree)Match(input,152,Follow._152_in_func_def938); 

				}
				break;

			}
			} finally { DebugExitSubRule(34); }

			DebugLocation(215, 70);
			PushFollow(Follow._ident_in_func_def944);
			b=ident();
			PopFollow();

			DebugLocation(215, 77);
			// SugarWalker.g:215:77: (x= generic_parameter )?
			int alt35=2;
			try { DebugEnterSubRule(35);
			try { DebugEnterDecision(35, false);
			int LA35_0 = input.LA(1);

			if ((LA35_0==Generic_Patameters))
			{
				alt35 = 1;
			}
			} finally { DebugExitDecision(35); }
			switch (alt35)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:215:78: x= generic_parameter
				{
				DebugLocation(215, 79);
				PushFollow(Follow._generic_parameter_in_func_def949);
				x=generic_parameter();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(35); }

			DebugLocation(215, 101);
			// SugarWalker.g:215:101: (args= func_args )?
			int alt36=2;
			try { DebugEnterSubRule(36);
			try { DebugEnterDecision(36, false);
			int LA36_0 = input.LA(1);

			if ((LA36_0==Func_Args))
			{
				alt36 = 1;
			}
			} finally { DebugExitDecision(36); }
			switch (alt36)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:215:102: args= func_args
				{
				DebugLocation(215, 106);
				PushFollow(Follow._func_args_in_func_def957);
				args=func_args();
				PopFollow();

				DebugLocation(215, 117);
				 value.Args = args; 

				}
				break;

			}
			} finally { DebugExitSubRule(36); }

			DebugLocation(216, 2);
			// SugarWalker.g:216:2: (e= stmt_block |f= expr )
			int alt37=2;
			try { DebugEnterSubRule(37);
			try { DebugEnterDecision(37, false);
			int LA37_0 = input.LA(1);

			if ((LA37_0==Stmt_Block))
			{
				alt37 = 1;
			}
			else if (((LA37_0>=Expr_Access && LA37_0<=Expr_Tuple)||LA37_0==IDENT||LA37_0==Match_Tuple||LA37_0==NUMBER||LA37_0==STRING||LA37_0==75||LA37_0==78||LA37_0==82||LA37_0==85||LA37_0==89||LA37_0==95||LA37_0==98||LA37_0==102||LA37_0==104||LA37_0==110||LA37_0==113||LA37_0==117||LA37_0==150||LA37_0==DOUBLE))
			{
				alt37 = 2;
			}
			else
			{
				NoViableAltException nvae = new NoViableAltException("", 37, 0, input);
				DebugRecognitionException(nvae);
				throw nvae;
			}
			} finally { DebugExitDecision(37); }
			switch (alt37)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:216:4: e= stmt_block
				{
				DebugLocation(216, 5);
				PushFollow(Follow._stmt_block_in_func_def968);
				e=stmt_block();
				PopFollow();

				DebugLocation(217, 2);

						if (attr != null) value.Attribute = attr;
						value.Type = a;
						value.Name = b;
						if (deconstructor != null) 
						{
							value.Name = "~" + value.Name;
						}
						if (x != null)
						{
							value.GenericParameter = x;
						}
						value.Body = e;
					

				}
				break;
			case 2:
				DebugEnterAlt(2);
				// SugarWalker.g:231:4: f= expr
				{
				DebugLocation(231, 5);
				PushFollow(Follow._expr_in_func_def978);
				f=expr();
				PopFollow();

				DebugLocation(232, 2);

						if (attr != null) value.Attribute = attr;
						value.Type = a;
						value.Name = b;
						if (deconstructor != null) 
						{
							value.Name = "~" + value.Name;
						}
						StmtBlock block = new StmtBlock();
						if (a == "void" || a == null)
						{
							block.StmtList.Add(new StmtExpr(f));
						}
						else
						{
							block.StmtList.Add(new StmtReturn(f));
						}
						if (x != null)
						{
							value.GenericParameter = x;
						}
						value.Body = block;
					

				}
				break;

			}
			} finally { DebugExitSubRule(37); }


			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("func_def", 17);
			LeaveRule("func_def", 17);
			LeaveRule_func_def();
		}
		DebugLocation(256, 1);
		} finally { DebugExitRule(GrammarFileName, "func_def"); }
		return value;

	}
        public override Template Visit(FuncDef func_def)
        {
            string prefix = "";

            if (func_def.Attribute.Find(x => x.Name == "inline") != null)
            {
                prefix += "inline ";
            }
            if (this.class_stack.Count() == 0)
            {
                if (func_def.Attribute.Find(x => x.Name == "static") != null)
                {
                    prefix += "static ";
                }
            }
            string suffix = "";

            if (func_def.Attribute.Find(x => x.Name == "const") != null)
            {
                suffix += " const";
            }

            Template template = null;

            if (func_def.GenericParameter.Count() == 0)
            {
                if (func_def.Type == null)
                {
                    template = new Template("<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                    template.Add("type", func_def.Type.Accept(this));
                }
            }
            else
            {
                if (func_def.Type == null)
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                    template.Add("type", func_def.Type.Accept(this));
                }
                template.Add("generics", func_def.GenericParameter.Select(x => string.Format("typename {0}", x)));
            }
            template.Add("prefix", prefix);
            template.Add("suffix", suffix);
            if (func_def.Name == "this")
            {
                template.Add("name", NameInNameSpace(class_stack.First().Name));
            }
            else if (func_def.Name == "~this")
            {
                template.Add("name", NameInNameSpace("~" + class_stack.First().Name));
            }
            else
            {
                template.Add("name", NameInNameSpace(func_def.Name));
            }

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

            foreach (var x in func_def.Args)
            {
                ExprAlloc alloc = new ExprAlloc(x.Type, x.Name, null, AllocType.Declare);
                args_list.Add(alloc.Accept(this));
            }
            template.Add("args", args_list);
            template.Add("list", func_def.Body.Accept(this));
            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);
        }
Beispiel #8
0
 public abstract Template Visit(FuncDef func_def);
Beispiel #9
0
 public abstract Template Visit(FuncDef func_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;
        }
        public override Template Visit(FuncDef func_def)
        {
            string prefix = "";
            if (func_def.Attribute.Find(x => x.Name == "inline") != null)
            {
                prefix += "inline ";
            }
            if (this.class_stack.Count() == 0)
            {
                if (func_def.Attribute.Find(x => x.Name == "static") != null)
                {
                    prefix += "static ";
                }
            }
            string suffix = "";
            if (func_def.Attribute.Find(x => x.Name == "const") != null)
            {
                suffix += " const";
            }

            Template template = null;
            if (func_def.GenericParameter.Count() == 0)
            {
                if (func_def.Type == null)
                {
                    template = new Template("<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                    template.Add("type", func_def.Type.Accept(this));
                }
            }
            else
            {
                if (func_def.Type == null)
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                    template.Add("type", func_def.Type.Accept(this));
                }
                template.Add("generics", func_def.GenericParameter.Select(x => string.Format("typename {0}", x)));
            }
            template.Add("prefix", prefix);
            template.Add("suffix", suffix);
            if (func_def.Name == "this")
                template.Add("name", NameInNameSpace(class_stack.First().Name));
            else if (func_def.Name == "~this")
                template.Add("name", NameInNameSpace("~" + class_stack.First().Name));
            else
                template.Add("name", NameInNameSpace(func_def.Name));

            List<Template> args_list = new List<Template>();
            foreach (var x in func_def.Args)
            {
                ExprAlloc alloc = new ExprAlloc(x.Type, x.Name, null, AllocType.Declare);
                args_list.Add(alloc.Accept(this));
            }
            template.Add("args", args_list);
            template.Add("list", func_def.Body.Accept(this));
            return template;
        }
        public override Template Visit(Enum enum_def)
        {
            if (enum_def.Attribute.Find(x => x.Name == "ToString") != null)
            {
                Attr attr = enum_def.Attribute.Find(x => x.Name == "ToString");

                FuncDef func = new FuncDef();
                func.Type = new IdentType("const char*");
                func.Name = attr.Args.Count() == 0 ? "ToString" : attr.Args.First();
                func.Args.Add(new ExprAlloc(new IdentType("const " + enum_def.Name + "&"), "_t_value", null, AllocType.Declare));
                List<StmtSwitchItem> switch_list = new List<StmtSwitchItem>();
                foreach (var item in enum_def.Values)
                {
                    StmtBlock block = new StmtBlock();
                    block.StmtList.Add(new StmtReturn(new ExprConst("\"" + item + "\"", ConstType.String)));
                    switch_list.Add(new StmtSwitchItem(new List<Expr> { new ExprConst(enum_def.Name + "::" + item, ConstType.Ident) }, block));
                }

                StmtBlock default_block = new StmtBlock();
                {
                    default_block.StmtList.Add(new StmtExpr(new ExprCall(new ExprConst("throw", ConstType.Ident), null, new List<Expr> { new ExprConst("\"Not Found\"", ConstType.String) })));
                }

                StmtSwitch stmt_switch = new StmtSwitch(new ExprConst("_t_value", ConstType.Ident), switch_list, default_block);
                StmtBlock body = new StmtBlock();
                body.StmtList.Add(stmt_switch);
                func.Body = body;
                return func.Accept(this);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Beispiel #13
0
	private FuncDef func_def()
	{
		EnterRule_func_def();
		EnterRule("func_def", 24);
		TraceIn("func_def", 24);
		FuncDef value = default(FuncDef);


		CommonTree pub = default(CommonTree);
		CommonTree vir = default(CommonTree);
		CommonTree deconstructor = default(CommonTree);
		CommonTree op = default(CommonTree);
		List<Attr> attr = default(List<Attr>);
		SugarType a = default(SugarType);
		string b = default(string);
		List<SugarType> x = default(List<SugarType>);
		List<ExprAlloc> args = default(List<ExprAlloc>);
		StmtBlock e = default(StmtBlock);
		Expr f = default(Expr);


			value = new FuncDef();

		try { DebugEnterRule(GrammarFileName, "func_def");
		DebugLocation(287, 1);
		try
		{
			// SugarWalker.g:292:2: ( ^( Func_Def (pub= 'public' )? (vir= 'virtual' )? (attr= attribute )? (a= type_name )? (deconstructor= '~' )? (b= ident |op= ( '+' | '-' | '*' | '/' ) )? (x= generic_parameter )? (args= func_args )? (e= stmt_block |f= expr | Func_Declare ) ) )
			DebugEnterAlt(1);
			// SugarWalker.g:292:4: ^( Func_Def (pub= 'public' )? (vir= 'virtual' )? (attr= attribute )? (a= type_name )? (deconstructor= '~' )? (b= ident |op= ( '+' | '-' | '*' | '/' ) )? (x= generic_parameter )? (args= func_args )? (e= stmt_block |f= expr | Func_Declare ) )
			{
			DebugLocation(292, 4);
			DebugLocation(292, 6);
			Match(input,Func_Def,Follow._Func_Def_in_func_def1209); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(292, 15);
			// SugarWalker.g:292:15: (pub= 'public' )?
			int alt38=2;
			try { DebugEnterSubRule(38);
			try { DebugEnterDecision(38, false);
			int LA38_0 = input.LA(1);

			if ((LA38_0==163))
			{
				alt38 = 1;
			}
			} finally { DebugExitDecision(38); }
			switch (alt38)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:16: pub= 'public'
				{
				DebugLocation(292, 19);
				pub=(CommonTree)Match(input,163,Follow._163_in_func_def1214); 

				}
				break;

			}
			} finally { DebugExitSubRule(38); }

			DebugLocation(292, 31);
			// SugarWalker.g:292:31: (vir= 'virtual' )?
			int alt39=2;
			try { DebugEnterSubRule(39);
			try { DebugEnterDecision(39, false);
			int LA39_0 = input.LA(1);

			if ((LA39_0==178))
			{
				alt39 = 1;
			}
			} finally { DebugExitDecision(39); }
			switch (alt39)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:32: vir= 'virtual'
				{
				DebugLocation(292, 35);
				vir=(CommonTree)Match(input,178,Follow._178_in_func_def1221); 

				}
				break;

			}
			} finally { DebugExitSubRule(39); }

			DebugLocation(292, 48);
			// SugarWalker.g:292:48: (attr= attribute )?
			int alt40=2;
			try { DebugEnterSubRule(40);
			try { DebugEnterDecision(40, false);
			int LA40_0 = input.LA(1);

			if ((LA40_0==Attribute))
			{
				alt40 = 1;
			}
			} finally { DebugExitDecision(40); }
			switch (alt40)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:49: attr= attribute
				{
				DebugLocation(292, 53);
				PushFollow(Follow._attribute_in_func_def1228);
				attr=attribute();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(40); }

			DebugLocation(292, 65);
			// SugarWalker.g:292:65: (a= type_name )?
			int alt41=2;
			try { DebugEnterSubRule(41);
			try { DebugEnterDecision(41, false);
			int LA41_0 = input.LA(1);

			if (((LA41_0>=Type_Array && LA41_0<=Type_Ident)||(LA41_0>=Type_Ref && LA41_0<=Type_Template)))
			{
				alt41 = 1;
			}
			} finally { DebugExitDecision(41); }
			switch (alt41)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:66: a= type_name
				{
				DebugLocation(292, 67);
				PushFollow(Follow._type_name_in_func_def1234);
				a=type_name();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(41); }

			DebugLocation(292, 80);
			// SugarWalker.g:292:80: (deconstructor= '~' )?
			int alt42=2;
			try { DebugEnterSubRule(42);
			try { DebugEnterDecision(42, false);
			int LA42_0 = input.LA(1);

			if ((LA42_0==186))
			{
				alt42 = 1;
			}
			} finally { DebugExitDecision(42); }
			switch (alt42)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:81: deconstructor= '~'
				{
				DebugLocation(292, 94);
				deconstructor=(CommonTree)Match(input,186,Follow._186_in_func_def1241); 

				}
				break;

			}
			} finally { DebugExitSubRule(42); }

			DebugLocation(292, 101);
			// SugarWalker.g:292:101: (b= ident |op= ( '+' | '-' | '*' | '/' ) )?
			int alt43=3;
			try { DebugEnterSubRule(43);
			try { DebugEnterDecision(43, false);
			try
			{
				alt43 = dfa43.Predict(input);
			}
			catch (NoViableAltException nvae)
			{
				DebugRecognitionException(nvae);
				throw;
			}
			} finally { DebugExitDecision(43); }
			switch (alt43)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:102: b= ident
				{
				DebugLocation(292, 103);
				PushFollow(Follow._ident_in_func_def1248);
				b=ident();
				PopFollow();


				}
				break;
			case 2:
				DebugEnterAlt(2);
				// SugarWalker.g:292:112: op= ( '+' | '-' | '*' | '/' )
				{
				DebugLocation(292, 114);

				op=(CommonTree)input.LT(1);
				if (input.LA(1)==95||input.LA(1)==97||input.LA(1)==101||input.LA(1)==109)
				{
					input.Consume();
					state.errorRecovery=false;
				}
				else
				{
					MismatchedSetException mse = new MismatchedSetException(null,input);
					DebugRecognitionException(mse);
					throw mse;
				}


				}
				break;

			}
			} finally { DebugExitSubRule(43); }

			DebugLocation(292, 135);
			// SugarWalker.g:292:135: (x= generic_parameter )?
			int alt44=2;
			try { DebugEnterSubRule(44);
			try { DebugEnterDecision(44, false);
			int LA44_0 = input.LA(1);

			if ((LA44_0==Generic_Patameters))
			{
				alt44 = 1;
			}
			} finally { DebugExitDecision(44); }
			switch (alt44)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:136: x= generic_parameter
				{
				DebugLocation(292, 137);
				PushFollow(Follow._generic_parameter_in_func_def1269);
				x=generic_parameter();
				PopFollow();


				}
				break;

			}
			} finally { DebugExitSubRule(44); }

			DebugLocation(292, 159);
			// SugarWalker.g:292:159: (args= func_args )?
			int alt45=2;
			try { DebugEnterSubRule(45);
			try { DebugEnterDecision(45, false);
			int LA45_0 = input.LA(1);

			if ((LA45_0==Func_Args))
			{
				alt45 = 1;
			}
			} finally { DebugExitDecision(45); }
			switch (alt45)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:292:160: args= func_args
				{
				DebugLocation(292, 164);
				PushFollow(Follow._func_args_in_func_def1277);
				args=func_args();
				PopFollow();

				DebugLocation(292, 175);
				 value.Args = args; 

				}
				break;

			}
			} finally { DebugExitSubRule(45); }

			DebugLocation(293, 2);
			// SugarWalker.g:293:2: (e= stmt_block |f= expr | Func_Declare )
			int alt46=3;
			try { DebugEnterSubRule(46);
			try { DebugEnterDecision(46, false);
			switch (input.LA(1))
			{
			case Stmt_Block:
				{
				alt46 = 1;
				}
				break;
			case Expr_Access:
			case Expr_Alloc_Bracket:
			case Expr_Alloc_Equal:
			case Expr_Bin:
			case Expr_Bracket:
			case Expr_Call:
			case Expr_Call_With:
			case Expr_Cast:
			case Expr_Chain:
			case Expr_Cond:
			case Expr_Cond_Not_Null:
			case Expr_Dict:
			case Expr_Infix:
			case Expr_Lambda:
			case Expr_List:
			case Expr_List_Generation:
			case Expr_New_Array:
			case Expr_New_Type:
			case Expr_Not_Null:
			case Expr_Prefix:
			case Expr_Suffix:
			case Expr_Tuple:
			case Expr_Where:
			case IDENT:
			case Match_Expr:
			case Match_Tuple:
			case NUMBER:
			case STRING:
			case 89:
			case 92:
			case 96:
			case 99:
			case 104:
			case 110:
			case 113:
			case 118:
			case 121:
			case 128:
			case 131:
			case 135:
			case 183:
			case DOUBLE:
				{
				alt46 = 2;
				}
				break;
			case Func_Declare:
				{
				alt46 = 3;
				}
				break;
			default:
				{
					NoViableAltException nvae = new NoViableAltException("", 46, 0, input);
					DebugRecognitionException(nvae);
					throw nvae;
				}
			}

			} finally { DebugExitDecision(46); }
			switch (alt46)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:293:4: e= stmt_block
				{
				DebugLocation(293, 5);
				PushFollow(Follow._stmt_block_in_func_def1288);
				e=stmt_block();
				PopFollow();

				DebugLocation(294, 2);

						if (attr != null) value.Attribute = attr;
						if (pub != null) value.Attribute.Add(new Attr { Name = "public" });
						if (vir != null) value.Attribute.Add(new Attr { Name = "virtual" });
						value.Type = a;
						value.Name = b != null ? b : "operator" + op.Text;
						if (deconstructor != null)
						{
							value.Name = "~" + value.Name;
						}
						if (x != null)
						{
							value.GenericParameter = x;
						}
						value.Body = e;
					

				}
				break;
			case 2:
				DebugEnterAlt(2);
				// SugarWalker.g:310:4: f= expr
				{
				DebugLocation(310, 5);
				PushFollow(Follow._expr_in_func_def1298);
				f=expr();
				PopFollow();

				DebugLocation(311, 2);

						if (attr != null) value.Attribute = attr;
						if (pub != null) value.Attribute.Add(new Attr { Name = "public" });
						if (vir != null) value.Attribute.Add(new Attr { Name = "virtual" });
						value.Type = a;
						value.Name = b != null ? b : "operator" + op.Text;
						if (deconstructor != null)
						{
							value.Name = "~" + value.Name;
						}
						StmtBlock block = new StmtBlock();
						if ((a is IdentType && ((IdentType)a).Type=="void") || a == null)
						{
							block.StmtList.Add(new StmtExpr(f));
						}
						else
						{
							block.StmtList.Add(new StmtReturn(f));
						}
						if (x != null)
						{
							value.GenericParameter = x;
						}
						value.Body = block;
					

				}
				break;
			case 3:
				DebugEnterAlt(3);
				// SugarWalker.g:336:4: Func_Declare
				{
				DebugLocation(336, 4);
				Match(input,Func_Declare,Follow._Func_Declare_in_func_def1306); 
				DebugLocation(337, 2);

						if (attr != null) value.Attribute = attr;
						if (pub != null) value.Attribute.Add(new Attr { Name = "public" });
						if (vir != null) value.Attribute.Add(new Attr { Name = "virtual" });
						value.Attribute.Add(new Attr { Name = "extern" });
						value.Type = a;
						value.Name = b != null ? b : "operator" + op.Text;
						if (deconstructor != null)
						{
							value.Name = "~" + value.Name;
						}
						StmtBlock block = new StmtBlock();
						if ((a is IdentType && ((IdentType)a).Type=="void") || a == null)
						{
							block.StmtList.Add(new StmtExpr(f));
						}
						else
						{
							block.StmtList.Add(new StmtReturn(f));
						}
						if (x != null)
						{
							value.GenericParameter = x;
						}
						value.Body = null;
					

				}
				break;

			}
			} finally { DebugExitSubRule(46); }


			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("func_def", 24);
			LeaveRule("func_def", 24);
			LeaveRule_func_def();
		}
		DebugLocation(364, 1);
		} finally { DebugExitRule(GrammarFileName, "func_def"); }
		return value;

	}
Beispiel #14
0
        public override Template Visit(FuncDef func_def)
        {
            if (this.GenericCount > 0 || func_def.GenericParameter.Count() > 0)
            {
                return(base.Visit(func_def));
            }
            string prefix = "";

            if (func_def.Attribute.Find(x => x.Name == "inline") != null)
            {
                prefix += "inline ";
            }
            if (func_def.Attribute.Find(x => x.Name == "static") != null)
            {
                prefix += "static ";
            }
            if (func_def.Attribute.Find(x => x.Name == "virtual") != null)
            {
                prefix += "virtual ";
            }
            string suffix = "";

            if (func_def.Attribute.Find(x => x.Name == "const") != null)
            {
                suffix += " const";
            }

            Template template = null;

            if (func_def.GenericParameter.Count() == 0)
            {
                if (func_def.Type == null)
                {
                    template = new Template("<prefix><name>(<args; separator=\", \">)<suffix>;");
                }
                else
                {
                    template = new Template("<prefix><type> <name>(<args; separator=\", \">)<suffix>;");
                    template.Add("type", func_def.Type.Accept(this));
                }
            }
            else
            {
                if (func_def.Type == null)
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                }
                else
                {
                    template = new Template("template \\<<generics; separator=\", \">>\n<prefix><type> <name>(<args; separator=\", \">)<suffix> {\n    <list; separator=\"\n\">\n}");
                    template.Add("type", func_def.Type.Accept(this));
                }
                template.Add("generics", func_def.GenericParameter.Select(x => string.Format("typename {0}", x)));
                template.Add("list", func_def.Body.Accept(this));
            }
            template.Add("prefix", prefix);
            template.Add("suffix", suffix);
            if (func_def.Name == "this")
            {
                template.Add("name", class_stack.First().Name);
            }
            else if (func_def.Name == "~this")
            {
                template.Add("name", "~" + class_stack.First().Name);
            }
            else
            {
                template.Add("name", func_def.Name);
            }
            template.Add("args", func_def.Args.Select(x => x.Accept(this)));
            return(template);
        }