Exemple #1
0
        public static string ToString(ScriptPackage pack, bool pretty = false)
        {
            StringBuilder b         = new StringBuilder();
            var           indent    = 0;
            var           isNewLine = true;

            foreach (var token in pack.tokens)
            {
                if (isNewLine)
                {
                    if (pretty && indent > 0)
                    {
                        b.Append(new string(' ', indent));
                    }

                    isNewLine = false;
                }
                else
                {
                    b.Append(' ');
                }

                switch (token.token)
                {
                case Tokens.Then:
                {
                    indent += 2;
                    b.AppendLine(token.token.ToString().ToLowerInvariant());
                    isNewLine = true;
                } break;

                case Tokens.Else:
                {
                    b.Length -= 2;
                    b.AppendLine();
                    b.AppendLine(token.token.ToString().ToLowerInvariant());
                    isNewLine = true;
                } break;

                case Tokens.Stop:
                case Tokens.If:
                {
                    b.Append(token.token.ToString().ToLowerInvariant());
                } break;

                case Tokens.Equals:
                {
                    b.Append("=");
                } break;

                case Tokens.NotEquals:
                {
                    b.Append("!=");
                } break;

                case Tokens.Name:
                case Tokens.Number:
                case Tokens.Boolean:
                {
                    b.Append(pack.strValues[token.valueIndex.Value]);
                } break;

                case Tokens.String:
                {
                    b.Append('"');
                    b.Append(pack.strValues[token.valueIndex.Value]);
                    b.Append('"');
                } break;

                case Tokens.Add:
                {
                    b.Append(" + ");
                } break;

                case Tokens.Argument:
                {
                    b.Append($"${pack.strValues[token.valueIndex.Value]}");
                } break;

                case Tokens.End:
                {
                    indent -= 2;
                    b.Append($"\n{token.token.ToString().ToLowerInvariant()}");
                } break;

                case Tokens.None:
                default:
                    break;
                }
            }
            return(b.ToString());
        }