Class MacroProcessor

java.lang.Object
edu.iu.jrsalata.MacroProcessor
All Implemented Interfaces:
MacroProcessorInterface

public class MacroProcessor extends Object implements MacroProcessorInterface
The MacroProcessor class is responsible for processing macros in assembly language. It implements the MacroProcessorInterface and provides methods to add lines to a macro, set a label for a macro, and retrieve the processed lines with arguments.
  • Constructor Details

    • MacroProcessor

      public MacroProcessor()
      Constructs a new MacroProcessor with default values. Initializes the label to an empty string, parameters to an empty array, and definition to an empty ArrayList.
    • MacroProcessor

      public MacroProcessor(String[] parameters)
      Constructs a new MacroProcessor with the specified parameters.
      Parameters:
      parameters - an array of strings representing the parameters for the macro processor
  • Method Details

    • addLine

      public void addLine(String line)
      Adds a line to the macro's definition after preprocessing it. The preprocessing involves: 1. Replacing each parameter in the line with its respective position placeholder. The placeholders are in the format of {i}, where i is the index of the parameter. 2. If a label is available, it is prepended to the line followed by three spaces. The label is then reset to an empty string to ensure it is only used once.
      Specified by:
      addLine in interface MacroProcessorInterface
      Parameters:
      line - The line to be added to the macro's definition.
    • setLabel

      public void setLabel(String label)
      Sets the label for the macro processor. Note: This method must be called before adding any lines using addLine(String line).
      Specified by:
      setLabel in interface MacroProcessorInterface
      Parameters:
      label - the label to set
    • getLines

      public Queue<String> getLines(String[] args) throws InvalidAssemblyFileException
      Processes the macro definition by replacing placeholders with the provided arguments.
      Specified by:
      getLines in interface MacroProcessorInterface
      Parameters:
      args - An array of strings representing the arguments to replace the placeholders in the macro definition.
      Returns:
      A Queue of strings where each string is a line from the macro definition with placeholders replaced by the corresponding arguments.
      Throws:
      InvalidAssemblyFileException - If the number of provided arguments does not match the number of parameters in the macro definition.