Example #1
0
        public static int Main(string[] args)
        {
            try {
                Version v = Assembly.GetExecutingAssembly().GetName().Version;
                string version = v.Major + "." + v.Minor + "." + v.Build;
                Console.WriteLine("While.NET Compiler v" + version);
                Console.WriteLine("Copyright (C) Einar Egilsson 2009. All rights reserved.");
                Console.WriteLine();

                CommandLineOptions options = new CommandLineOptions(args);
                if (options.Empty) {
                    System.Console.Error.WriteLine("ERROR: No inputs specified");
                    return 1;
                } else if (options.Help) {
                    System.Console.Error.WriteLine("Usage: wc.exe [options] filename");
                    CommandLineOptions.Print();
                    return 2;
                } else if (!options.ReadStdIn && !File.Exists(options.InputFilename)) {
                    System.Console.Error.WriteLine("ERROR: File '{0}' does not exist", options.InputFilename);
                    return 3;
                }

                Parser parser;
                if (options.ReadStdIn) {
                    //Passing the StdIn stream directly to the scanner doesn't work well
                    //so we do it this way instead
                    string source = Console.In.ReadToEnd();
                    StreamWriter writer = new StreamWriter(new MemoryStream());
                    writer.Write(source);
                    writer.Flush();
                    writer.BaseStream.Seek(0, SeekOrigin.Begin);
                    parser = new Parser(new Scanner(writer.BaseStream), options);
                } else {
                    parser = new Parser(new Scanner(new FileStream(options.InputFilename, FileMode.Open)), options);
                }
                parser.Parse();
                if (parser.errors.count > 0) {
                    return 1;
                }
                WhileProgram.SymbolTable.Clear();
                PluginLoader pluginLoader = new PluginLoader();
                pluginLoader.Load(options.Plugins);

                foreach (ICompilerPlugin plugin in pluginLoader.LoadedPlugins) {
                    Console.WriteLine("Executing the '{0}' plugin...", plugin.Name);
                    plugin.ProcessSyntaxTree(WhileProgram.Instance);
                }

                WhileProgram.Instance.Compile(options.OutputFilename);
                Console.WriteLine();
                Console.WriteLine("Compiled program to '{0}'", options.OutputFilename);
                return 0;
            } catch (Exception ex) {
                Console.Error.WriteLine("ERROR: " + ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                return 1;
            }
        }