Class ParserSuryono
java.lang.Object
org.opensourcephysics.ejs.control.value.ParserSuryono
The class
Parser is a mathematical expression parser.Example of code that uses this class:
Parser parser = new Parser(1); // creates parser with one variable
parser.defineVariable(0,"x"); // lets the variable be 'x'
parser.define("Math.sin(x)/x"); // defines function: sin(x)/x
parser.parse(); // parses the function
IMPORTANT: Notice that my variables start at 0 in this version
// calculates: sin(x)/x with x = -5.0 .. +5.0 in 20 steps
// and prints the result to standard output.
double result;
for (i=-10; i <= 10; i++) {
parser.setVariable(0,(double)i/2.0f);
result = parser.evaluate();
System.out.println(result);
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCode damaged.static final intComma expected.static final intExpression expected.static final intInvalid operand.static final intInvalid operator.static final intNo error.static final intNo function definition to parse.static final intOperator expected.static final intParentheses expected.static final intParenthesis mismatch.static final intReferenced name could not be found.static final intStack overflow.static final intSyntax error.static final intToo many constants.static final intAttempt to evaluate an uncompiled function.static final intUnknown identifier. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDefines a function.voiddefineVariable(int index, String name) Sets the variable names.doubleevaluate()Evaluates compiled function.intGets error code of last operation.intGets error position.Gets error string/message of last operation.static String[]getVariableList(String _expression) Gets an expression and returns a String[] of the variables the expression will need.getVariableList(String _expression, Vector<String> varlist) Gets an expression and a Vector of declared variables and updates the Vector with extra variables the expression will need.static booleanWether a given token is a reserved wordvoidparse()Parses defined function.voidsetVariable(int index, double value) Sets the variable value.static StringtoErrorString(int errorcode) Converts error code to error string.
-
Field Details
-
NO_ERROR
public static final int NO_ERRORNo error.- See Also:
-
SYNTAX_ERROR
public static final int SYNTAX_ERRORSyntax error.- See Also:
-
PAREN_EXPECTED
public static final int PAREN_EXPECTEDParentheses expected.- See Also:
-
UNCOMPILED_FUNCTION
public static final int UNCOMPILED_FUNCTIONAttempt to evaluate an uncompiled function.- See Also:
-
EXPRESSION_EXPECTED
public static final int EXPRESSION_EXPECTEDExpression expected.- See Also:
-
UNKNOWN_IDENTIFIER
public static final int UNKNOWN_IDENTIFIERUnknown identifier.- See Also:
-
OPERATOR_EXPECTED
public static final int OPERATOR_EXPECTEDOperator expected.- See Also:
-
PAREN_NOT_MATCH
public static final int PAREN_NOT_MATCHParenthesis mismatch.- See Also:
-
CODE_DAMAGED
public static final int CODE_DAMAGEDCode damaged.- See Also:
-
STACK_OVERFLOW
public static final int STACK_OVERFLOWStack overflow.- See Also:
-
TOO_MANY_CONSTS
public static final int TOO_MANY_CONSTSToo many constants.- See Also:
-
COMMA_EXPECTED
public static final int COMMA_EXPECTEDComma expected.- See Also:
-
INVALID_OPERAND
public static final int INVALID_OPERANDInvalid operand.- See Also:
-
INVALID_OPERATOR
public static final int INVALID_OPERATORInvalid operator.- See Also:
-
NO_FUNC_DEFINITION
public static final int NO_FUNC_DEFINITIONNo function definition to parse.- See Also:
-
REF_NAME_EXPECTED
public static final int REF_NAME_EXPECTEDReferenced name could not be found.- See Also:
-
-
Constructor Details
-
ParserSuryono
public ParserSuryono(int variablecount) The constructor ofParser.- Parameters:
variablecount- the number of variables
-
-
Method Details
-
isKeyword
Wether a given token is a reserved word- Parameters:
token-- Returns:
-
getVariableList
Gets an expression and returns a String[] of the variables the expression will need. This can be used to decide beforehand how many variables a new parser should have. -
getVariableList
Gets an expression and a Vector of declared variables and updates the Vector with extra variables the expression will need. This can be used to decide beforehand how many variables a new parser should have.- Returns:
- the same Vector updated
-
defineVariable
Sets the variable names.- Parameters:
index- the variable index (one based)name- the variable name
-
setVariable
public void setVariable(int index, double value) Sets the variable value. The variable is accessed by index.- Parameters:
index- the variable index (one based)value- the variable value
-
define
Defines a function. Current postfix code becomes invalid.- Parameters:
definition- the function definition
-
parse
public void parse()Parses defined function. -
evaluate
public double evaluate()Evaluates compiled function.- Returns:
- the result of the function
-
getErrorCode
public int getErrorCode()Gets error code of last operation.- Returns:
- the error code
-
getErrorString
Gets error string/message of last operation.- Returns:
- the error string
-
getErrorPosition
public int getErrorPosition()Gets error position. Valid only if error code != NO_ERROR- Returns:
- error position (one based)
-
toErrorString
Converts error code to error string.- Returns:
- the error string
-