1 9 10 package org.nfunk.jep.evaluation; 11 12 import java.util.*; 13 import org.nfunk.jep.*; 14 import org.nfunk.jep.function.*; 15 16 20 public class CommandEvaluator { 21 private int nCommands, nParam; 22 private CommandElement command; 23 private Stack stack; 24 private PostfixMathCommandI pfmc; 25 private int i; 26 27 public CommandEvaluator() { 28 stack = new Stack(); 29 } 30 31 public Object evaluate(CommandElement[] commands, SymbolTable symTab) throws Exception { 32 33 int nCommands = commands.length; 34 35 stack.removeAllElements(); 36 37 i = 0; 39 while (i<nCommands) { 40 command = commands[i]; 41 42 switch (command.getType()) { 43 case CommandElement.FUNC: { 44 pfmc = command.getPFMC(); 46 47 pfmc.setCurNumberOfParameters(command.getNumParam()); 50 51 pfmc.run(stack); 52 break; 53 } 54 case CommandElement.VAR: { 55 stack.push(symTab.getValue(command.getVarName())); 57 break; 58 } 59 default: { 60 stack.push(command.getValue()); 62 } 63 } 64 80 81 i++; 82 } 83 if (stack.size() != 1) { 84 throw new Exception ("CommandEvaluator.evaluate(): Stack size is not 1"); 85 } 86 return stack.pop(); 87 } 88 } 89 | Popular Tags |