1 8 package org.lsmp.djep.xjep; 9 10 import org.nfunk.jep.function.PostfixMathCommand; 11 import org.nfunk.jep.*; 12 import java.util.*; 13 31 public class MacroFunction extends PostfixMathCommand 32 { 33 private String name; 34 private Node topNode; 35 private EvaluatorVisitor ev = new EvaluatorVisitor(); 36 private XSymbolTable mySymTab; 38 private Variable vars[]; 39 40 public String getName() { return name; } 41 public Node getTopNode() { return topNode; } 42 43 52 public MacroFunction(String inName,int nargs,String expression,XJep jep) throws IllegalArgumentException ,ParseException 53 { 54 super(); 55 name = inName; 56 57 XSymbolTable jepSymTab = (XSymbolTable) jep.getSymbolTable(); 58 mySymTab = (XSymbolTable) jepSymTab.newInstance(); 59 mySymTab.copyConstants(jepSymTab); 60 XJep localJep = jep.newInstance(mySymTab); 61 numberOfParameters = nargs; 62 63 if(numberOfParameters == 0) {} 64 else if(numberOfParameters == 1) 65 vars = new Variable[]{mySymTab.addVariable("x",null)}; 66 else if(numberOfParameters == 2) 67 { 68 vars = new Variable[]{ 69 mySymTab.addVariable("x",null), 70 mySymTab.addVariable("y",null)}; 71 } 72 else 73 { 74 vars = new Variable[numberOfParameters]; 75 for(int i=numberOfParameters-1;i>0;) 76 vars[i] = mySymTab.addVariable("x"+String.valueOf(i),null); 77 } 78 79 topNode = localJep.parse(expression); 80 } 81 82 86 public void run(Stack stack) throws ParseException 87 { 88 89 if(numberOfParameters == 0) {} 90 else if(numberOfParameters == 1) 91 vars[0].setValue(stack.pop()); 92 else if(numberOfParameters == 2) 93 { 94 vars[1].setValue(stack.pop()); 95 vars[0].setValue(stack.pop()); 96 } 97 else 98 { 99 for(int i=numberOfParameters-1;i>0;) 100 vars[i].setValue(stack.pop()); 101 } 102 try 103 { 104 Object res = ev.getValue(topNode,null,mySymTab); 105 stack.push(res); 106 } 107 catch(Exception e1) { throw new ParseException("MacroFunction eval: "+e1.getMessage()); } 108 } 109 } 110 | Popular Tags |