source: trunk/j/src/bsh/commands/eval.bsh @ 2

Last change on this file since 2 was 2, checked in by piso, 18 years ago

Initial checkin.

File size: 1.2 KB
Line 
1
2bsh.help.eval = "usage: eval( String expression )";
3
4/**
5  Evaluate a string as if it were written directly in the current scope,
6  with side effects in the current scope.
7  <p>
8  e.g.
9  <pre>
10    a=5;
11    eval("b=a*2");
12    print(b); // 10
13  </pre>
14  <p>
15
16  eval() acts just like invoked text except that any exceptions generated
17  by the code are captured in a bsh.EvalError.  This includes ParseException
18  for syntactic errors and TargetError for exceptions thrown by the evaluated
19  code.
20  <p>
21  e.g.
22  <pre>
23    try {
24      eval("foo>>><>M>JK$LJLK$");
25    } catch ( EvalError e ) {
26      // ParseException caught here
27    }
28
29    try {
30      eval("(Integer)true");  // illegal cast
31    } catch ( EvalError e ) {
32      // TargetException caught here
33      print( e.getTarget() )  // prints ClassCastException
34    }
35  </pre>
36  <p>
37 
38  If you want eval() to throw target exceptions directly, without wrapping
39  them, you can simply redefine own eval like so:
40
41  <pre>
42    myeval( String expression ) {
43      try {
44        return eval( expression );
45      } catch ( TargetError e ) {
46        throw e.getTarget();
47      }
48    }
49  </pre>
50
51  @return the value of the expression.
52  @throws bsh.EvalError on error
53*/
54eval( String expression ) {
55    return this.interpreter.eval( expression, this.caller.namespace );
56}
Note: See TracBrowser for help on using the repository browser.