コード例 #1
0
ファイル: TargetCpp.cs プロジェクト: BYVoid/SugarCpp
        public override Template Visit(GlobalAlloc global_alloc)
        {
            string type = global_alloc.Type;
            string name_prefix = "";
            string name_suffix = "";
            while (true)
            {
                if (type.EndsWith("*"))
                {
                    type = type.Substring(0, type.Length - 1);
                    name_prefix = "*" + name_prefix;
                    continue;
                }
                if (type.EndsWith("&"))
                {
                    type = type.Substring(0, type.Length - 1);
                    name_prefix = "&" + name_prefix;
                    continue;
                }
                if (type.EndsWith("[]"))
                {
                    type = type.Substring(0, type.Length - 2);
                    name_suffix = "[]" + name_suffix;
                    continue;
                }
                break;
            }

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

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

            if (global_alloc.ExprList.Count() > 0)
            {
                List<Template> list = new List<Template>();
                foreach (var name in global_alloc.Name)
                {
                    Template stmt = null;
                    if (global_alloc.IsEqualSign)
                    {
                        stmt = new Template("<prefix><type> <name> = <expr; separator=\", \">;");
                    }
                    else
                    {
                        stmt = new Template("<prefix><type> <name>(<expr; separator=\", \">);");
                    }
                    stmt.Add("prefix", prefix);
                    if (type == "auto")
                    {
                        Template tmp = new Template("decltype(<expr; separator=\", \">)");
                        tmp.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                        stmt.Add("type", tmp);
                    }
                    else
                    {
                        stmt.Add("type", type);
                    }
                    stmt.Add("name", string.Format("{0}{1}{2}", name_prefix, name, name_suffix));
                    stmt.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                    list.Add(stmt);
                }
                Template template = new Template("<list; separator=\"\n\">");
                template.Add("list", list);
                return template;
            }
            else
            {
                Template template = new Template("<prefix><type> <name; separator=\", \">;");
                template.Add("prefix", prefix);
                template.Add("type", type);
                template.Add("name", global_alloc.Name.Select(x => string.Format("{0}{1}{2}", name_prefix, x, name_suffix)));
                return template;
            }
        }
コード例 #2
0
ファイル: TargetCpp.cs プロジェクト: BYVoid/SugarCpp
        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;
        }
コード例 #3
0
ファイル: TargetCpp.cs プロジェクト: Connect2Begin/SugarCpp
        public override Template Visit(GlobalAlloc global_alloc)
        {
            Template template = null;

            var type = global_alloc.Type;

            string name_prefix = "";
            string name_suffix = "";
            while (true)
            {
                if (type is StarType)
                {
                    name_prefix = "*" + name_prefix;
                    type = ((StarType)type).Type;
                    continue;
                }
                if (type is RefType)
                {
                    name_prefix = "&" + name_prefix;
                    type = ((RefType)type).Type;
                    continue;
                }
                if (type is ArrayType)
                {
                    Template tmp = new Template("<type_list>");
                    List<Template> type_list = new List<Template>();
                    foreach (var x in ((ArrayType)type).Args)
                    {
                        Template item = new Template("[<expr>]");
                        item.Add("expr", x.Accept(this));
                        type_list.Add(item);
                    }
                    tmp.Add("type_list", type_list);
                    name_suffix = tmp.Render() + name_suffix;
                    type = ((ArrayType)type).Type;
                    continue;
                }
                break;
            }

            string prefix = "";
            if (global_alloc.Attribute.Find(x => x.Name == "static") != null)
            {
                prefix += "static ";
            }
            if (global_alloc.Attribute.Find(x => x.Name == "const") != null)
            {
                prefix += "const ";
            }

            if (type is AutoType)
            {
                // Todo: Check ExprList.Count()
                Debug.Assert(global_alloc.ExprList.Count() == 1);
                type = new DeclType(global_alloc.ExprList.First());
            }

            // Can declare inline
            if (global_alloc.Style == AllocType.Declare)
            {
                template = new Template("<prefix><type> <name; separator=\", \">;");
                template.Add("prefix", prefix);
                template.Add("type", type.Accept(this));
                template.Add("name", global_alloc.Name.Select(x => string.Format("{0}{1}{2}", name_prefix, x, name_suffix)));
                return template;
            }

            List<Template> list = new List<Template>();
            foreach (var name in global_alloc.Name)
            {
                switch (global_alloc.Style)
                {
                    case AllocType.Equal:
                        {
                            Template stmt = new Template("<prefix><type> <name> = <expr; separator=\", \">;");
                            stmt.Add("prefix", prefix);
                            stmt.Add("type", type.Accept(this));
                            stmt.Add("name", string.Format("{0}{1}{2}", name_prefix, name, name_suffix));
                            stmt.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                            list.Add(stmt);
                            break;
                        }

                    case AllocType.Bracket:
                        {
                            Template stmt = new Template("<prefix><type> <name> { <expr; separator=\", \"> } ;");
                            stmt.Add("prefix", prefix);
                            stmt.Add("type", type.Accept(this));
                            stmt.Add("name", string.Format("{0}{1}{2}", name_prefix, name, name_suffix));
                            stmt.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                            list.Add(stmt);
                            break;
                        }
                }
            }

            template = new Template("<list; separator=\"\n\">");
            template.Add("list", list);
            return template;
        }
