Esempio n. 1
0
        public static CompileContext CompileGMLText(string input, CompileContext context, bool redoAssets = false)
        {
            context.Setup(redoAssets);                                    // Set up
            List <Lexer.Token> tokens = Lexer.LexString(context, input);  // Peform lexical analysis

            Parser.Statement block = Parser.ParseTokens(context, tokens); // Parse tokens, make syntax tree

            // Optimize and process syntax tree
            Parser.Statement optimizedBlock = null;
            if (Parser.ErrorMessages.Count == 0)
            {
                optimizedBlock = Parser.Optimize(context, block);
            }

            // Handle errors from either function
            if (Parser.ErrorMessages.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Error{0} parsing code:", Parser.ErrorMessages.Count == 1 ? "" : "s");
                sb.AppendLine();
                sb.AppendLine();
                foreach (string msg in Parser.ErrorMessages)
                {
                    sb.AppendLine(msg);
                }
                context.SetError(sb.ToString());
                return(context);
            }

            CodeWriter codeWriter = AssemblyWriter.AssembleStatement(context, optimizedBlock); // Write assembly code

            context.ResultAssembly = codeWriter.Finish();

            if (codeWriter.ErrorMessages.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Error{0} writing assembly code:", codeWriter.ErrorMessages.Count == 1 ? "" : "s");
                sb.AppendLine();
                sb.AppendLine();
                foreach (string msg in codeWriter.ErrorMessages)
                {
                    sb.AppendLine(msg);
                }
                sb.AppendLine();
                sb.Append(context.ResultAssembly);
                context.SetError(sb.ToString());
                return(context);
            }

            context.OnSuccessfulFinish();
            return(context);
        }