http://www.eteks.com

com.eteks.parser
Interface Interpreter

All Known Implementing Classes:
DoubleInterpreter, MathMLInterpreter

public interface Interpreter

Runtime interpreter. This interface specifies the set of methods that computes the value of the different literals, constants, operators and functions available in the Syntax interface. Once a string is parsed by one of the parsers, these methods are called at time of interpretation to evaluate the result of a function or an expression according to the value of its parameters.
The values managed at runtime by the methods are of type Object : This allows to apply computations on a wide range of parameter types, from number types (with the classes Long, Double,...) to strings (instances of String) and also booleans (Boolean). Other classes like java.math.BigDecimal or javax.vecmath.GVector and javax.vecmath.GMatrix can be also used.
Depending on some implementations of this interface, these methods may throw a runtime exception (generally an instance of IllegalArgumentException) if they consider that their parameters have a wrong type to perform the resquested operation or if the key of a constant, operator or function of a given syntax isn't implemented.
Note that an Interpreter implementation isn't obliged to implement all the default keys listed in Syntax. For example, the com.eteks.tools.calculator.JeksCalculator class implements the Syntax interface to recognize only the operators and common functions available in the calculator and uses its own keys. On the other side, its implementation of Interpreter is able to compute only these operators and common functions according to these keys.

Since:
Jeks 1.0
Version:
1.0
Author:
Emmanuel Puybaret
See Also:
Syntax, DoubleInterpreter, com.eteks.tools.calculator.JeksCalculator

Method Summary
 java.lang.Object getBinaryOperatorValue(java.lang.Object binaryOperatorKey, java.lang.Object operand1, java.lang.Object operand2)
          Returns the value of the operation of the binary operator binaryOperatorKey applied on the two operands operand1 and operand2.
 java.lang.Object getCommonFunctionValue(java.lang.Object commonFunctionKey, java.lang.Object param)
          Returns the value of the common function commonFunctionKey with the parameter param.
 java.lang.Object getConditionValue(java.lang.Object paramIf, java.lang.Object paramThen, java.lang.Object paramElse)
          Returns the value paramThen or paramElse depending on whether the condition paramIf being true or false.
 java.lang.Object getConstantValue(java.lang.Object constantKey)
          Returns the value of the constant constantKey.
 java.lang.Object getFunctionValue(Function function, java.lang.Object[] parametersValue, boolean recursiveCall)
          Returns the value of the function function with its parameters parametersValue.
 java.lang.Object getLiteralValue(java.lang.Object literal)
          Returns the value of the literal literal.
 java.lang.Object getParameterValue(java.lang.Object parameter)
          Returns the value of the parameter parameter.
 java.lang.Object getUnaryOperatorValue(java.lang.Object unaryOperatorKey, java.lang.Object operand)
          Returns the value of the operation of the unary operator unaryOperatorKey applied on the operand operand.
 boolean isTrue(java.lang.Object condition)
          Returns true or false according to the value of condition.
 boolean supportsRecursiveCall()
          Returns true if the isTrue () method is able to evaluate its parameter.
 

Method Detail

getLiteralValue

public java.lang.Object getLiteralValue(java.lang.Object literal)
Returns the value of the literal literal. literal may be an instance of Number, String or another class matching the literal recognized by the syntax. This method may throw an exception if the interpreter doesn't accept one of these types.
Parameters:
literal - a literal.
Returns:
the value of the literal. The type of the returned value depends on the implementation.

getParameterValue

public java.lang.Object getParameterValue(java.lang.Object parameter)
Returns the value of the parameter parameter. parameter is one of the values passed to the computeFunction () method of Function to compute the value of a function or the value returned by the getParameterValue () method of ExpressionParameter to compute the value of an expression.
This method may throw an exception if the interpreter doesn't accept the type of parameter.
Parameters:
parameter - a paramater.
Returns:
the value of the parameter. The type of the returned value depends on the implementation.
See Also:
Function.computeFunction(com.eteks.parser.Interpreter, java.lang.Object[]), ExpressionParameter.getParameterValue(java.lang.Object)

getConstantValue

public java.lang.Object getConstantValue(java.lang.Object constantKey)
Returns the value of the constant constantKey. constantKey may be the key of a constant of Syntax (one of CONSTANT_PI, CONSTANT_E, CONSTANT_FALSE, CONSTANT_TRUE or an other user defined key).
Parameters:
constantKey - the key of a constant of Syntax.
Returns:
the value of the constant. The type of the returned value depends on the implementation.

