1 8 package org.lsmp.djep.xjep; 9 import org.nfunk.jep.*; 10 26 public class SubstitutionVisitor extends DoNothingVisitor { 27 28 private String names[]; 29 private Node replacements[]; 30 private XJep xjep; 31 public SubstitutionVisitor() {} 32 33 42 public Node substitute(Node orig,String name,Node replacement,XJep xjep) throws ParseException 43 { 44 this.names = new String []{name}; 45 this.replacements = new Node[]{replacement}; 46 this.xjep=xjep; 47 Node res = (Node) orig.jjtAccept(this,null); 48 return res; 49 } 50 51 60 public Node substitute(Node orig,String names[],Node replacements[],XJep xjep) throws ParseException 61 { 62 this.names = names; 63 this.replacements = replacements; 64 this.xjep=xjep; 65 Node res = (Node) orig.jjtAccept(this,null); 66 return res; 67 } 68 69 public Object visit(ASTVarNode node, Object data) throws ParseException 70 { 71 for(int i=0;i<names.length;++i) 72 { 73 if(names[i].equals(node.getName())) 74 return xjep.deepCopy(replacements[i]); 75 } 76 if(node.getVar().isConstant()) 77 return xjep.getNodeFactory().buildVariableNode(xjep.getSymbolTable().getVar(node.getName())); 78 79 throw new ParseException("No substitution specified for variable "+node.getName()); 80 } 81 } 82 | Popular Tags |