Sunday, 15 July 2012

java - How does the arity.jar arithmetic library evaluates an operation String? -



java - How does the arity.jar arithmetic library evaluates an operation String? -

hi came across arity library -> source can found here , found evaluates string arithmetic operation using .eval() method, looking @ source found method of symbols object:

/** evaluates simple look (such "1+1") , returns value. @throws syntaxexception in these cases: <ul> <li> look not well-formed <li> look definition (such "a=1+1") <li> look implicit function (such "x+1") </ul> */ public synchronized double eval(string expression) throws syntaxexception { homecoming compiler.compilesimple(this, expression).eval(); }

this method calls .compilesimple of compiler compile object:

function compilesimple(symbols symbols, string expression) throws syntaxexception { rpn.setconsumer(simplecodegen.setsymbols(symbols)); lexer.scan(expression, rpn); homecoming simplecodegen.getfun(); }

which returns function object , calls eval() method on that. looking @ function.eval() method saw this:

/** evaluates arity-0 function (a function no arguments). @return value of function */ public double eval() { throw new arityexception(0); }

the method eval must homecoming double type , implementation throws arityexception has implementation:

public class arityexception extends runtimeexception { public arityexception(string mes) { super(mes); } public arityexception(int nargs) { this("didn't expect " + nargs + " arguments"); } }

but when arityexception thrown calls super() constructor of runtimeexception, , exception , no double returned should, maybe have mess passages, didn't understand lastly throw new arityexception of 0 within function.eval() implementation.

so how works?

you missed declaration of simplecodegen:

private final simplecodegen simplecodegen = new simplecodegen(exception);

that means compilesimple(...) returns compiledfunction, extends contextfunction , such function.

compiledfunction getfun() { homecoming new compiledfunction(0, code.toarray(), consts.getre(), consts.getim(), funcs.toarray()); }

so it's eval(...) function in contextfunction beingness called. 1 has real implementation.

doing code analysis without ide , watching code can tricky. using debugger , stepping through show programme flow.

java eval

No comments:

Post a Comment