getUnaryOperatorValue

public java.lang.Object getUnaryOperatorValue(java.lang.Object unaryOperatorKey,
                                              java.lang.Object operand)
Returns the value of the operation of the unary operator unaryOperatorKey applied on the operand operand. unaryOperatorKey is the key of an unary operator of Syntax (one of OPERATOR_POSITIVE ,OPERATOR_OPPOSITE, OPERATOR_LOGICAL_NOT, OPERATOR_BITWISE_NOT or an other user defined key).
Parameters:
unaryOperatorKey - the key of an unary operator of Syntax.
operand - the operand (already evaluated if it's an expression).
Returns:
the result of the operation. The type of the returned value depends on the implementation.

getBinaryOperatorValue

public java.lang.Object getBinaryOperatorValue(java.lang.Object binaryOperatorKey,
                                               java.lang.Object operand1,
                                               java.lang.Object operand2)
Returns the value of the operation of the binary operator binaryOperatorKey applied on the two operands operand1 and operand2. binaryOperatorKey is the key of a binary operator of Syntax (one of OPERATOR_ADD, OPERATOR_SUBSTRACT, OPERATOR_MULTIPLY, OPERATOR_DIVIDE,... or an other user defined key).
Parameters:
binaryOperatorKey - the key of a binary operator of Syntax.
operand1 - the first operand (already evaluated if it's an expression).
operand2 - the second operand (already evaluated if it's an expression).
Returns:
the result of the operation. The type of the returned value depends on the implementation.

getCommonFunctionValue

public java.lang.Object getCommonFunctionValue(java.lang.Object commonFunctionKey,
                                               java.lang.Object param)
Returns the value of the common function commonFunctionKey with the parameter param. commonFunctionKey is the key of a commomon function of Syntax (one of FUNCTION_LN, FUNCTION_LOG, FUNCTION_EXP, FUNCTION_SQR,... or an other user defined key).
Parameters:
commonFunctionKey - the key of a common function of Syntax.
param - the parameter of the function (already evaluated if it's an expression).
Returns:
the result of the function. The type of the returned value depends on the implementation.

getConditionValue

public java.lang.Object getConditionValue(java.lang.Object paramIf,
                                          java.lang.Object paramThen,
                                          java.lang.Object paramElse)
Returns the value paramThen or paramElse depending on whether the condition paramIf being true or false. As paramIf, paramThen and paramElse are already evaluated before the call to this method, note that this method will be called to get the result of a condition only if supportsRecursiveCall () returns false (otherwise recursive calls would never end). If supportsRecursiveCall () returns true, only paramThen or paramElse is evaluated depending on whether isTrue (paramIf) returning true or false.
Parameters:
paramIf - the condition.
paramThen - the true condition value.
paramElse - the false condition value.
Returns:
the result of the condition. The type of the returned value depends on the implementation.
See Also:
supportsRecursiveCall()

isTrue

public boolean isTrue(java.lang.Object condition)
Returns true or false according to the value of condition. This method is called internally if supportsRecursiveCall () returns true. It may also be used in other methods of Interpreter to test the boolean value of its operands.
Parameters:
condition - the value to test (already evaluated if it's an expression).
Returns:
true or false.
See Also:
getConditionValue(java.lang.Object, java.lang.Object, java.lang.Object)

supportsRecursiveCall

public boolean supportsRecursiveCall()
Returns true if the isTrue () method is able to evaluate its parameter. isTrue () must be able to evaluate a boolean condition to stop the recursive calls when the stop condition is met during interpretation.
Returns:
true or false.
See Also:
isTrue(java.lang.Object), getConditionValue(java.lang.Object, java.lang.Object, java.lang.Object)

getFunctionValue

public java.lang.Object getFunctionValue(Function function,
                                         java.lang.Object[] parametersValue,
                                         boolean recursiveCall)
Returns the value of the function function with its parameters parametersValue. This method can evaluate the value of the function call symply by computing function.computeFunction (this, parametersValue).
Parameters:
function - the function to compute.
parametersValue - the value of function's parameters (already evaluated if they are expressions).
recursiveCall - true if the call to this function is a recursive call, meaning that the current evaluated function calls itself.
Returns:
the result of the function. The type of the returned value depends on the implementation.

&cp; 1998-2003 eTeks - All rights reserved