public final class RpEval extends Object implements ParserVisitor
To use do
JEP j = ...; Node node = ...; RpEval rpe = new RpEval(j); RpCommandList list = rpe.compile(node); double val = rpe.evaluate(list); System.out.println(val); rpe.cleanUp();The compile methods converts the expression represented by node into a string of commands. For example the expression "1+2*3" will be converted into the sequence of commands
Constant no 1 (pushes constant onto stack) Constant no 2 Constant no 3 Multiply scalers (multiplies last two entries on stack) Add scalers (adds last two entries on stack)The evaluate method executes these methods sequentially using a stack and returns the last object on the stack.
A few cautionary notes: Its very unlikely to be thread safe. It only works over doubles expressions with complex numbers or strings will cause problems. It only works for expressions involving scalers.
Implementation notes A lot of things have been done to make it as fast as possible:
Modifier and Type | Field and Description |
---|---|
static short |
ADD |
static short |
AND |
static short |
ASSIGN |
static short |
CONST
Index for each command
|
static short |
CROSS |
static short |
DIV |
static short |
DOT |
static short |
EQ |
static short |
FUN |
static short |
GE |
static short |
GT |
static short |
LE |
static short |
LIST |
static short |
LT |
static short |
MLIST |
static short |
MOD |
static short |
MUL |
static short |
NE |
static short |
NOT |
static short |
OR |
static short |
POW |
static short |
SUB |
static short |
UMINUS |
static short |
VAR |
static short |
VLIST |
Modifier and Type | Method and Description |
---|---|
void |
cleanUp()
Removes observers and other cleanup needed when evaluator no longer used.
|
RpCommandList |
compile(Node node)
Compile the expressions to produce a set of commands in reverse Polish notation.
|
double |
evaluate(RpCommandList comList)
Evaluate the expression.
|
String |
getFunction(short ref) |
Variable |
getVariable(int ref) |
Object |
visit(ASTConstant node,
Object data) |
Object |
visit(ASTFunNode node,
Object data) |
Object |
visit(ASTStart node,
Object data) |
Object |
visit(ASTVarNode node,
Object data) |
Object |
visit(SimpleNode node,
Object data) |
public static final short CONST
public static final short VAR
public static final short ADD
public static final short SUB
public static final short MUL
public static final short DIV
public static final short MOD
public static final short POW
public static final short AND
public static final short OR
public static final short NOT
public static final short LT
public static final short LE
public static final short GT
public static final short GE
public static final short NE
public static final short EQ
public static final short LIST
public static final short DOT
public static final short CROSS
public static final short ASSIGN
public static final short VLIST
public static final short MLIST
public static final short FUN
public static final short UMINUS
public RpEval(JEP jep)
public final RpCommandList compile(Node node) throws ParseException
ParseException
public final Object visit(ASTStart node, Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final Object visit(SimpleNode node, Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final Object visit(ASTConstant node, Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final Object visit(ASTVarNode node, Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final Object visit(ASTFunNode node, Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final double evaluate(RpCommandList comList)
public void cleanUp()
public Variable getVariable(int ref)
public String getFunction(short ref)
Jas4pp 1.5 © Java Analysis Studio for Particle Physics