Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
 public override Template Visit(StmtSwitch stmt_switch)
 {
     Template template = new Template("switch (<expr>) {\n<list; separator=\"\n\n\">\n}");
     template.Add("expr", stmt_switch.Expr.Accept(this));
     template.Add("list", stmt_switch.List.Select(x => x.Accept(this)));
     return template;
 }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        public override Template Visit(StmtSwitch stmt_switch)
        {
            if (stmt_switch.Expr != null)
            {
                Template template = new Template("switch (<expr>) {\n<list; separator=\"\n\n\">\n}");
                template.Add("expr", stmt_switch.Expr.Accept(this));
                List<Template> list = stmt_switch.List.Select(x => x.Accept(this)).ToList();
                if (stmt_switch.DefalutBlock != null)
                {
                    Template node = new Template("defalult:\n    <block>");
                    node.Add("block", stmt_switch.DefalutBlock.Accept(this));
                    list.Add(node);
                }
                template.Add("list", list);
                return template;
            }
            else
            {
                Template template = new Template("<list; separator=\" \">");
                List<Template> list = new List<Template>();
                int ct = 0;
                foreach (var x in stmt_switch.List)
                {
                    Template node = null;
                    if (ct++ == 0)
                    {
                        node = new Template("if (<expr>) {\n    <block>\n}");
                    }
                    else
                    {
                        node = new Template("else if (<expr>) {\n    <block>\n}");
                    }

                    if (x.ExprList.Count() == 1)
                    {
                        node.Add("expr", x.ExprList.First().Accept(this));
                    }
                    else
                    {
                        Template tmp = new Template("<list; separator=\" && \">");
                        tmp.Add("list", x.ExprList.Select(i => (new ExprBracket(i)).Accept(this)));
                        node.Add("expr", tmp);
                    }
                    node.Add("block", x.Block.Accept(this));
                    list.Add(node);
                }
                if (stmt_switch.DefalutBlock != null)
                {
                    Template node = new Template("else {\n    <block>\n}");
                    node.Add("block", stmt_switch.DefalutBlock.Accept(this));
                    list.Add(node);
                }
                template.Add("list", list);
                return template;
            }
        }
Exemplo n.º 5
0
 public abstract Template Visit(StmtSwitch stmt_switch);
Exemplo n.º 6
0
 public abstract Template Visit(StmtSwitch stmt_switch);
        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();
            }
        }
Exemplo n.º 8
0
	private Stmt stmt_switch()
	{
		EnterRule_stmt_switch();
		EnterRule("stmt_switch", 29);
		TraceIn("stmt_switch", 29);
		Stmt value = default(Stmt);


		Expr a = default(Expr);
		List<StmtSwitchItem> b = default(List<StmtSwitchItem>);
		StmtBlock c = default(StmtBlock);

		try { DebugEnterRule(GrammarFileName, "stmt_switch");
		DebugLocation(427, 1);
		try
		{
			// SugarWalker.g:428:2: ( ^( Stmt_Switch (a= expr )? b= stmt_switch_item_list (c= stmt_block )? ) )
			DebugEnterAlt(1);
			// SugarWalker.g:428:4: ^( Stmt_Switch (a= expr )? b= stmt_switch_item_list (c= stmt_block )? )
			{
			DebugLocation(428, 4);
			DebugLocation(428, 6);
			Match(input,Stmt_Switch,Follow._Stmt_Switch_in_stmt_switch1568); 

			if (input.LA(1) == TokenTypes.Down)
			{
				Match(input, TokenTypes.Down, null); 
				DebugLocation(428, 18);
				// SugarWalker.g:428:18: (a= expr )?
				int alt51=2;
				try { DebugEnterSubRule(51);
				try { DebugEnterDecision(51, false);
				int LA51_0 = input.LA(1);

				if (((LA51_0>=Expr_Access && LA51_0<=Expr_Alloc_Equal)||(LA51_0>=Expr_Bin && LA51_0<=Expr_Where)||LA51_0==IDENT||LA51_0==Match_Expr||LA51_0==Match_Tuple||LA51_0==NUMBER||LA51_0==STRING||LA51_0==89||LA51_0==92||LA51_0==96||LA51_0==99||LA51_0==104||LA51_0==110||LA51_0==113||LA51_0==118||LA51_0==121||LA51_0==128||LA51_0==131||LA51_0==135||LA51_0==183||LA51_0==DOUBLE))
				{
					alt51 = 1;
				}
				} finally { DebugExitDecision(51); }
				switch (alt51)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:428:19: a= expr
					{
					DebugLocation(428, 20);
					PushFollow(Follow._expr_in_stmt_switch1573);
					a=expr();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(51); }

				DebugLocation(428, 29);
				PushFollow(Follow._stmt_switch_item_list_in_stmt_switch1579);
				b=stmt_switch_item_list();
				PopFollow();

				DebugLocation(428, 52);
				// SugarWalker.g:428:52: (c= stmt_block )?
				int alt52=2;
				try { DebugEnterSubRule(52);
				try { DebugEnterDecision(52, false);
				int LA52_0 = input.LA(1);

				if ((LA52_0==Stmt_Block))
				{
					alt52 = 1;
				}
				} finally { DebugExitDecision(52); }
				switch (alt52)
				{
				case 1:
					DebugEnterAlt(1);
					// SugarWalker.g:428:53: c= stmt_block
					{
					DebugLocation(428, 54);
					PushFollow(Follow._stmt_block_in_stmt_switch1584);
					c=stmt_block();
					PopFollow();


					}
					break;

				}
				} finally { DebugExitSubRule(52); }


				Match(input, TokenTypes.Up, null); 
			}

			DebugLocation(429, 2);

					value = new StmtSwitch(a, b, c);
				

			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("stmt_switch", 29);
			LeaveRule("stmt_switch", 29);
			LeaveRule_stmt_switch();
		}
		DebugLocation(432, 1);
		} finally { DebugExitRule(GrammarFileName, "stmt_switch"); }
		return value;

	}