|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.opensourcephysics.ejs.control.value.ParserSuryono
public final class 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 | |
---|---|
static int |
CODE_DAMAGED
Code damaged. |
static int |
COMMA_EXPECTED
Comma expected. |
static int |
EXPRESSION_EXPECTED
Expression expected. |
static int |
INVALID_OPERAND
Invalid operand. |
static int |
INVALID_OPERATOR
Invalid operator. |
static int |
NO_ERROR
No error. |
static int |
NO_FUNC_DEFINITION
No function definition to parse. |
static int |
OPERATOR_EXPECTED
Operator expected. |
static int |
PAREN_EXPECTED
Parentheses expected. |
static int |
PAREN_NOT_MATCH
Parenthesis mismatch. |
static int |
REF_NAME_EXPECTED
Referenced name could not be found. |
static int |
STACK_OVERFLOW
Stack overflow. |
static int |
SYNTAX_ERROR
Syntax error. |
static int |
TOO_MANY_CONSTS
Too many constants. |
static int |
UNCOMPILED_FUNCTION
Attempt to evaluate an uncompiled function. |
static int |
UNKNOWN_IDENTIFIER
Unknown identifier. |
Constructor Summary | |
---|---|
ParserSuryono(int variablecount)
The constructor of Parser . |
Method Summary | |
---|---|
void |
define(java.lang.String definition)
Defines a function. |
void |
defineVariable(int index,
java.lang.String name)
Sets the variable names. |
double |
evaluate()
Evaluates compiled function. |
int |
getErrorCode()
Gets error code of last operation. |
int |
getErrorPosition()
Gets error position. |
java.lang.String |
getErrorString()
Gets error string/message of last operation. |
static java.lang.String[] |
getVariableList(java.lang.String _expression)
Gets an expression and returns a String[] of the variables the expression will need. |
static java.util.Vector<java.lang.String> |
getVariableList(java.lang.String _expression,
java.util.Vector<java.lang.String> varlist)
Gets an expression and a Vector of declared variables and updates the Vector with extra variables the expression will need. |
static boolean |
isKeyword(java.lang.String token)
Wether a given token is a reserved word |
void |
parse()
Parses defined function. |
void |
setVariable(int index,
double value)
Sets the variable value. |
static java.lang.String |
toErrorString(int errorcode)
Converts error code to error string. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int NO_ERROR
public static final int SYNTAX_ERROR
public static final int PAREN_EXPECTED
public static final int UNCOMPILED_FUNCTION
public static final int EXPRESSION_EXPECTED
public static final int UNKNOWN_IDENTIFIER
public static final int OPERATOR_EXPECTED
public static final int PAREN_NOT_MATCH
public static final int CODE_DAMAGED
public static final int STACK_OVERFLOW
public static final int TOO_MANY_CONSTS
public static final int COMMA_EXPECTED
public static final int INVALID_OPERAND
public static final int INVALID_OPERATOR
public static final int NO_FUNC_DEFINITION
public static final int REF_NAME_EXPECTED
Constructor Detail |
---|
public ParserSuryono(int variablecount)
Parser
.
variablecount
- the number of variablesMethod Detail |
---|
public static boolean isKeyword(java.lang.String token)
token
-
public static java.lang.String[] getVariableList(java.lang.String _expression)
public static java.util.Vector<java.lang.String> getVariableList(java.lang.String _expression, java.util.Vector<java.lang.String> varlist)
public void defineVariable(int index, java.lang.String name)
index
- the variable index (one based)name
- the variable namepublic void setVariable(int index, double value)
index
- the variable index (one based)value
- the variable valuepublic void define(java.lang.String definition)
definition
- the function definitionpublic void parse()
public double evaluate()
public int getErrorCode()
public java.lang.String getErrorString()
public int getErrorPosition()
public static java.lang.String toErrorString(int errorcode)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |