Exemple #1
0
 void assembler_TryHandleParameter(object sender, HandleParameterEventArgs e)
 {
     if (e.Parameter == "--cow")
     {
         Console.WriteLine(cow);
         e.Handled     = true;
         e.StopProgram = true;
     }
 }
Exemple #2
0
        public static int Main(string[] args)
        {
            DateTime startTime  = DateTime.Now;
            int      returnCode = 0;

            DisplaySplash();
            if (args.Length == 0)
            {
                DisplayHelp();
                return(1);
            }
            string    inputFile = null;
            string    outputFile = null;
            string    listingFile = null;
            string    jsonFile = null;
            string    pipe = null;
            string    workingDirectory = Directory.GetCurrentDirectory();
            bool      bigEndian = true, quiet = false, verbose = false;
            Assembler assembler = new Assembler();

            assembler.IncludePath = Environment.GetEnvironmentVariable("ORGINCLUDE");
            if (string.IsNullOrEmpty(assembler.IncludePath))
            {
                assembler.IncludePath = "";
            }
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg.StartsWith("-"))
                {
                    try
                    {
                        switch (arg)
                        {
                        case "-h":
                        case "-?":
                        case "/h":
                        case "/?":
                        case "--help":
                            DisplayHelp();
                            return(1);

                        case "-o":
                        case "--output":
                        case "--output-file":
                            outputFile = args[++i];
                            break;

                        case "--input-file":
                            inputFile = args[++i];
                            break;

                        case "-e":
                        case "--equate":
                            ExpressionResult result = assembler.ParseExpression(args[i + 2]);
                            if (!result.Successful)
                            {
                                Console.WriteLine("Error: " + ListEntry.GetFriendlyErrorMessage(ErrorCode.IllegalExpression));
                                return(1);
                            }
                            assembler.Values.Add(args[i + 1].ToLower(), result.Value);
                            i += 2;
                            break;

                        case "-l":
                        case "--listing":
                            listingFile = args[++i];
                            break;

                        case "--little-endian":
                            bigEndian = false;
                            break;

                        case "--long-literals":
                            assembler.ForceLongLiterals = true;
                            break;

                        case "--quiet":
                        case "-q":
                            quiet = true;
                            break;

                        case "--pipe":
                        case "-p":
                            pipe = args[++i];
                            break;

                        case "--json":
                            jsonFile = args[++i];
                            break;

                        case "--include":
                        case "-i":
                            assembler.IncludePath = Environment.GetEnvironmentVariable("ORGINCLUDE") + ";" + args[++i];
                            break;

                        case "--plugins":
                            ListPlugins(assembler);
                            return(0);

                        case "--working-directory":
                        case "-w":
                            workingDirectory = args[++i];
                            break;

                        case "--verbose":
                        case "-v":
                            verbose = true;
                            break;

                        case "--debug-mode":
                            Console.ReadKey();
                            break;

                        case "--install":
                            assembler.InstallPlugin(args[++i]);
                            return(0);

                        case "--remove":
                            assembler.RemovePlugin(args[++i]);
                            return(0);

                        case "--search":
                            assembler.SearchPlugins(args[++i]);
                            return(0);

                        case "--info":
                            assembler.GetInfo(args[++i]);
                            return(0);

                        default:
                            HandleParameterEventArgs hpea = new HandleParameterEventArgs(arg);
                            hpea.Arguments = args;
                            hpea.Index     = i;
                            if (assembler.TryHandleParameter != null)
                            {
                                assembler.TryHandleParameter(assembler, hpea);
                            }
                            if (!hpea.Handled)
                            {
                                Console.WriteLine("Error: Invalid parameter: " + arg + "\nUse Organic.exe --help for usage information.");
                                return(1);
                            }
                            else
                            {
                                i = hpea.Index;
                            }
                            if (hpea.StopProgram)
                            {
                                return(0);
                            }
                            break;
                        }
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        Console.WriteLine("Error: Missing argument: " + arg + "\nUse Organic.exe --help for usage information.");
                        return(1);
                    }
                }
                else
                {
                    if (inputFile == null)
                    {
                        inputFile = arg;
                    }
                    else if (outputFile == null)
                    {
                        outputFile = arg;
                    }
                    else
                    {
                        Console.WriteLine("Error: Invalid parameter: " + arg + "\nUse Organic.exe --help for usage information.");
                        return(1);
                    }
                }
            }
            if (inputFile == null && pipe == null)
            {
                Console.WriteLine("Error: No input file specified.\nUse Organic.exe --help for usage information.");
                return(1);
            }
            if (outputFile == null)
            {
                outputFile = Path.GetFileNameWithoutExtension(inputFile) + ".bin";
            }
            if (!File.Exists(inputFile) && pipe == null && inputFile != "-")
            {
                Console.WriteLine("Error: File not found (" + inputFile + ")");
                return(1);
            }

            string contents;

            if (pipe == null)
            {
                if (inputFile != "-")
                {
                    StreamReader reader = new StreamReader(inputFile);
                    contents = reader.ReadToEnd();
                    reader.Close();
                }
                else
                {
                    contents = Console.In.ReadToEnd();
                }
            }
            else
            {
                contents = pipe;
            }


            List <ListEntry> output;
            string           wdOld = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(workingDirectory);
            if (pipe == null)
            {
                output = assembler.Assemble(contents, inputFile);
            }
            else
            {
                output = assembler.Assemble(contents, "[piped input]");
            }
            Directory.SetCurrentDirectory(wdOld);

            if (assembler.AssemblyComplete != null)
            {
                assembler.AssemblyComplete(assembler, new AssemblyCompleteEventArgs(output));
            }

            // Output errors
            if (!quiet)
            {
                foreach (var entry in output)
                {
                    if (entry.ErrorCode != ErrorCode.Success)
                    {
                        Console.Error.WriteLine("Error " + entry.FileName + " (line " + entry.LineNumber + "): " +
                                                ListEntry.GetFriendlyErrorMessage(entry.ErrorCode));
                        returnCode = 1;
                    }
                    if (entry.WarningCode != WarningCode.None)
                    {
                        Console.WriteLine("Warning " + entry.FileName + " (line " + entry.LineNumber + "): " +
                                          ListEntry.GetFriendlyWarningMessage(entry.WarningCode));
                    }
                }
            }

            ushort currentAddress = 0;
            Stream binStream      = null;

            if (outputFile != "-")
            {
                if (!string.IsNullOrEmpty(Path.GetDirectoryName(outputFile)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(outputFile));
                }
                binStream = File.Open(outputFile, FileMode.Create);
            }
            foreach (var entry in output)
            {
                if (entry.Output != null)
                {
                    foreach (ushort value in entry.Output)
                    {
                        currentAddress++;
                        byte[] buffer = BitConverter.GetBytes(value);
                        if (bigEndian)
                        {
                            Array.Reverse(buffer);
                        }
                        if (outputFile != "-")
                        {
                            binStream.Write(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            Console.Out.Write(Encoding.ASCII.GetString(buffer));
                        }
                    }
                }
            }

            string listing = "";

            if (listingFile != null || verbose)
            {
                listing = CreateListing(output);
            }

            string json = "";

            if (jsonFile != null)
            {
                json = CreateJson(output);
            }

            if (verbose)
            {
                Console.Write(listing);
            }
            if (listingFile != null)
            {
                if (!string.IsNullOrEmpty(Path.GetDirectoryName(listingFile)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(listingFile));
                }
                var writer = new StreamWriter(listingFile);
                writer.Write(listing);
                writer.Close();
            }

            if (jsonFile != null)
            {
                if (!string.IsNullOrEmpty(Path.GetDirectoryName(jsonFile)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(jsonFile));
                }
                var writer = new StreamWriter(jsonFile);
                writer.Write(json);
                writer.Close();
            }

            TimeSpan duration = DateTime.Now - startTime;

            Console.WriteLine("Organic build complete " + duration.TotalMilliseconds + "ms");
            return(returnCode);
        }