Пример #1
0
        // Used by Emblem Magic (and could potentially be used by other external software)
        public static void Assemble(EACodeLanguage language,
                                    TextReader input, BinaryWriter output, ILog log)
        {
            List <string> stringList = new List <string>();

            stringList.Add("_" + language.Name + "_");
            stringList.Add("_EA_");
            using (IPreprocessor preprocessor = new Preprocessor(log))
            {
                preprocessor.AddReserved(language.GetCodeNames());
                preprocessor.AddDefined(stringList.ToArray());
                using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor))
                {
                    new EAExpressionAssembler(language.CodeStorage,
                                              new TokenParser <int>(new Func <string, int>(StringExtensions.GetValue))).Assemble(inputStream, output, log);
                }
            }
        }
Пример #2
0
        private static void Assemble(ILog log)
        {
            TextReader input;
            bool       inputIsFile;

            if (Program.RunConfig.inputFile != null)
            {
                input       = File.OpenText(Program.RunConfig.inputFile);
                inputIsFile = false;
            }
            else
            {
                input       = Console.In;
                inputIsFile = true;
            }

            using (IDirectivePreprocessor preprocessor = new Preprocessor(log)) {
                // preprocessor.AddReserved (eaCodeLanguage.GetCodeNames ());
                preprocessor.AddDefined(new string[] { "_" + Program.RunConfig.language + "_", "_EA_" });

                DependencyMakingIncludeListener depMaker = null;

                if (Program.RunConfig.ppDepEnable)
                {
                    depMaker = new DependencyMakingIncludeListener();
                    preprocessor.IncludeListener = depMaker;
                }

                using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor)) {
                    if (Program.RunConfig.ppSimulation)
                    {
                        // preprocess to null output
                        while (inputStream.ReadLine() != null)
                        {
                            ;
                        }
                    }
                    else
                    {
                        if (Program.RunConfig.outputFile == null)
                        {
                            log.AddError("No output file specified for assembly.");
                            return;
                        }

                        string outFile = Program.RunConfig.outputFile;

                        if (File.Exists(outFile) && File.GetAttributes(outFile).HasFlag((Enum)FileAttributes.ReadOnly))
                        {
                            log.AddError("File `{0}` exists and cannot be written to.", outFile);
                            return;
                        }

                        ChangeStream changeStream = new ChangeStream();

                        using (BinaryWriter output = new BinaryWriter((Stream)changeStream)) {
                            if (!Program.CodesLoaded)
                            {
                                LoadCodes(false);
                            }

                            EACodeLanguage language = Program.Languages [Program.RunConfig.language];

                            EAExpressionAssembler assembler = new EAExpressionAssembler(language.CodeStorage, new TokenParser <int> (new Func <string, int> (StringExtensions.GetValue)));
                            assembler.Assemble(inputStream, output, log);

                            if (Program.RunConfig.symbolOutputFile != null)
                            {
                                // Outputting global symbols to another file

                                try {
                                    if (File.Exists(Program.RunConfig.symbolOutputFile))
                                    {
                                        File.Delete(Program.RunConfig.symbolOutputFile);
                                    }

                                    using (FileStream fileStream = File.OpenWrite(Program.RunConfig.symbolOutputFile))
                                        using (StreamWriter symOut = new StreamWriter(fileStream))
                                            foreach (KeyValuePair <string, int> symbol in assembler.GetGlobalSymbols())
                                            {
                                                symOut.WriteLine("{0}={1}", symbol.Key, symbol.Value.ToHexString("$"));
                                            }
                                } catch (Exception e) {
                                    log.AddError(e.ToString());
                                }
                            }
                        }

                        if (log.ErrorCount == 0)
                        {
                            using (Stream stream = (Stream)File.OpenWrite(outFile))
                                changeStream.WriteToFile(stream);
                        }
                    }
                }

                if (depMaker != null)
                {
                    try {
                        depMaker.GenerateMakeDependencies(log);
                    } catch (Exception e) {
                        log.AddError(e.ToString());
                    }
                }
            }

            if (inputIsFile)
            {
                input.Close();
            }
        }
