Package edu.iu.jrsalata
Class ObjectWriter
java.lang.Object
edu.iu.jrsalata.ObjectWriter
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionprotected AbstractStatementBuilder
holds the current builder to write its outputprotected String
stores the name of the output fileprotected boolean
previouslyUsed is a boolean that indicates if the writer was used before If so, output needs to be appendedqueue stores the current queue of statements Possibly unnecessary? -
Constructor Summary
ConstructorDescriptionConstructs an ObjectWriter with default settings.ObjectWriter
(String fileName, AbstractStatementBuilder builder, Queue<Statement> queue) Constructs an ObjectWriter with the specified file name, statement builder, and queue of statements. -
Method Summary
Modifier and TypeMethodDescriptionvoid
execute()
Executes the process of writing an object file.void
setBuilder
(AbstractStatementBuilder builder) Sets the builder for this ObjectWriter.void
setFileName
(String fileName) Sets the name of the file.void
Sets the queue of statements to be processed.void
writeDefineRecord
(FileWriter fileWriter, AbstractStatementBuilder builder) Writes a define record to the specified FileWriter using the provided AbstractStatementBuilder.void
writeEndRecord
(FileWriter fileWriter, AbstractStatementBuilder builder) Writes the end record to the specified FileWriter.void
writeHeaderRecord
(FileWriter fileWriter, AbstractStatementBuilder builder) Writes a header record to the specified FileWriter.void
writeModificationRecords
(FileWriter fileWriter, Queue<String> modifications, AbstractStatementBuilder builder) Writes modification records to the provided FileWriter.void
writeReferRecords
(FileWriter fileWriter, AbstractStatementBuilder builder) Writes the reference records to the specified file using the provided FileWriter.void
writeTextRecords
(FileWriter fileWriter, Queue<Statement> queue, AbstractStatementBuilder builder, FileWriter debugWriter) Writes text records to the provided FileWriter based on the given queue of Statements.
-
Field Details
-
fileName
stores the name of the output file -
builder
holds the current builder to write its output -
queue
queue stores the current queue of statements Possibly unnecessary? -
previouslyUsed
protected boolean previouslyUsedpreviouslyUsed 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
Constructs an ObjectWriter with the specified file name, statement builder, and queue of statements.- Parameters:
fileName
- the name of the file to write tobuilder
- the builder used to create statementsqueue
- the queue of statements to be written
-
-
Method Details
-
setFileName
Sets the name of the file.- Specified by:
setFileName
in interfaceObjectWriterInterface
- Parameters:
fileName
- the name of the file to be set
-
setBuilder
Sets the builder for this ObjectWriter.- Specified by:
setBuilder
in interfaceObjectWriterInterface
- Parameters:
builder
- the AbstractStatementBuilder to be used by this ObjectWriter
-
setQueue
Sets the queue of statements to be processed.- Specified by:
setQueue
in interfaceObjectWriterInterface
- Parameters:
queue
- the queue of statements to set
-
execute
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 interfaceObjectWriterInterface
- 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 tobuilder
- 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 tobuilder
- 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 tobuilder
- 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 toqueue
- the queue of Statements to be assembled and writtenbuilder
- the AbstractStatementBuilder used to get starting addresses and other informationdebugWriter
- the FileWriter used to write the generated object code with the original line- Throws:
InvalidAssemblyFileException
- if there is an error in the assembly fileIOException
- 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 tomodifications
- a queue of modification strings to be writtenbuilder
- 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 tobuilder
- the AbstractStatementBuilder containing the starting address- Throws:
IOException
- if an I/O error occurs
-