public Program Import(TextReader input, ImportErrorList errors)
        {
            try
            {
                // Do main import
                ProgramBuilder builder = ImportBuilder(input, errors);

                // Exit if there were errors
                if (errors.ErrorCount > 0)
                    return null;
                else if (builder == null)
                    throw new ImportException("Unknown import error");

                // Create program
                Program program = builder.CreateProgram(Processor, KeepDebugInfo);

                // Validate program
                ProgramValidator.Validate(program, errors);
                return (errors.ErrorCount == 0) ? program : null;
            }
            catch (ImportException e)
            {
                // Add to error list
                errors.AddError(e.Message);
                return null;
            }
        }
Beispiel #2
0
        protected override ProgramBuilder ImportBuilder(TextReader input, ImportErrorList errors)
        {
            // Create disassembler and builder
            InstructionDisassembler disasm = InstructionDisassembler.Create(Processor);
            ProgramBuilder builder = new ProgramBuilder();

            // Disassemble each instruction and add to the builder
            for (int lineNumber = 1; ; lineNumber++)
            {
                string line = input.ReadLine();

                // Eof?
                if (line == null)
                    break;

                // Parse as hex
                int instructionInt;
                if (!int.TryParse(line, NumberStyles.HexNumber, null, out instructionInt))
                {
                    // Fail fast here
                    errors.AddError("Line is not a valid hex string", lineNumber);
                    return null;
                }

                // Try to disassemble and add instruction
                try
                {
                    builder.Add(disasm.Disassemble(instructionInt));
                }
                catch (ImportException e)
                {
                    // Report error
                    errors.AddError(e.Message, lineNumber);
                }
            }

            return builder;
        }