public override string GenerateScript(LanguageOption options, int indentationlevel = 0) { StringBuilder sb = new StringBuilder(string.Format("{0}{1}{2}{3}{4}\n", Indenter(indentationlevel), Keyword.If.Value, Punct.LPara.Value, Expression.GenerateScript(options), Punct.RPara.Value)); if (!options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(Statement is BlockNode)) { sb.Append(BlockNode.GenerateBlock(Statement == null ? null : new IStatement[] { (IStatement)Statement }, options, indentationlevel)); } else { sb.Append(Statement.GenerateScript(options, indentationlevel)); } if (StatementElse != null) { sb.Append('\n'); sb.Append(Indenter(indentationlevel)); sb.Append(Keyword.Else.Value); sb.Append('\n'); if (!options.HasOption(LanguageOption.UnBracedLoopsIfs) && !(StatementElse is BlockNode)) { sb.Append(BlockNode.GenerateBlock(StatementElse == null ? null : new IStatement[] { (IStatement)StatementElse }, options, indentationlevel)); } else { sb.Append(StatementElse.GenerateScript(options, indentationlevel)); } } return(sb.ToString()); }
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()); }