Exemplo n.º 1
0
        public override string GenerateScript(LanguageOption options, int indentationlevel = 0)
        {
            StringBuilder sb;

            if (LoopExpression == null || !(LoopInitializer is AssignmentNode || LoopInitializer is VariableDeclarationNode))
            {   // Convert to a while loop for compatibility
                sb = new StringBuilder();
                if (LoopInitializer != null)
                {
                    sb.Append(Indenter(indentationlevel, "{0}{1}\n", ((ScopedNode)LoopInitializer).GenerateScript(options), Punct.Semi.Value));
                }
                sb.Append(Indenter(indentationlevel, "{0}{1}{2}{3}\n", Keyword.While.Value,
                                   Punct.LPara.Value, LoopExpression == null ? "1" : LoopExpression.GenerateScript(options), Punct.RPara.Value));
                List <IStatement> statements = new List <IStatement>();
                if (Loop is BlockNode)
                {
                    statements.AddRange(((BlockNode)Loop).ChildNodes.Select(node => node as IStatement));
                }
                if (LoopStatement != null)
                {
                    statements.Add(LoopStatement as IStatement);
                }
                sb.Append(BlockNode.GenerateBlock(statements.ToArray(), options, indentationlevel));
            }
            else
            {
                sb = new StringBuilder(Indenter(indentationlevel, "{0}{1}{2}{5}{3}{5}{4}{6}\n", Keyword.For.Value, Punct.LPara.Value,
                                                LoopInitializer == null ? string.Empty : ((ScopedNode)LoopInitializer).GenerateScript(options),
                                                LoopExpression == null ? string.Empty : LoopExpression.GenerateScript(options),
                                                LoopStatement == null ? string.Empty : ((ScopedNode)LoopStatement).GenerateScript(options),
                                                Punct.Semi.Value, Punct.RPara.Value));
                if (Loop == null)
                {
                    sb.Append("{}");
                }
                else if (!options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(Loop is BlockNode))
                {
                    sb.Append(BlockNode.GenerateBlock(new IStatement[] { Loop }, options, indentationlevel));
                }
                else
                {
                    sb.Append(((ScopedNode)Loop).GenerateScript(options, indentationlevel));
                }
            }
            return(sb.ToString());
        }