1 8 package org.lsmp.djep.xjep; 9 10 import org.nfunk.jep.*; 11 import org.lsmp.djep.xjep.function.*; 12 import java.util.*; 13 import java.io.PrintStream; 14 import java.io.Reader; 15 22 public class XJep extends JEP { 23 24 protected NodeFactory nf = null; 25 26 protected TreeUtils tu = null; 27 protected DeepCopyVisitor copier = null; 28 protected SubstitutionVisitor subv = null; 29 protected SimplificationVisitor simpv = null; 30 protected CommandVisitor commandv = null; 31 protected PrintVisitor pv = null; 32 private VariableFactory vf = new XVariableFactory(); 33 34 37 public XJep() 38 { 39 this.symTab = new XSymbolTable(vf); 40 41 42 nf = new NodeFactory(); 43 44 opSet = new XOperatorSet(); 45 46 tu = new TreeUtils(); 47 48 copier = new DeepCopyVisitor(); 49 subv = new SubstitutionVisitor(); 50 ev = new XEvaluatorVisitor(); 51 simpv = new SimplificationVisitor(); 52 commandv = new CommandVisitor(); 53 pv = new PrintVisitor(); 54 } 55 56 57 protected XJep(XJep j) 58 { 59 super((JEP) j); 60 this.commandv=j.commandv; 61 this.copier=j.copier; 62 this.ev=j.ev; 63 this.nf=j.nf; 64 this.opSet=j.opSet; 65 this.pv=j.pv; 66 this.simpv=j.simpv; 67 this.subv=j.subv; 68 this.tu=j.tu; 69 } 70 71 private JEP ingrediant = null; 72 76 public XJep(JEP j) 77 { 78 ingrediant=j; 79 80 nf = new NodeFactory(); 81 this.symTab = new XSymbolTable(vf); 82 this.funTab = j.getFunctionTable(); 83 84 opSet = new XOperatorSet(j.getOperatorSet()); 85 86 tu = new TreeUtils(); 87 copier = new DeepCopyVisitor(); 88 subv = new SubstitutionVisitor(); 89 ev = new XEvaluatorVisitor(); 90 simpv = new SimplificationVisitor(); 91 commandv = new CommandVisitor(); 92 pv = new PrintVisitor(); 93 } 94 98 public XJep newInstance() 99 { 100 XJep newJep = new XJep(this); 101 return newJep; 102 } 103 107 public XJep newInstance(SymbolTable st) 108 { 109 XJep newJep = new XJep(this); 110 newJep.symTab = st; 111 return newJep; 112 } 113 114 public void addStandardFunctions() 115 { 116 if(ingrediant!=null) 117 { 118 ingrediant.addStandardFunctions(); 119 } 120 else super.addStandardFunctions(); 121 addFunction("eval",new Eval()); 122 addFunction("Sum",new Sum()); 123 addFunction("Product",new Product()); 124 addFunction("Min",new Min()); 125 addFunction("Max",new Max()); 126 addFunction("MinArg",new MinArg()); 127 addFunction("MaxArg",new MaxArg()); 128 } 129 130 public void addStandardConstants() 131 { 132 if(ingrediant!=null) 133 { 134 ingrediant.addStandardConstants(); 135 for(Enumeration enum=ingrediant.getSymbolTable().elements();enum.hasMoreElements();) 136 { 137 Variable var = (Variable) enum.nextElement(); 138 if(var.isConstant()) 139 this.symTab.addConstant(var.getName(),var.getValue()); 140 } 143 } 144 else super.addStandardConstants(); 145 } 146 147 148 public Node deepCopy(Node node) throws ParseException 149 { 150 return copier.deepCopy(node,this); 151 } 152 153 public Object evaluate(Node node) throws Exception 154 { 155 return ev.getValue(node,new Vector(),this.symTab); 156 } 157 158 public Node simplify(Node node) throws ParseException 159 { 160 return simpv.simplify(node,this); 161 } 162 163 public Node preprocess(Node node) throws ParseException 164 { 165 return commandv.process(node,this); 166 } 167 168 public Node substitute(Node orig,String name,Node replacement) throws ParseException 169 { 170 return subv.substitute(orig,name,replacement,this); 171 } 172 173 public Node substitute(Node orig,String names[],Node replacements[]) throws ParseException 174 { 175 return subv.substitute(orig,names,replacements,this); 176 } 177 178 public void print(Node node) { pv.print(node); } 179 180 public void print(Node node,PrintStream out) { pv.print(node,out); } 181 182 public void println(Node node) { pv.println(node); } 183 184 public void println(Node node,PrintStream out) { pv.println(node,out); } 185 186 public String toString(Node node) { return pv.toString(node); } 187 188 public NodeFactory getNodeFactory() {return nf;} 189 190 public TreeUtils getTreeUtils() { return tu; } 191 193 public PrintVisitor getPrintVisitor() { return pv; } 194 195 199 public Object calcVarValue(String name) throws Exception 200 { 201 XVariable xvar = (XVariable) getVar(name); 202 return xvar.calcValue(this); 203 } 204 205 225 public Node continueParsing() throws ParseException { 226 return parser.continueParse(); 227 } 228 229 234 public void restartParser(String str) { 235 parser.restart(new java.io.StringReader(str), this); 236 } 237 238 243 public void restartParser(Reader reader) { 244 parser.restart(reader, this); 245 } 246 247 } 248 | Popular Tags |