Class CodeGenerator

java.lang.Object
math.differentialcalculus.CodeGenerator

public class CodeGenerator extends Object
Objects of this class take the output of the semantic analyzer and uses it to generate code that the derivative engine can work with.
  • Field Details

  • Constructor Details

  • Method Details

    • getScanner

      public ArrayList<String> getScanner()
    • simplifyTokens

      public static void simplifyTokens(ArrayList<String> scan)
      Coordinating method to generate math code almost ready for calculus.
      Parameters:
      scan - An ArrayList object containing math tokens.
    • unbracketNumbers

      private static void unbracketNumbers(ArrayList<String> scan)
      Open up unnecessary brackets..e.g. (5) becomes 5
      Parameters:
      scan - The scanner output.
    • openUpUnnecessaryBrackets

      public static void openUpUnnecessaryBrackets(ArrayList<String> scan)
      Open up unnecessary brackets..e.g. (5) or 3+(2-9+8+cos(4))-7*x..should become 3+2-9+8+cos(4)-7*x
      Parameters:
      scan - The scanner output.
    • bracketVariableProductsAndQuotients

      private static void bracketVariableProductsAndQuotients(ArrayList<String> scan)
      Introduces brackets around products of variables with themselves and with numbers.
      Parameters:
      scan - The scanner output.
    • quicksimplify

      private static void quicksimplify(ArrayList<String> scan)
      Runs through the scanner output and simplifies expressions like 2,*,3..2,+,3...5,^,2 and so on. It does this in readiness for token re-arranging. It also removes some unnecessary brackets.
      Parameters:
      scan - The scanner output.
    • multiplyOutProductBrackets

      private static void multiplyOutProductBrackets(ArrayList<String> scan)
      Attempts to reduce output complexity by multiplying out bracketed expressions that are separated by the * operator.
      Parameters:
      scan - The scanner output.
    • tokenRearranger

      private static void tokenRearranger(ArrayList<String> scan)
      Condenses numbers and variables to be added within a bracket to the end of the bracket. For instance,(3+x+sin(3*x)+5-7) becomes (sin(3*x)+3+x+5-7)
      Parameters:
      scan - The scanner output.
    • cleanUp

      private static void cleanUp(ArrayList<String> scan)
      Removes unconventional code generated and introduces final optimization modifications before the output goes to the differentiator stages.
      Parameters:
      scan - The scanner output.
    • utilitySimplifier

      private static Object[] utilitySimplifier(ArrayList<String> scan, int i)
      Parameters:
      scan - The ArrayList object containing the scanned tokens.
      i - The starting index in the ArrayList.
      Returns:
      an object array containing in the first index, the ending index after simplifying as far as possible and in the second index, the ArrayList object that stores the simplified version of the scanner output in the given range. This stage simplifies portions of the expression that have strings of multiplications and divisions of numbers and variables, generating number products and variable powers. Come here to allow the parser deal with the pattern x^constant_name also. For now, it deals with x^constant_value...e.g. x^3 but not x^a. Once this is done, update the other parser methods as relevant all through the parser.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • main

      public static void main(String[] args)