KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > Eval


1 /* @author rich
2  * Created on 18-Jun-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep;
9 import org.nfunk.jep.function.PostfixMathCommand;
10 import org.nfunk.jep.*;
11 import java.util.*;
12
13 /**
14  * Symbolic eval(x^3,x,2) operator.
15  * @author R Morris.
16  * Created on 18-Jun-2003
17  */

18 public class Eval extends PostfixMathCommand implements CommandVisitorI
19 {
20     /**
21      * Create a function that evaluates the lhs with values given on rhs.
22      * e.g. eval(f,x,1,y,2) sets variable x to 1, y to 2 and evaluates f.
23      */

24     public Eval()
25     {
26         super();
27         numberOfParameters = -1;
28     }
29     //TODO probably broken
30
public Node process(Node node,Node children[],XJep xjep) throws ParseException
31     {
32         Vector errorList = new Vector();
33         int nchild = children.length;
34         if(nchild %2 == 0)
35             throw new ParseException("Number of parameters must be odd");
36         XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) xjep.getSymbolTable()).newInstance();
37         XJep localJep = xjep.newInstance(localSymTab);
38
39         for(Enumeration enume = xjep.getSymbolTable().keys();enume.hasMoreElements();)
40         {
41             String JavaDoc key = (String JavaDoc) enume.nextElement();
42             Object JavaDoc val = xjep.getSymbolTable().getValue(key);
43             localSymTab.addVariable(key,val);
44         }
45         /** first evaluate the arguments **/
46         for(int i=1;i<nchild;i+=2)
47         {
48             ASTVarNode var;
49             Object JavaDoc value;
50             try
51             {
52                 var = (ASTVarNode) children[i];
53                 Node rhs = children[i+1];
54                 if( rhs instanceof ASTConstant)
55                     value = ((ASTConstant) rhs).getValue();
56                 else
57                 {
58                     value = localJep.evaluate(rhs);
59                     if(!errorList.isEmpty())
60                         throw new ParseException(errorList.toString());
61                 }
62             }
63             catch(ClassCastException JavaDoc e)
64             {
65                 throw new ParseException("Format should be eval(f,x,1,y,2) where x,y are variables and 1,2 are constants");
66             }
67             catch(Exception JavaDoc e) { throw new ParseException(e.getMessage()); }
68             localSymTab.setVarValue(var.getName(),value);
69         }
70         /** now evaluate the equation **/
71         try
72         {
73             Object JavaDoc res = localJep.evaluate(node.jjtGetChild(0));
74             if(!errorList.isEmpty())
75                 throw new ParseException(errorList.toString());
76             return xjep.getNodeFactory().buildConstantNode(res);
77         }
78         catch(Exception JavaDoc e2) { throw new ParseException(e2.getMessage()); }
79     }
80     
81     /**
82      * Should not be called by evaluator
83      * @throws ParseException if run.
84      */

85     public void run(Stack s) throws ParseException
86     {
87         throw new ParseException("Eval should not be called by Evaluator");
88     }
89 }
90
Popular Tags