Пример #3
0
        private static void Compile(ILog log)
        {
            TextReader input;
            bool       inputIsFile;

            if (Program.RunConfig.inputFile != null)
            {
                input       = File.OpenText(Program.RunConfig.inputFile);
                inputIsFile = false;
            }
            else
            {
                input       = Console.In;
                inputIsFile = true;
            }

            using (IDirectivePreprocessor preprocessor = new Preprocessor(log)) {
                // preprocessor.AddReserved (eaCodeLanguage.GetCodeNames ());
                preprocessor.AddDefined(new string[] { "_" + Program.RunConfig.language + "_", "_EA_" });

                DependencyMakingIncludeListener depMaker = null;

                if (Program.RunConfig.ppDepEnable)
                {
                    depMaker = new DependencyMakingIncludeListener();
                    preprocessor.IncludeListener = depMaker;
                }

                using (IInputStream inputStream = new PreprocessingInputStream(input, preprocessor)) {
                    if (Program.RunConfig.ppSimulation)
                    {
                        // preprocess to null output
                        while (inputStream.ReadLine() != null)
                        {
                            ;
                        }
                    }
                    else
                    {
                        if (Program.RunConfig.outputFile == null)
                        {
                            log.AddError("No output file specified for assembly.");
                            return;
                        }

                        string outFile = Program.RunConfig.outputFile;

                        if (File.Exists(outFile) && File.GetAttributes(outFile).HasFlag((Enum)FileAttributes.ReadOnly))
                        {
                            log.AddError("File `{0}` exists and cannot be written to.", outFile);
                            return;
                        }

                        using (StreamWriter output = new StreamWriter(outFile, false, Encoding.Default)) {
                            // Make entry point label global to call in C source file
                            //TODO support ARM?
                            output.WriteLine("\t.thumb");
                            output.WriteLine("\t.global " + Path.GetFileNameWithoutExtension(outFile).Replace(".", "_"));
                            output.WriteLine("\t.include \"event_func_Thumb.inc\"");
                            output.WriteLine("\t.include \"event_func_C.inc\"");
                            output.WriteLine(Path.GetFileNameWithoutExtension(outFile).Replace(".", "_") + ":");

                            if (!Program.CodesLoaded)
                            {
                                LoadCodes(false);
                            }

                            // Console.WriteLine("language: {0}", Program.RunConfig.language);
                            EACodeLanguage language = Program.languages [Program.RunConfig.language];

                            EAExpressionAssembler assembler = new EAExpressionAssembler(language.CodeStorage, new TokenParser <int> (new Func <string, int> (StringExtensions.GetValue)));
                            assembler.Compile(inputStream, output, log);

                            if (Program.RunConfig.symbolOutputFile != null)
                            {
                                // Outputting global symbols to another file

                                try {
                                    if (File.Exists(Program.RunConfig.symbolOutputFile))
                                    {
                                        File.Delete(Program.RunConfig.symbolOutputFile);
                                    }

                                    using (FileStream fileStream = File.OpenWrite(Program.RunConfig.symbolOutputFile))
                                        using (StreamWriter symOut = new StreamWriter(fileStream))
                                            foreach (KeyValuePair <string, int> symbol in assembler.GetGlobalSymbols())
                                            {
                                                symOut.WriteLine("{0}={1}", symbol.Key, symbol.Value.ToHexString("$"));
                                            }
                                } catch (Exception e) {
                                    log.AddError(e.ToString());
                                }
                            }

                            output.Close();
                        }
                    }
                }

                if (depMaker != null)
                {
                    try {
                        depMaker.GenerateMakeDependencies(log);
                    } catch (Exception e) {
                        log.AddError(e.ToString());
                    }
                }
            }

            if (inputIsFile)
            {
                input.Close();
            }
        }