Пример #1
0
 public void Serialize(List <string> lines)
 {
     this.Commands = ACMDCompiler.CompileCommands(lines.ToArray()).Cast <ICommand>().ToList();
 }
Пример #2
0
        private static List <MoveDef> Compile(string input)
        {
            MoveDef     move = null;
            StringToken lastToken = new StringToken();
            string      curLine = "", codeRegion = "";

            List <string>  lines        = new List <string>();
            List <MoveDef> movedefs     = new List <MoveDef>();
            bool           CodeBlock    = false;
            int            bracketScope = 0;

            foreach (var tok in Tokenizer.Tokenize(input))
            {
                if (tok.Token == "MoveDef")
                {
                    if (move != null)
                    {
                        movedefs.Add(move);
                    }

                    move = new MoveDef();
                }
                if (lastToken.Token == "MoveDef")
                {
                    move.AnimName = tok.Token;
                    goto end;
                }
                if (tok.Token == "Unlisted")
                {
                    move.Unlisted = true;
                    goto end;
                }
                else if (tok.TokType == TokenType.Bracket)
                {
                    if (tok.Token == "{")
                    {
                        bracketScope++;
                    }

                    // End of a code block, try and compile
                    if (CodeBlock && tok.TokType == TokenType.Bracket && bracketScope == 2)
                    {
                        var commands = ACMDCompiler.CompileCommands(lines.ToArray()).ToList();
                        if (commands.Count > 0)
                        {
                            move[codeRegion] = commands;
                        }

                        CodeBlock  = false;
                        codeRegion = string.Empty;
                        lines.Clear();
                    }
                    else if (bracketScope > 2)
                    {
                        curLine += tok.Token;
                    }

                    // Marks the beginning of a code block, indicates that we should start
                    // adding tokens together to form a command string.
                    if (lastToken.Token == "Main" || lastToken.Token == "Effect" ||
                        lastToken.Token == "Expression" || lastToken.Token == "Sound")
                    {
                        CodeBlock  = true;
                        codeRegion = lastToken.Token;
                    }

                    if (tok.Token == "}")
                    {
                        bracketScope--;
                    }
                }
                else if (CodeBlock && tok.Token != "\n" && tok.Token != "\r") // if this isn't a newline, add the token to the code line.
                {
                    curLine += tok.Token;
                    goto end;
                }
                else if (CodeBlock && tok.Token == "\n") // Add the completed code line to the list of lines for compilation
                {
                    if (string.IsNullOrEmpty(curLine))
                    {
                        goto end;
                    }

                    curLine += tok.Token;
                    lines.Add(curLine);
                    curLine = string.Empty;
                }

end:
                if (tok.TokType != TokenType.Seperator)
                {
                    lastToken = tok;
                }
            }
            if (move != null)
            {
                movedefs.Add(move);
            }

            // clean up in case we compile more than one file
            Commands.Clear();
            return(movedefs);
        }