Ejemplo n.º 1
0
    static void Main(string[] args)
    {
        var options = new Options();

        if (!CommandLine.Parser.Default.ParseArguments(args, options))
        {
            Environment.Exit(1);
        }

        var formatter = new Formatter();
        ExceptionOr <Tuple <string, List <IError> > > result = Safe.Wrapper(formatter).Format(options.InputFile);

        if (result.HasFailed())
        {
            Console.Error.WriteLine("Failed to reformat the instructions file: {0}", result.GetException());
            Environment.Exit(2);
        }

        var output    = result.GetValue().Item1;
        var errorList = result.GetValue().Item2;

        if (errorList.Count > 0)
        {
            foreach (var err in errorList)
            {
                Console.Error.WriteLine(err);
            }
            Environment.Exit(3);
        }

        Console.WriteLine("{0}", output);
    }
    static void Main(string[] args)
    {
        var options = new Options();

        if (!CommandLine.Parser.Default.ParseArguments(args, options))
        {
            Environment.Exit(1);
        }

        var compiler = new BytecodeCompiler();
        ExceptionOr <List <int> > result = Safe.Wrapper(compiler).FileToInstructions(options.InputFile);

        if (result.HasFailed())
        {
            Console.Error.WriteLine("Failed to compile the instructions file: {0}", result.GetException().Message);
            Environment.Exit(2);
        }

        if (options.ShowOpcodes)
        {
            foreach (var op in result.GetValue())
            {
                Console.Write("{0} ", op);
            }
            Console.WriteLine();
            return;
        }

        if (options.ShowLayout)
        {
            var bytecode = new Bytecode();
            int address  = 0;

            int length  = result.GetValue().Count;
            var opcodes = result.GetValue();
            int i       = 0;
            while (i < length)
            {
                var insName = bytecode.FindNameByOpcode(opcodes[i]);
                var numArgs = bytecode.GetNumArgs(opcodes[i]);

                Console.Write("{0:0000}:\t{1} ", i, insName);
                i++;
                for (int j = 0; j < numArgs; j++)
                {
                    Console.Write("{0} ", opcodes[i]);
                    i++;
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            return;
        }

        var interpreter = new VM.Interpreter();

        if (options.Debugger != null)
        {
            var monitor = new Monitor();
            monitor.InsertNewLineBefore = true;
            interpreter.SetMonitor(monitor);

            switch (options.Debugger)
            {
            case "tracer":
                interpreter.Attach(new VM.Tracer());
                break;

            case "singlestep":
                interpreter.Attach(new VM.SingleStepDebugger());
                break;

            default:
                Console.Error.WriteLine("\"{0}\" is not a valid debugger. Use: \"tracer\", \"singlestep\"", options.Debugger);
                Environment.Exit(3);
                break;
            }
        }

        var ins = result.GetValue();

        if (ins.Count > 0)
        {
            interpreter.Run(result.GetValue(), 0);
        }
    }