Пример #1
0
        public override Template Visit(StmtFor stmt_for)
        {
            Template template = new Template("<body>");
            template.Add("body", stmt_for.Body.Accept(this));
            Stack<ForItem> stack = new Stack<ForItem>();
            stmt_for.List.ForEach(x => stack.Push(x));
            foreach (var item in stack)
            {
                switch (item.Type)
                {
                    case ForItemType.Each:
                        {
                            var node = (ForItemEach)item;
                            Template tmp = new Template("for (auto <var> : <expr>) {\n    <body>\n}");
                            tmp.Add("var", node.Var);
                            tmp.Add("expr", node.Expr.Accept(this));
                            tmp.Add("body", template);
                            template = tmp;
                            break;
                        }

                    case ForItemType.Map:
                        {
                            var node = (ForItemMap)item;
                            Template tmp = new Template("{\n    auto _t_iterator = <expr>;\n    auto <var> = _t_iterator;\n    <body>\n}");
                            tmp.Add("var", node.Var);
                            tmp.Add("expr", node.Expr.Accept(this));
                            tmp.Add("body", template);
                            template = tmp;
                            break;
                        }

                    case ForItemType.To:
                        {
                            var node = (ForItemRange)item;
                            if (node.By == null)
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> <op> <to>; <prefix_op><var>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("body", template);

                                switch (node.Style)
                                {
                                    case ForItemRangeType.To:
                                        {
                                            tmp.Add("prefix_op", "++");
                                            tmp.Add("op", "<=");
                                            break;
                                        }

                                    case ForItemRangeType.DownTo:
                                        {
                                            tmp.Add("prefix_op", "--");
                                            tmp.Add("op", ">=");
                                            break;
                                        }

                                    case ForItemRangeType.Til:
                                        {
                                            tmp.Add("prefix_op", "++");
                                            tmp.Add("op", "!=");
                                            break;
                                        }
                                }
                                template = tmp;
                            }
                            else
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> <op> <to>; <var> = <var> + <by>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("by", node.By.Accept(this));
                                tmp.Add("body", template);

                                switch (node.Style)
                                {
                                    case ForItemRangeType.To:
                                        tmp.Add("op", "<=");
                                        break;

                                    case ForItemRangeType.DownTo:
                                        tmp.Add("op", ">=");
                                        break;

                                    case ForItemRangeType.Til:
                                        tmp.Add("op", "!=");
                                        break;
                                }
                                template = tmp;
                            }
                            break;
                        }

                    case ForItemType.When:
                        {
                            var node = (ForItemWhen)item;
                            Template tmp = new Template("if (<expr>) {\n    <body>\n}");
                            tmp.Add("expr", node.Expr.Accept(this));
                            tmp.Add("body", template);
                            template = tmp;
                            break;
                        }
                }
            }
            return template;
        }
Пример #2
0
        public override Template Visit(StmtFor stmt_for)
        {
            Template template = new Template("<body>");
            template.Add("body", stmt_for.Body.Accept(this));
            Stack<ForItem> stack = new Stack<ForItem>();
            stmt_for.List.ForEach(x => stack.Push(x));
            foreach (var item in stack)
            {
                switch (item.Type)
                {
                    case ForItemType.Each:
                        {
                            var node = (ForItemEach)item;
                            Template tmp = new Template("for (auto <var> : <expr>) {\n    <body>\n}");
                            tmp.Add("var", node.Var);
                            tmp.Add("expr", node.Expr.Accept(this));
                            tmp.Add("body", template);
                            template = tmp;
                            break;
                        }

                    case ForItemType.To:
                        {
                            var node = (ForItemTo)item;
                            if (node.By == null)
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> \\<= <to>; ++<var>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("body", template);
                                template = tmp;
                            }
                            else
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> \\<= <to>; <var> = <var> + <by>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("by", node.By.Accept(this));
                                tmp.Add("body", template);
                                template = tmp;
                            }
                            break;
                        }

                    case ForItemType.DownTo:
                        {
                            var node = (ForItemDownTo)item;
                            if (node.By == null)
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> >= <to>; --<var>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("body", template);
                                template = tmp;
                            }
                            else
                            {
                                Template tmp = new Template("for (auto <var> = <from>; <var> >= <to>; <var> = <var> + <by>) {\n    <body>\n}");
                                tmp.Add("var", node.Var);
                                tmp.Add("from", node.From.Accept(this));
                                tmp.Add("to", node.To.Accept(this));
                                tmp.Add("by", node.By.Accept(this));
                                tmp.Add("body", template);
                                template = tmp;
                            }
                            break;
                        }

                    case ForItemType.When:
                        {
                            var node = (ForItemWhen)item;
                            Template tmp = new Template("if (<expr>) {\n    <body>\n}");
                            tmp.Add("expr", node.Expr.Accept(this));
                            tmp.Add("body", template);
                            template = tmp;
                            break;
                        }
                }
            }
            return template;
        }
Пример #3
0
	private Stmt stmt_for()
	{
		EnterRule_stmt_for();
		EnterRule("stmt_for", 30);
		TraceIn("stmt_for", 30);
		Stmt value = default(Stmt);


		List<ForItem> a = default(List<ForItem>);
		StmtBlock b = default(StmtBlock);

		try { DebugEnterRule(GrammarFileName, "stmt_for");
		DebugLocation(406, 1);
		try
		{
			// SugarWalker.g:407:2: ( ^( Stmt_For a= for_item_list b= stmt_block ) )
			DebugEnterAlt(1);
			// SugarWalker.g:407:4: ^( Stmt_For a= for_item_list b= stmt_block )
			{
			DebugLocation(407, 4);
			DebugLocation(407, 6);
			Match(input,Stmt_For,Follow._Stmt_For_in_stmt_for1598); 

			Match(input, TokenTypes.Down, null); 
			DebugLocation(407, 16);
			PushFollow(Follow._for_item_list_in_stmt_for1602);
			a=for_item_list();
			PopFollow();

			DebugLocation(407, 32);
			PushFollow(Follow._stmt_block_in_stmt_for1606);
			b=stmt_block();
			PopFollow();


			Match(input, TokenTypes.Up, null); 

			DebugLocation(408, 2);

					value = new StmtFor(a, b);
				

			}

		}
		catch (RecognitionException re)
		{
			ReportError(re);
			Recover(input,re);
		}
		finally
		{
			TraceOut("stmt_for", 30);
			LeaveRule("stmt_for", 30);
			LeaveRule_stmt_for();
		}
		DebugLocation(411, 1);
		} finally { DebugExitRule(GrammarFileName, "stmt_for"); }
		return value;

	}
Пример #4
0
 public ExprListGeneration(SugarType type, StmtFor stmt_for, Expr expr)
 {
     this.For  = stmt_for;
     this.Expr = expr;
     this.Type = type;
 }
Пример #5
0
 public abstract Template Visit(StmtFor stmt_for);
Пример #6
0
 public ExprListGeneration(SugarType type, StmtFor stmt_for, Expr expr)
 {
     this.For = stmt_for;
     this.Expr = expr;
     this.Type = type;
 }
Пример #7
0
 public abstract Template Visit(StmtFor stmt_for);