コード例 #4
0
        public override Template Visit(GlobalAlloc global_alloc)
        {
            Template template = null;

            var type = global_alloc.Type;

            string name_prefix = "";
            string name_suffix = "";

            while (true)
            {
                if (type is StarType)
                {
                    name_prefix = "*" + name_prefix;
                    type        = ((StarType)type).Type;
                    continue;
                }
                if (type is RefType)
                {
                    name_prefix = "&" + name_prefix;
                    type        = ((RefType)type).Type;
                    continue;
                }
                if (type is ArrayType)
                {
                    Template        tmp       = new Template("<type_list>");
                    List <Template> type_list = new List <Template>();
                    foreach (var x in ((ArrayType)type).Args)
                    {
                        Template item = new Template("[<expr>]");
                        item.Add("expr", x.Accept(this));
                        type_list.Add(item);
                    }
                    tmp.Add("type_list", type_list);
                    name_suffix = tmp.Render() + name_suffix;
                    type        = ((ArrayType)type).Type;
                    continue;
                }
                break;
            }

            string prefix = "";

            if (this.class_stack.Count() == 0)
            {
                if (global_alloc.Attribute.Find(x => x.Name == "static") != null)
                {
                    prefix += "static ";
                }
            }
            if (global_alloc.Attribute.Find(x => x.Name == "const") != null)
            {
                prefix += "const ";
            }

            if (type is AutoType)
            {
                // Todo: Check ExprList.Count()
                Debug.Assert(global_alloc.ExprList.Count() == 1);
                type = new DeclType(global_alloc.ExprList.First());
            }

            // Can declare inline
            if (global_alloc.Style == AllocType.Declare)
            {
                template = new Template("<prefix><type> <name; separator=\", \">;");
                template.Add("prefix", prefix);
                template.Add("type", type.Accept(this));
                template.Add("name", global_alloc.Name.Select(x => string.Format("{0}{1}{2}", name_prefix, NameInNameSpace(x), name_suffix)).ToList());
                return(template);
            }

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

            foreach (var name in global_alloc.Name)
            {
                switch (global_alloc.Style)
                {
                case AllocType.Equal:
                {
                    Template stmt = new Template("<prefix><type> <name> = <expr; separator=\", \">;");
                    stmt.Add("prefix", prefix);
                    stmt.Add("type", type.Accept(this));
                    stmt.Add("name", string.Format("{0}{1}{2}", name_prefix, NameInNameSpace(name), name_suffix));
                    stmt.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                    list.Add(stmt);
                    break;
                }

                case AllocType.Bracket:
                {
                    Template stmt = new Template("<prefix><type> <name> { <expr; separator=\", \"> };");
                    stmt.Add("prefix", prefix);
                    stmt.Add("type", type.Accept(this));
                    stmt.Add("name", string.Format("{0}{1}{2}", name_prefix, NameInNameSpace(name), name_suffix));
                    stmt.Add("expr", global_alloc.ExprList.Select(x => x.Accept(this)));
                    list.Add(stmt);
                    break;
                }
                }
            }

            template = new Template("<list; separator=\"\n\">");
            template.Add("list", list);
            return(template);
        }
コード例 #5
0
ファイル: Visitor.cs プロジェクト: BYVoid/SugarCpp
 public abstract Template Visit(GlobalAlloc global_alloc);
コード例 #6
0
 public abstract Template Visit(GlobalAlloc global_alloc);
