1 9 10 package org.nfunk.jep; 11 12 import java.util.*; 13 import org.nfunk.jep.function.PostfixMathCommandI; 14 import org.nfunk.jep.function.SpecialEvaluationI; 15 16 32 public class EvaluatorVisitor implements ParserVisitor { 33 34 protected Stack stack; 35 36 37 protected Vector errorList; 38 39 40 protected SymbolTable symTab; 41 42 43 protected boolean errorFlag; 44 45 46 private static final boolean debug = false; 47 48 49 public EvaluatorVisitor() { 50 errorList = null; 51 symTab = null; 52 stack = new Stack(); 53 } 54 55 58 protected void addToErrorList(String errorStr) { 59 if (errorList != null) { 60 errorList.addElement(errorStr); 61 } 62 } 63 64 80 public Object getValue( 81 Node topNode, 82 Vector errorList_in, 83 SymbolTable symTab_in) 84 throws Exception { 85 86 if (topNode == null) { 88 throw new IllegalArgumentException ("topNode parameter is null"); 89 } 90 91 errorList = errorList_in; 93 symTab = symTab_in; 94 errorFlag = false; 95 stack.removeAllElements(); 96 99 try { 101 topNode.jjtAccept(this, null); 102 } catch (ParseException e) { 103 this.addToErrorList(e.getMessage()); 104 throw e; 105 } 106 107 if (errorFlag || stack.size() != 1) { 110 throw new Exception ("EvaluatorVisitor.getValue(): Error during evaluation"); 111 } 112 113 return stack.pop(); 115 } 116 117 121 public Object visit(SimpleNode node, Object data) throws ParseException { 122 throw new ParseException( 123 "No visit method for " + node.getClass().toString()); 124 } 125 126 130 public Object visit(ASTStart node, Object data) throws ParseException { 131 throw new ParseException("Start node encountered during evaluation"); 132 } 133 134 142 public Object visit(ASTFunNode node, Object data) throws ParseException { 143 144 if (node == null) 145 return null; 146 PostfixMathCommandI pfmc = node.getPFMC(); 147 148 if (pfmc == null) 150 throw new ParseException( 151 "No function class associated with " + node.getName()); 152 153 if (pfmc instanceof SpecialEvaluationI) { 159 return ((SpecialEvaluationI) node.getPFMC()).evaluate( 160 node,data,this,stack); 161 } 162 163 if (debug == true) { 164 System.out.println( 165 "Stack size before childrenAccept: " + stack.size()); 166 } 167 168 170 data = node.childrenAccept(this, data); 171 172 if (debug == true) { 173 System.out.println( 174 "Stack size after childrenAccept: " + stack.size()); 175 } 176 177 if (pfmc.getNumberOfParameters() == -1) { 178 pfmc.setCurNumberOfParameters(node.jjtGetNumChildren()); 181 } 182 183 185 pfmc.run(stack); 186 187 if (debug == true) { 188 System.out.println("Stack size after run: " + stack.size()); 189 } 190 191 return data; 192 } 193 194 198 public Object visit(ASTVarNode node, Object data) throws ParseException { 199 200 204 207 209 Variable var = node.getVar(); 210 if (var == null) { 211 String message = "Could not evaluate " + node.getName() + ": "; 212 throw new ParseException(message + " variable not set"); 213 } 214 215 Object temp = var.getValue(); 216 217 if (temp == null) { 218 String message = "Could not evaluate " + node.getName() + ": "; 219 throw new ParseException(message + "the variable was not found in the symbol table"); 220 } else { 221 stack.push(temp); 224 } 225 226 return data; 227 } 228 229 233 public Object visit(ASTConstant node, Object data) { 234 stack.push(node.getValue()); 235 return data; 236 } 237 } 238 | Popular Tags |