Package util

Class VariableManager.CommandInterpreter

java.lang.Object
util.VariableManager.CommandInterpreter
Enclosing class:
VariableManager

public class VariableManager.CommandInterpreter extends Object
Objects of this class parse a variable initialization or modifying command string. For example var x = 2;var y=3; var z =4; x=9;y=3.123;const k = 3x+z; k=34; var p,q,r=32; const r,s,t=25; z=3.123; All that is needed to execute the parse is to create an object of the class with the available constructor. At the end of the object creation, the parse has been executed and if it is successful, a call to getAccumulator() will return a java.util.List object filled with the Variable objects embedded in the command. Also at the end of the object creation( the call to the constructor of this class) The VARIABLES attribute of the containing class will have been fed with the Variable objects embedded in the command parsed here. The feeding could be the insertion of the Variable objects, if the store doe not already contain them,or updating Variable objects in the store with the incoming ones if they have the same name. The behavior of the parser is such that it interpretes and executes the code on the fly. This means that it will stop inserting data in the VARIABLES only when it detects error in the code.
  • Field Details

    • command

      private String command
    • valid

      private boolean valid
      At every step of parsing, this boolean records if the expression is valid or not.
  • Constructor Details

    • CommandInterpreter

      public CommandInterpreter()
    • CommandInterpreter

      public CommandInterpreter(String command)
      Parameters:
      command - The variable creation command string.
  • Method Details

    • setCommand

      public void setCommand(String command)
    • getCommand

      public String getCommand()
    • setValid

      public void setValid(boolean valid)
    • isValid

      public boolean isValid()
    • getValue

      private String getValue(String expr) throws NullPointerException, InputMismatchException
      Takes a String object that represents a variable name, checks if the variable exists in the store and then returns the value of the variable.
      Parameters:
      expr - The math expression to evaluate
      Returns:
      The value of the expression.
      Throws:
      NullPointerException
      InputMismatchException
    • generateLines

      private List<String> generateLines()
      A line is a statement that ends with a semicolon that is...';' This method generates lines of instructions from the command that is input.
      Returns:
      an ArrayList object containing all lines of instructions in the input command string.
    • isConstant

      private boolean isConstant(String name)
      Parameters:
      name - The name of the Variable whose type is to be checked.
      Returns:
      true if a constant< A Variable object that is constant> by that name is found in the accumulator of variables during expression parsing or in the variable store. It returns false otherwise. A return value of false means one of two things: 1. The item was found and was found not to be a constant but a variable. 2. The item was not found at all.
    • isVariable

      private boolean isVariable(String name)
      Parameters:
      name - The name of the Variable whose type is to be checked.
      Returns:
      true if a variable< A Variable object that is not constant> by that name is found in the accumulator of variables during expression parsing or in the variable store. It returns false otherwise. A return value of false means one of two things: 1. The item was found and was found not to be a variable but a constant. 2. The item was not found at all.
    • validateLine

      private boolean validateLine(String line)
      Validates a single line of Variable initialization or updating statement.
      Parameters:
      line - The line.
      Returns:
      true if the line is valid.
    • analyzeLine

      private void analyzeLine(String line)
      Some scenarios arise here. Scenario: name = value.e.g t=4 Action: Check the variables store to see if a variable by this name exists. If so,the line is correct. But make sure that no line before this one declares it again. e.g store has variable,then var t =4; t=2;.....the t=2; is the scenario now. if the store has declared it then we don't need to declare it before using it again. Else,check if this variable has been declared in a line before this one. If so the line is correct.Else it is wrong,terminate the whole process. Scenario: var|const name = value|expression. var t = 4 or const w=2 Action: Check the variables store to see if a variable by this name exists. If it does exist,the line is wrong,terminate the whole process. Else it is correct. Scenario: name1,name2,name3....,nameN = value|expression e.g. var r,a,t,d=32 or const g,h,i,j=3.14 Action: Check the variables store to see if any of these variables have names that match that of any variable in the store. If they do, the code is wrong. Else it is correct if and only if...their exists no line before this one that declares any of the variables in this line.
    • storeHasVariable

      private boolean storeHasVariable(String name)
      Parameters:
      name - The name of the variable to search for.
      Returns:
      true if the store of variables of objects of the containing class contain a variable or constant having the parameter name.
    • parse

      public void parse()
      Conducts an holistic parse of the input data.