1 8 package org.lsmp.djep.xjep; 9 import org.nfunk.jep.*; 10 import org.nfunk.jep.function.*; 11 import java.util.Stack ; 12 22 public class NodeFactory { 23 24 public NodeFactory() {} 25 26 31 public void copyChildren(Node node,Node children[]) 32 { 33 int nchild = children.length; 34 node.jjtOpen(); 35 for(int i=0;i<nchild;++i) 36 { 37 children[i].jjtSetParent(node); 38 node.jjtAddChild(children[i],i); 39 } 40 node.jjtClose(); 41 } 42 43 46 public ASTConstant buildConstantNode(Object value) throws ParseException 47 { 48 ASTConstant node = new ASTConstant(ParserTreeConstants.JJTCONSTANT); 49 node.setValue(value); 50 return node; 51 } 52 53 54 public ASTConstant buildConstantNode(ASTConstant node) throws ParseException 55 { 56 return buildConstantNode(node.getValue()); 57 } 58 59 60 61 public ASTConstant buildConstantNode(PostfixMathCommandI pfmc,Node children[]) throws IllegalArgumentException ,ParseException 62 { 63 Stack stack = new Stack (); 64 for(int i=0;i<children.length;++i) 65 { 66 if(!(children[i] instanceof ASTConstant)) 67 throw new ParseException("buildConstantNode: arguments must all be constant nodes"); 68 stack.push(((ASTConstant) children[i]).getValue()); 69 } 70 pfmc.setCurNumberOfParameters(children.length); 71 pfmc.run(stack); 72 return buildConstantNode(stack.pop()); 73 } 74 75 76 public ASTConstant buildConstantNode(Operator op,Node children[]) throws IllegalArgumentException ,ParseException 77 { 78 return buildConstantNode(op.getPFMC(),children); 79 } 80 81 82 public ASTConstant buildConstantNode(Operator op,Node child1,Node child2) throws IllegalArgumentException ,ParseException 83 { 84 return buildConstantNode(op.getPFMC(),new Node[]{child1,child2}); 85 } 86 87 88 public ASTConstant buildConstantNode(Operator op,Node child1) throws IllegalArgumentException ,ParseException 89 { 90 return buildConstantNode(op.getPFMC(),new Node[]{child1}); 91 } 92 93 94 public ASTVarNode buildVariableNode(ASTVarNode node) throws ParseException 95 { 96 return buildVariableNode(node.getVar()); 97 } 98 99 102 public ASTVarNode buildVariableNode(Variable var) throws ParseException 103 { 104 ASTVarNode node = new ASTVarNode(ParserTreeConstants.JJTVARNODE); 105 node.setVar(var); 106 return node; 107 } 108 109 117 118 public ASTFunNode buildOperatorNode(Operator op,Node[] arguments) throws ParseException 119 { 120 ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE); 121 res.setOperator(op); 122 copyChildren(res,arguments); 123 return res; 124 } 125 126 134 135 public ASTFunNode buildFunctionNode(String name,PostfixMathCommandI pfmc,Node[] arguments) throws ParseException 136 { 137 ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE); 138 res.setFunction(name,pfmc); 139 copyChildren(res,arguments); 140 return res; 141 } 142 143 144 146 public ASTFunNode buildUnfinishedOperatorNode(Operator op) throws ParseException 147 { 148 ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE); 149 res.setOperator(op); 150 return res; 151 } 152 153 154 155 public ASTFunNode buildOperatorNode(Operator op,Node child) throws ParseException 156 { 157 return buildOperatorNode(op,new Node[]{child}); 158 } 159 160 161 162 public ASTFunNode buildOperatorNode(Operator op,Node lhs,Node rhs) throws ParseException 163 { 164 return buildOperatorNode(op,new Node[]{lhs,rhs}); 165 } 166 167 173 public ASTFunNode buildFunctionNode(ASTFunNode node,Node[] arguments) throws ParseException 174 { 175 if(node.getOperator()!=null) 176 return buildOperatorNode(node.getOperator(),arguments); 177 return buildFunctionNode(node.getName(),node.getPFMC(),arguments); 178 } 179 180 } 181 | Popular Tags |