Class ObjectWriter

java.lang.Object
edu.iu.jrsalata.ObjectWriter
All Implemented Interfaces:
ObjectWriterInterface

public class ObjectWriter extends Object implements ObjectWriterInterface
The ObjectWriter class is responsible for writing object files based on the provided statements and builder. It implements the ObjectWriterInterface. This class supports writing various records such as header, define, refer, text, and end records to the object file.
  • Field Details

    • fileName

      protected String fileName
      stores the name of the output file
    • builder

      protected AbstractStatementBuilder builder
      holds the current builder to write its output
    • queue

      protected Queue<Statement> queue
      queue stores the current queue of statements Possibly unnecessary?
    • previouslyUsed

      protected boolean previouslyUsed
      previouslyUsed is a boolean that indicates if the writer was used before If so, output needs to be appended
  • Constructor Details

    • ObjectWriter

      public ObjectWriter()
      Constructs an ObjectWriter with default settings. Initializes the fileName to "output.obj", creates a new StatementBuilder, initializes the queue as a LinkedList, and sets previouslyUsed to false.
    • ObjectWriter

      public ObjectWriter(String fileName, AbstractStatementBuilder builder, Queue<Statement> queue)
      Constructs an ObjectWriter with the specified file name, statement builder, and queue of statements.
      Parameters:
      fileName - the name of the file to write to
      builder - the builder used to create statements
      queue - the queue of statements to be written
  • Method Details

    • setFileName

      public void setFileName(String fileName)
      Sets the name of the file.
      Specified by:
      setFileName in interface ObjectWriterInterface
      Parameters:
      fileName - the name of the file to be set
    • setBuilder

      public void setBuilder(AbstractStatementBuilder builder)
      Sets the builder for this ObjectWriter.
      Specified by:
      setBuilder in interface ObjectWriterInterface
      Parameters:
      builder - the AbstractStatementBuilder to be used by this ObjectWriter
    • setQueue

      public void setQueue(Queue<Statement> queue)
      Sets the queue of statements to be processed.
      Specified by:
      setQueue in interface ObjectWriterInterface
      Parameters:
      queue - the queue of statements to set
    • execute

      public void execute() throws InvalidAssemblyFileException, IOException
      Executes the process of writing an object file. This method writes various sections of the object file including the header, define, refer, text, and end records. It uses a FileWriter to write to the specified file.
      Specified by:
      execute in interface ObjectWriterInterface
      Throws:
      InvalidAssemblyFileException - if the assembly file is invalid.
      IOException - if an I/O error occurs while writing to the file.
    • writeHeaderRecord

      public void writeHeaderRecord(FileWriter fileWriter, AbstractStatementBuilder builder) throws IOException
      Writes a header record to the specified FileWriter. The header record consists of: - 'H' character at the beginning - Program name (columns 2-7) - Starting address (columns 8-13) - Length of the program (columns 14-19)
      Parameters:
      fileWriter - the FileWriter to write the header record to
      builder - the AbstractStatementBuilder containing the program details
      Throws:
      IOException - if an I/O error occurs
    • writeDefineRecord

      public void writeDefineRecord(FileWriter fileWriter, AbstractStatementBuilder builder) throws IOException
      Writes a define record to the specified FileWriter using the provided AbstractStatementBuilder. A define record starts with 'D' and contains pairs of external symbol names and their addresses. Each name/address pair occupies 12 columns, and the total length of the define record is limited to 73 columns. The define record consists of: - 'D' character at the beginning - Symbol name (columns 2-7) - Symbol address (columns 8-13) - repeat name and address until we reach column 73
      Parameters:
      fileWriter - the FileWriter to write the define record to
      builder - the AbstractStatementBuilder that provides the external definitions
      Throws:
      IOException - if an I/O error occurs
    • writeReferRecords

      public void writeReferRecords(FileWriter fileWriter, AbstractStatementBuilder builder) throws IOException
      Writes the reference records to the specified file using the provided FileWriter. The refer record consists of: - 'R' character at the beginning - Symbol name (columns 2-7) - repeat names of symbols until we reach column 73
      Parameters:
      fileWriter - the FileWriter to write the reference records to
      builder - the AbstractStatementBuilder that provides the external references
      Throws:
      IOException - if an I/O error occurs
    • writeTextRecords

      public void writeTextRecords(FileWriter fileWriter, Queue<Statement> queue, AbstractStatementBuilder builder, FileWriter debugWriter) throws InvalidAssemblyFileException, IOException
      Writes text records to the provided FileWriter based on the given queue of Statements. Each text record is constructed by assembling the statements and appending their byte code. The method also handles different program blocks and ensures that text records are correctly formatted and written to the file. The text record consists of: - 'T' character at the beginning - Starting address (columns 2-7) - Length (in bytes) (columns 8-9) - Object code (10-69)
      Parameters:
      fileWriter - the FileWriter to write the text records to
      queue - the queue of Statements to be assembled and written
      builder - the AbstractStatementBuilder used to get starting addresses and other information
      debugWriter - the FileWriter used to write the generated object code with the original line
      Throws:
      InvalidAssemblyFileException - if there is an error in the assembly file
      IOException - if an I/O error occurs
    • writeModificationRecords

      public void writeModificationRecords(FileWriter fileWriter, Queue<String> modifications, AbstractStatementBuilder builder) throws IOException
      Writes modification records to the provided FileWriter. This method processes a queue of modification strings and writes each one to the FileWriter, followed by a newline character. After processing the queue, it writes any additional modification records stored in the provided AbstractStatementBuilder. The definition of modification records can vary and is defined in the other methods that define them
      Parameters:
      fileWriter - the FileWriter to write the modification records to
      modifications - a queue of modification strings to be written
      builder - an AbstractStatementBuilder containing additional modification records
      Throws:
      IOException - if an I/O error occurs
    • writeEndRecord

      public void writeEndRecord(FileWriter fileWriter, AbstractStatementBuilder builder) throws IOException
      Writes the end record to the specified FileWriter. The end record consists of the character 'E' followed by the starting address if this is the first output. All other outputs are just 'E'
      Parameters:
      fileWriter - the FileWriter to write the end record to
      builder - the AbstractStatementBuilder containing the starting address
      Throws:
      IOException - if an I/O error occurs