Exemple #1
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;
        }
Exemple #2
0
 public override Template Visit(ExprTuple expr)
 {
     Template template = new Template("std::make_tuple(<list; separator=\", \">)");
     template.Add("list", expr.ExprList.Select(x => x.Accept(this)));
     return template;
 }
Exemple #3
0
	private ExprTuple expr_tuple()
	{
		EnterRule_expr_tuple();
		EnterRule("expr_tuple", 36);
		TraceIn("expr_tuple", 36);
		ExprTuple value = default(ExprTuple);


		Expr a = default(Expr);


			value = new ExprTuple();

		try { DebugEnterRule(GrammarFileName, "expr_tuple");
		DebugLocation(454, 1);
		try
		{
			// SugarWalker.g:459:2: ( ^( Expr_Tuple (a= expr )+ ) )
			DebugEnterAlt(1);
			// SugarWalker.g:459:4: ^( Expr_Tuple (a= expr )+ )
			{
			DebugLocation(459, 4);
			DebugLocation(459, 6);
			Match(input,Expr_Tuple,Follow._Expr_Tuple_in_expr_tuple1817); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(459, 17);
			// SugarWalker.g:459:17: (a= expr )+
			int cnt59=0;
			try { DebugEnterSubRule(59);
			while (true)
			{
				int alt59=2;
				try { DebugEnterDecision(59, false);
				int LA59_0 = input.LA(1);

				if (((LA59_0>=Expr_Access && LA59_0<=Expr_Tuple)||LA59_0==IDENT||LA59_0==Match_Tuple||LA59_0==NUMBER||LA59_0==STRING||LA59_0==75||LA59_0==78||LA59_0==82||LA59_0==85||LA59_0==89||LA59_0==95||LA59_0==98||LA59_0==102||LA59_0==104||LA59_0==110||LA59_0==113||LA59_0==117||LA59_0==150||LA59_0==DOUBLE))
				{
					alt59 = 1;
				}


				} finally { DebugExitDecision(59); }
				switch (alt59)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:459:18: a= expr
					{
					DebugLocation(459, 19);
					PushFollow(Follow._expr_in_expr_tuple1822);
					a=expr();
					PopFollow();

					DebugLocation(459, 25);
					 value.ExprList.Add(a); 

					}
					break;

				default:
					if (cnt59 >= 1)
						goto loop59;

					EarlyExitException eee59 = new EarlyExitException( 59, input );
					DebugRecognitionException(eee59);
					throw eee59;
				}
				cnt59++;
			}
			loop59:
				;

			} finally { DebugExitSubRule(59); }


			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("expr_tuple", 36);
			LeaveRule("expr_tuple", 36);
			LeaveRule_expr_tuple();
		}
		DebugLocation(460, 1);
		} finally { DebugExitRule(GrammarFileName, "expr_tuple"); }
		return value;

	}
Exemple #4
0
 public abstract Template Visit(ExprTuple expr);
Exemple #5
0
 public abstract Template Visit(ExprTuple expr);
	private ExprTuple expr_tuple()
	{
		EnterRule_expr_tuple();
		EnterRule("expr_tuple", 45);
		TraceIn("expr_tuple", 45);
		ExprTuple value = default(ExprTuple);


		Expr a = default(Expr);


			value = new ExprTuple();

		try { DebugEnterRule(GrammarFileName, "expr_tuple");
		DebugLocation(602, 1);
		try
		{
			// SugarWalker.g:607:2: ( ^( Expr_Tuple (a= expr )+ ) )
			DebugEnterAlt(1);
			// SugarWalker.g:607:4: ^( Expr_Tuple (a= expr )+ )
			{
			DebugLocation(607, 4);
			DebugLocation(607, 6);
			Match(input,Expr_Tuple,Follow._Expr_Tuple_in_expr_tuple2292); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(607, 17);
			// SugarWalker.g:607:17: (a= expr )+
			int cnt70=0;
			try { DebugEnterSubRule(70);
			while (true)
			{
				int alt70=2;
				try { DebugEnterDecision(70, false);
				int LA70_0 = input.LA(1);

				if (((LA70_0>=Expr_Access && LA70_0<=Expr_Alloc_Equal)||(LA70_0>=Expr_Bin && LA70_0<=Expr_Where)||LA70_0==IDENT||LA70_0==Match_Expr||LA70_0==Match_Tuple||LA70_0==NUMBER||LA70_0==STRING||LA70_0==89||LA70_0==92||LA70_0==96||LA70_0==99||LA70_0==104||LA70_0==110||LA70_0==113||LA70_0==118||LA70_0==121||LA70_0==128||LA70_0==131||LA70_0==135||LA70_0==183||LA70_0==DOUBLE))
				{
					alt70 = 1;
				}


				} finally { DebugExitDecision(70); }
				switch (alt70)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:607:18: a= expr
					{
					DebugLocation(607, 19);
					PushFollow(Follow._expr_in_expr_tuple2297);
					a=expr();
					PopFollow();

					DebugLocation(607, 25);
					 value.ExprList.Add(a); 

					}
					break;

				default:
					if (cnt70 >= 1)
						goto loop70;

					EarlyExitException eee70 = new EarlyExitException( 70, input );
					DebugRecognitionException(eee70);
					throw eee70;
				}
				cnt70++;
			}
			loop70:
				;

			} finally { DebugExitSubRule(70); }


			Match(input, TokenTypes.Up, null); 


			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("expr_tuple", 45);
			LeaveRule("expr_tuple", 45);
			LeaveRule_expr_tuple();
		}
		DebugLocation(608, 1);
		} finally { DebugExitRule(GrammarFileName, "expr_tuple"); }
		return value;

	}