コード例 #7
0
ファイル: SugarWalker.cs プロジェクト: Connect2Begin/SugarCpp
	private List<GlobalAlloc> global_alloc()
	{
		EnterRule_global_alloc();
		EnterRule("global_alloc", 5);
		TraceIn("global_alloc", 5);
		List<GlobalAlloc> value = default(List<GlobalAlloc>);


		CommonTree has_extern = default(CommonTree);
		List<Attr> attr = default(List<Attr>);
		SugarType a = default(SugarType);
		List<string> b = default(List<string>);
		List<Expr> c = default(List<Expr>);
		List<string> d = default(List<string>);
		List<Expr> e = default(List<Expr>);


			value = new List<GlobalAlloc>();
			GlobalAlloc item;

		try { DebugEnterRule(GrammarFileName, "global_alloc");
		DebugLocation(83, 1);
		try
		{
			// SugarWalker.g:89:2: ( ^( Expr_Alloc_Equal (attr= attribute )? (has_extern= 'extern' )? a= type_name b= ident_list c= expr_list ) | ^( Expr_Alloc_Bracket (attr= attribute )? (has_extern= 'extern' )? a= type_name b= ident_list c= expr_list ) | ^( ':=' (attr= attribute )? (has_extern= 'extern' )? d= ident_list e= expr_list ) )
			int alt11=3;
			try { DebugEnterDecision(11, false);
			switch (input.LA(1))
			{
			case Expr_Alloc_Equal:
				{
				alt11 = 1;
				}
				break;
			case Expr_Alloc_Bracket:
				{
				alt11 = 2;
				}
				break;
			case 113:
				{
				alt11 = 3;
				}
				break;
			default:
				{
					NoViableAltException nvae = new NoViableAltException("", 11, 0, input);
					DebugRecognitionException(nvae);
					throw nvae;
				}
			}

			} finally { DebugExitDecision(11); }
			switch (alt11)
			{
			case 1:
				DebugEnterAlt(1);
				// SugarWalker.g:89:4: ^( Expr_Alloc_Equal (attr= attribute )? (has_extern= 'extern' )? a= type_name b= ident_list c= expr_list )
				{
				DebugLocation(89, 4);
				DebugLocation(89, 6);
				Match(input,Expr_Alloc_Equal,Follow._Expr_Alloc_Equal_in_global_alloc302); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(89, 23);
				// SugarWalker.g:89:23: (attr= attribute )?
				int alt5=2;
				try { DebugEnterSubRule(5);
				try { DebugEnterDecision(5, false);
				int LA5_0 = input.LA(1);

				if ((LA5_0==Attribute))
				{
					alt5 = 1;
				}
				} finally { DebugExitDecision(5); }
				switch (alt5)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:89:24: attr= attribute
					{
					DebugLocation(89, 28);
					PushFollow(Follow._attribute_in_global_alloc307);
					attr=attribute();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(5); }

				DebugLocation(89, 41);
				// SugarWalker.g:89:41: (has_extern= 'extern' )?
				int alt6=2;
				try { DebugEnterSubRule(6);
				try { DebugEnterDecision(6, false);
				int LA6_0 = input.LA(1);

				if ((LA6_0==147))
				{
					alt6 = 1;
				}
				} finally { DebugExitDecision(6); }
				switch (alt6)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:89:42: has_extern= 'extern'
					{
					DebugLocation(89, 52);
					has_extern=(CommonTree)Match(input,147,Follow._147_in_global_alloc314); 

					}
					break;

				}
				} finally { DebugExitSubRule(6); }

				DebugLocation(89, 65);
				PushFollow(Follow._type_name_in_global_alloc320);
				a=type_name();
				PopFollow();

				DebugLocation(89, 77);
				PushFollow(Follow._ident_list_in_global_alloc324);
				b=ident_list();
				PopFollow();

				DebugLocation(89, 90);
				PushFollow(Follow._expr_list_in_global_alloc328);
				c=expr_list();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(90, 2);

						if (c != null && c.Count > 0)
						{
							item = new GlobalAlloc(a, b, c, attr, AllocType.Equal);
							if (has_extern != null) item.Attribute.Add(new Attr { Name = "extern" });
							value.Add(item);
						}
						else
						{
							item = new GlobalAlloc(a, b, c, attr, AllocType.Declare);
							if (has_extern != null) item.Attribute.Add(new Attr { Name = "extern" });
							value.Add(item);
						}
					

				}
				break;
			case 2:
				DebugEnterAlt(2);
				// SugarWalker.g:104:4: ^( Expr_Alloc_Bracket (attr= attribute )? (has_extern= 'extern' )? a= type_name b= ident_list c= expr_list )
				{
				DebugLocation(104, 4);
				DebugLocation(104, 6);
				Match(input,Expr_Alloc_Bracket,Follow._Expr_Alloc_Bracket_in_global_alloc338); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(104, 25);
				// SugarWalker.g:104:25: (attr= attribute )?
				int alt7=2;
				try { DebugEnterSubRule(7);
				try { DebugEnterDecision(7, false);
				int LA7_0 = input.LA(1);

				if ((LA7_0==Attribute))
				{
					alt7 = 1;
				}
				} finally { DebugExitDecision(7); }
				switch (alt7)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:104:26: attr= attribute
					{
					DebugLocation(104, 30);
					PushFollow(Follow._attribute_in_global_alloc343);
					attr=attribute();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(7); }

				DebugLocation(104, 43);
				// SugarWalker.g:104:43: (has_extern= 'extern' )?
				int alt8=2;
				try { DebugEnterSubRule(8);
				try { DebugEnterDecision(8, false);
				int LA8_0 = input.LA(1);

				if ((LA8_0==147))
				{
					alt8 = 1;
				}
				} finally { DebugExitDecision(8); }
				switch (alt8)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:104:44: has_extern= 'extern'
					{
					DebugLocation(104, 54);
					has_extern=(CommonTree)Match(input,147,Follow._147_in_global_alloc350); 

					}
					break;

				}
				} finally { DebugExitSubRule(8); }

				DebugLocation(104, 67);
				PushFollow(Follow._type_name_in_global_alloc356);
				a=type_name();
				PopFollow();

				DebugLocation(104, 79);
				PushFollow(Follow._ident_list_in_global_alloc360);
				b=ident_list();
				PopFollow();

				DebugLocation(104, 92);
				PushFollow(Follow._expr_list_in_global_alloc364);
				c=expr_list();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(105, 2);

						item = new GlobalAlloc(a, b, c, attr, AllocType.Bracket);
						if (has_extern != null) item.Attribute.Add(new Attr { Name = "extern" });
						value.Add(item);
					

				}
				break;
			case 3:
				DebugEnterAlt(3);
				// SugarWalker.g:110:4: ^( ':=' (attr= attribute )? (has_extern= 'extern' )? d= ident_list e= expr_list )
				{
				DebugLocation(110, 4);
				DebugLocation(110, 6);
				Match(input,113,Follow._113_in_global_alloc374); 

				Match(input, TokenTypes.Down, null); 
				DebugLocation(110, 11);
				// SugarWalker.g:110:11: (attr= attribute )?
				int alt9=2;
				try { DebugEnterSubRule(9);
				try { DebugEnterDecision(9, false);
				int LA9_0 = input.LA(1);

				if ((LA9_0==Attribute))
				{
					alt9 = 1;
				}
				} finally { DebugExitDecision(9); }
				switch (alt9)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:110:12: attr= attribute
					{
					DebugLocation(110, 16);
					PushFollow(Follow._attribute_in_global_alloc379);
					attr=attribute();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(9); }

				DebugLocation(110, 29);
				// SugarWalker.g:110:29: (has_extern= 'extern' )?
				int alt10=2;
				try { DebugEnterSubRule(10);
				try { DebugEnterDecision(10, false);
				int LA10_0 = input.LA(1);

				if ((LA10_0==147))
				{
					alt10 = 1;
				}
				} finally { DebugExitDecision(10); }
				switch (alt10)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:110:30: has_extern= 'extern'
					{
					DebugLocation(110, 40);
					has_extern=(CommonTree)Match(input,147,Follow._147_in_global_alloc386); 

					}
					break;

				}
				} finally { DebugExitSubRule(10); }

				DebugLocation(110, 53);
				PushFollow(Follow._ident_list_in_global_alloc392);
				d=ident_list();
				PopFollow();

				DebugLocation(110, 66);
				PushFollow(Follow._expr_list_in_global_alloc396);
				e=expr_list();
				PopFollow();


				Match(input, TokenTypes.Up, null); 

				DebugLocation(111, 2);

						int k = 0;
						for (int i = 0; i < d.Count(); i++)
						{
							item = new GlobalAlloc(new AutoType(), d[i], e[k], attr, AllocType.Equal);
							if (has_extern != null) item.Attribute.Add(new Attr { Name = "extern" });
							value.Add(item);
							k = (k + 1) % e.Count();
						}
					

				}
				break;

			}
		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("global_alloc", 5);
			LeaveRule("global_alloc", 5);
			LeaveRule_global_alloc();
		}
		DebugLocation(121, 1);
		} finally { DebugExitRule(GrammarFileName, "global_alloc"); }
		return value;

	}