Class OverloadedOperator

java.lang.Object
com.singularsys.jep.Operator
com.singularsys.jep.misc.overloadedfunctions.OverloadedOperator
All Implemented Interfaces:
Serializable

public class OverloadedOperator extends Operator
An operator representing two alternative operators that can be used in alternative contexts based on the structure of the node. Two alternative operators are defined and the OverloadResolver will substitute the appropriate one when it visits the node.
See Also:
  • Constructor Details

    • OverloadedOperator

      public OverloadedOperator(String name, String symbol, Operator op1, Operator op2, Predicate<Node> pred, int flags, int prec)
      Constructor. The two component operators should both have the same symbol and precedence. The flags for all three operators should be the same, apart from the NOT_IN_PARSER flag which should be set for the two component operators, and unset for the overloaded operator. All three operators should be added to the operator table.
      Parameters:
      name - unique name of the operator
      symbol - the symbol for the operator
      op1 - the first operator, used if the predicate returns true
      op2 - the second operator, used if the predicate returns false
      pred - a predicate that tests the structure of the parse tree
      flags - the flags for the operator, see Operator
      prec - the precedence of the operator
  • Method Details

    • getOp1

      public Operator getOp1()
    • getOp2

      public Operator getOp2()
    • getPredicate

      public Predicate<Node> getPredicate()
    • getOpFor

      public Operator getOpFor(Node n)
      Run the predicate against a node, returning the corresponding operator.
      Parameters:
      n - node to test
      Returns:
      the selected operator
    • extendOperator

      public static OverloadedOperator extendOperator(Jep jep, PostfixMathCommandI fun1, Operator originalOp, Predicate<Node> pred, EmptyOperatorTable.OperatorKey overKey, EmptyOperatorTable.OperatorKey altKey, EmptyOperatorTable.OperatorKey origKey)
      Extends an existing operator to form an overloaded operator, using a additional function and predicate, and add them to the Jep instance.

      This method creates three operators:

      • The original operator that will be used when the predicate returns false.
      • An alternate operator that will be used when the predicate returns true. This uses the supplied PostfixMathCommand.
      • An OverloadedOperator that will be used in parsing.

      The three operators share the same, precedence, symbol and flags but the first two have the NO_IN_PARSER flag set. All three operators are added to the operator table. If the PrintVisitor has a special rule for the original operator then rules are also added for the alternate operator and overloaded operator. Names for alternate operator and overloaded operator are constructed using message properties.

      Parameters:
      jep - Jep instance
      fun1 - pfmc used by first operator
      originalOp - existing operator
      pred - predicate used to test the node
      overKey - key for the overloaded operator
      altKey - key for the alternate operator
      origKey - key for the original operator
      Returns:
      a new OverloadedOperator
    • applyPrintRules

      public static void applyPrintRules(OverloadedOperator op, Jep jep)
      Apply the PrintVisitor.PrintRulesI to an operators created using extendOperator(OperatorTable2, PostfixMathCommandI, Operator, Predicate, OperatorKey, OperatorKey, OperatorKey). This method takes the rule, if specified, for the second operator and applies it to the root operator and the second operator.
      Parameters:
      op -
      jep -
    • extendOperator

      Extends an existing operator to form an overloaded operator, using a additional function and predicate, and add them to the OperatorTable, does not set the PrintRules.

      This method creates three operators:

      • The original operator that will be used when the predicate returns false.
      • An alternate operator that will be used when the predicate returns true. This uses the supplied PostfixMathCommand.
      • An OverloadedOperator that will be used in parsing.

      The three operators share the same, precedence, symbol and flags but the first two have the NO_IN_PARSER flag set. All three operators are added to the operator table. Names for alternate operator and overloaded operator are constructed using message properties.

      If the PrintVisitor.PrintRulesI of the operators need to be set call applyPrintRules(OverloadedOperator, Jep) when the Jep instance is available.

      Parameters:
      ot - the operator table to which the operators should be added
      fun1 - pfmc used by first operator
      originalOp - existing operator
      pred - predicate used to test the node
      overKey - key for the overloaded operator
      altKey - key for the alternate operator
      originalKey - key for the original operator
      Returns:
      a new OverloadedOperator