Ejemplo n.º 1
0
        public static Runner Run(string code, TextReader input, TextWriter output, int memoryLength = DefaultMemoryLength, int stackDepth = DefaultStackDepth, ParseMode parseMode = ParseMode.Interpret)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(null);
            }
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            Runner instructionSet = new Runner(code, memoryLength, stackDepth);

            switch (parseMode)
            {
            case ParseMode.Compile:
                try {
                    instructionSet.Compile();
                } catch { }
                break;
            }
            instructionSet.Run(input, output);

            return(instructionSet);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            try {
                Options options = new Options();
                if (Parser.Default.ParseArguments(args, options))
                {
                    if (options.FileName.Count < 1)
                    {
                        Console.WriteLine("No file is selected");
                        return;
                    }

                    string arg = options.FileName[0], path = "", content;
                    try {
                        path = Path.GetFullPath(arg);
                        FileInfo fileInfo = new FileInfo(path);
                        if (!fileInfo.Exists)
                        {
                            return;
                        }
                        using (StreamReader sr = fileInfo.OpenText())
                            content = sr.ReadToEnd();
                    } catch (ArgumentException) {
                        content = arg;
                    }

                    Runner runner = new Runner(content, options.MemorySize, options.MaxStackDepth);

                    if (!string.IsNullOrEmpty(options.OutputFileName))
                    {
                        if (string.IsNullOrEmpty(options.OutputAssemblyName))
                        {
                            options.OutputAssemblyName = Path.GetFileNameWithoutExtension(options.OutputFileName);
                        }
                        Console.WriteLine("Creating assembly {0} to {1}...", options.OutputAssemblyName, options.OutputFileName);
                        BF2Assembly.CompileToFile(runner, options.OutputAssemblyName, options.OutputFileName);
                        return;
                    }

                    if (!options.Interpret)
                    {
                        runner.Compile();
                    }

                    Console.InputEncoding  = Encoding.ASCII;
                    Console.OutputEncoding = Encoding.ASCII;
                    runner.Run(Console.In, Console.Out);

                    Console.ReadKey(true);
                }
            } catch (Exception ex) {
                Console.OutputEncoding = Encoding.Default;
                Console.Write("Error: ");
                while (ex != null)
                {
                    Console.WriteLine("{0}\n{1}", ex.Message, ex.StackTrace);
                    if (ex == ex.InnerException)
                    {
                        break;
                    }
                    ex = ex.InnerException;
                    if (ex != null)
                    {
                        Console.Write("Inner Exception: ");
                    }
                }
            }
        }