Class Term

  • Direct Known Subclasses:
    Term.Call, Term.Const, Term.Op, Term.Ref

    public abstract class Term
    extends Object
    The abstract Term class is an in-memory representation of an element within an arbitrary expression tree. The class defines a number of concrete Term implementations each representing either an an atomic leave (number constant, symbol reference) or a node (e.g. binary operator, function call) within an expression tree.

    Instances of this class are normally created using an expression parser which implements the Parser interface. The ParserImpl class provides a default implementation of such a parser.

    Version:
    $Revision$ $Date$
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Term.Add
      The numerical ADD operation: n-term + n-term
      static class  Term.AndB
      The logical AND operation: b-term && b-term
      static class  Term.AndI
      The bitwise AND operation: i-term & i-term
      static class  Term.Assign
      The assignment operation variable-ref-term = term
      static class  Term.Binary  
      static class  Term.BinaryB
      An abstract binary (= 2 operands) operation.
      static class  Term.BinaryI
      An abstract binary (= 2 operands) operation with a return type of int.
      static class  Term.BinaryN
      An abstract binary (= 2 operands) operation with a numeric return type.
      static class  Term.Call
      A call of a (or reference to a) Function.
      static class  Term.Cond
      The conditional operation b-term ? term : term
      static class  Term.Const
      A boolean constant, e.g.
      static class  Term.ConstB
      A boolean constant, e.g.
      static class  Term.ConstD
      A floating point constant, e.g.
      static class  Term.ConstI
      An integer constant, e.g.
      static class  Term.ConstS  
      static class  Term.Div
      The numerical DIV operation: n-term / n-term
      static class  Term.EqB
      The boolean EQ operation: b-term == b-term
      static class  Term.EqD
      The double EQ operation: d-term == d-term
      static class  Term.EqI
      The integer EQ operation: i-term == i-term
      static class  Term.GeD
      The double GE operation: d-term >= d-term
      static class  Term.GeI
      The integer GE operation: i-term >= i-term
      static class  Term.GtD
      The double GT operation: d-term > d-term
      static class  Term.GtI
      The integer GT operation: i-term > i-term
      static class  Term.LeD
      The double LE operation: d-term <= d-term
      static class  Term.LeI
      The integer LE operation: i-term <= i-term
      static class  Term.LtD
      The double LT operation: d-term < d-term
      static class  Term.LtI
      The integer LT operation: i-term < i-term
      static class  Term.Mod
      The numerical MOD (modulo) operation: n-term % n-term
      static class  Term.Mul
      The numerical MUL operation: n-term * n-term
      static class  Term.Neg
      The numerical NEG operation: - d-term
      static class  Term.NEqB
      The boolean NEQ operation: b-term != b-term
      static class  Term.NEqD
      The double NEQ operation: d-term != d-term
      static class  Term.NEqI
      The integer NEQ operation: i-term != i-term
      static class  Term.NotB
      The logical NOT operation ! b-term
      static class  Term.NotI
      The bitwise NOT operation: ~ i-term
      static class  Term.Op
      An abstract operation.
      static class  Term.OrB
      The logical OR operation: b-term || b-term
      static class  Term.OrI
      The bitwise OR operation: i-term ¦ i-term
      static class  Term.Ref
      A reference to a Symbol.
      static class  Term.Sub
      The numerical SUB operation: n-term - n-term
      static class  Term.Unary
      An abstract unary (= 1 operand) operation.
      static class  Term.UnaryB
      An abstract unary (= 1 operand) operation with return type of boolean.
      static class  Term.UnaryI
      An abstract unary (= 1 operand) operation with return type of int.
      static class  Term.UnaryN
      An abstract unary (= 1 operand) operation with a numeric return type.
      static class  Term.XOrI
      The bitwise XOR operation: i-term ^ i-term
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int TYPE_B
      The ID for the boolean type.
      static int TYPE_D
      The ID for the double type.
      static int TYPE_I
      The ID for the int type.
      static int TYPE_S
      The ID for the string type.
    • Constructor Summary

      Constructors 
      Constructor Description
      Term()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract <T> T accept​(TermVisitor<T> visitor)
      Visitor support.
      abstract int compare​(Term other)  
      abstract boolean evalB​(EvalEnv context)
      Evaluates this term to a boolean value.
      abstract double evalD​(EvalEnv env)
      Evaluates this term to a double value.
      abstract int evalI​(EvalEnv env)
      Evaluates this term to an int value.
      String evalS​(EvalEnv env)
      Evaluates this term to a String value.
      Term[] getChildren()
      Returns an array of terms which are children of this term.
      abstract int getRetType()
      Gets the term's "natural" return type.
      boolean isB()
      Tests whether or not this term "naturally" returns a boolean.
      abstract boolean isConst()  
      boolean isD()
      Tests whether or not this term "naturally" returns a double.
      boolean isI()
      Tests whether or not this term "naturally" returns an int.
      boolean isN()
      Tests whether or not this term "naturally" returns a numeric value.
      boolean isS()
      Tests whether or not this term returns a String.
      abstract int pre()  
      static boolean toB​(double value)
      Converts a double to a boolean.
      static boolean toB​(int value)
      Converts an int to a boolean.
      static double toD​(boolean value)
      Converts a boolean to an double.
      static int toI​(boolean value)
      Converts a boolean to an int.
      static int toI​(double value)
      Converts a double to an int.
      static String toS​(boolean value)
      Converts a boolean to a String.
      static String toS​(double value)
      Converts a double to a String.
      static String toS​(int value)
      Converts a integer to a String.
      abstract String toString()
      Returns a string representation of this term which can be used for debugging.
    • Constructor Detail

      • Term

        public Term()
    • Method Detail

      • getRetType

        public abstract int getRetType()
        Gets the term's "natural" return type.
        Returns:
        the type, should always be one of the TYPE_X constants defined in this class.
      • evalB

        public abstract boolean evalB​(EvalEnv context)
        Evaluates this term to a boolean value.
        Parameters:
        context - the application dependant environment.
        Returns:
        a boolean value
        Throws:
        EvalException - if the evaluation fails
      • evalI

        public abstract int evalI​(EvalEnv env)
        Evaluates this term to an int value.
        Parameters:
        env - the application dependant environment.
        Returns:
        an int value
        Throws:
        EvalException - if the evaluation fails
      • evalD

        public abstract double evalD​(EvalEnv env)
        Evaluates this term to a double value.
        Parameters:
        env - the application dependant environment.
        Returns:
        a double value
        Throws:
        EvalException - if the evaluation fails
      • accept

        public abstract <T> T accept​(TermVisitor<T> visitor)
        Visitor support.
        Parameters:
        visitor - A visitor.
      • evalS

        public String evalS​(EvalEnv env)
        Evaluates this term to a String value. The default implementation simply returns the value of toString().
        Parameters:
        env - the application dependant environment.
        Returns:
        a String value
        Throws:
        EvalException - if the evaluation fails
      • getChildren

        public Term[] getChildren()
        Returns an array of terms which are children of this term.
        Returns:
        an array of terms, never null but can be empty
      • toString

        public abstract String toString()
        Returns a string representation of this term which can be used for debugging.
        Overrides:
        toString in class Object
      • isB

        public final boolean isB()
        Tests whether or not this term "naturally" returns a boolean.
        Returns:
        true if so
      • isI

        public final boolean isI()
        Tests whether or not this term "naturally" returns an int.
        Returns:
        true if so
      • isD

        public final boolean isD()
        Tests whether or not this term "naturally" returns a double.
        Returns:
        true if so
      • isN

        public final boolean isN()
        Tests whether or not this term "naturally" returns a numeric value.
        Returns:
        true if so
      • isS

        public final boolean isS()
        Tests whether or not this term returns a String.
        Returns:
        true if so
      • toB

        public static boolean toB​(int value)
        Converts an int to a boolean.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value != 0.
      • toB

        public static boolean toB​(double value)
        Converts a double to a boolean.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value != 0.0.
      • toI

        public static int toI​(boolean value)
        Converts a boolean to an int.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value ? 1 : 0.
      • toI

        public static int toI​(double value)
        Converts a double to an int.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is (int) value.
      • toD

        public static double toD​(boolean value)
        Converts a boolean to an double.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value ? 1.0 : 0.0.
      • toS

        public static String toS​(boolean value)
        Converts a boolean to a String.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value ? 1.0 : 0.0.
      • toS

        public static String toS​(int value)
        Converts a integer to a String.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value ? 1.0 : 0.0.
      • toS

        public static String toS​(double value)
        Converts a double to a String.
        Parameters:
        value - the value to be converted
        Returns:
        the conversion result, which is value ? 1.0 : 0.0.
      • isConst

        public abstract boolean isConst()
        Returns:
        true, if this term evaluates to the same constant value regardless of any EvalEnv, even null.
      • compare

        public abstract int compare​(Term other)
      • pre

        public abstract int pre()