Class Evaluator


  • public final class Evaluator
    extends java.lang.Object
    The Evaluator class is responsible for evaluating Expressions and calculating their results, so that the the final value(s) of the input code can be produced.

    It creates a List of Objects that hold the final values generated by the compilation process. However, only values which were printed by the user are added to this list. During this stage no errors are reported, though exceptions can occur if it comes across an undefined Statement or Expression.

    The AnnotatedParseTree produced by the TypeChecker is retrieved, with its contents being parsed recursively to mirror the structure of the tree. The Expressions contained within each individual statement are inspected in an attempt to discover the data types being used and the operation being performed, which is then executed and the result calculated using Java code. This result may be used as part of a large Expression or Statement, such as in a conditional or loop, or returned by the Evaluator as an element in a List object.

    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      Evaluator​(TypeChecker typeChecker, SymbolTable symbolTable, boolean replMode)
      Constructs an Evaluator object initialised with the AnnotatedParseTree from the TypeChecker.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object[] getEvaluation()
      Returns a primitive array of Objects generated from the TypeChecker
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Evaluator

        public Evaluator​(TypeChecker typeChecker,
                         SymbolTable symbolTable,
                         boolean replMode)
        Constructs an Evaluator object initialised with the AnnotatedParseTree from the TypeChecker.

        The Evaluator adds any Symbols to the symbolTable, and initialises a new ArrayList to store output objects.

        If REPL mode is true, the Evaluator will return all calculated values. This should typically be a single value, as the during this mode only a single Expression is parsed at a time.

        Parameters:
        typeChecker - The TypeChecker used to generate the AnnotatedParseTree
        symbolTable - The SymbolTable to store any Symbols that are declared
        replMode - The boolean indicating the Parser should run in REPL mode
    • Method Detail

      • getEvaluation

        public java.lang.Object[] getEvaluation()
                                         throws Exception
        Returns a primitive array of Objects generated from the TypeChecker

        The AnnotatedStatements contained within the AnnotatedParseTree retrieved from the TypeChecker have their contained Expressions evaluated, calculating their results and appended to an array, which is then returned.

        Returns:
        A primitive array of Objects containing the result(s)
        Throws:
        Exception - If the AnnotatedParseTree contains an AnnotatedStatementType that has not been defined