1 8 package org.lsmp.djep.xjep; 9 import org.nfunk.jep.function.PostfixMathCommand; 10 import org.nfunk.jep.*; 11 import java.util.*; 12 13 18 public class Eval extends PostfixMathCommand implements CommandVisitorI 19 { 20 24 public Eval() 25 { 26 super(); 27 numberOfParameters = -1; 28 } 29 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 key = (String ) enume.nextElement(); 42 Object val = xjep.getSymbolTable().getValue(key); 43 localSymTab.addVariable(key,val); 44 } 45 46 for(int i=1;i<nchild;i+=2) 47 { 48 ASTVarNode var; 49 Object 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 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 e) { throw new ParseException(e.getMessage()); } 68 localSymTab.setVarValue(var.getName(),value); 69 } 70 71 try 72 { 73 Object 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 e2) { throw new ParseException(e2.getMessage()); } 79 } 80 81 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 |