1 8 package org.lsmp.djep.matrixJep; 9 import org.nfunk.jep.*; 10 import org.nfunk.jep.function.*; 11 import org.lsmp.djep.matrixJep.nodeTypes.*; 12 import org.lsmp.djep.vectorJep.*; 13 import org.lsmp.djep.vectorJep.function.*; 14 import org.lsmp.djep.xjep.*; 15 16 26 public class MatrixNodeFactory extends NodeFactory { 27 28 public MatrixNodeFactory() 29 { 30 } 31 32 33 public ASTConstant buildConstantNode(Object value) throws ParseException 34 { 35 ASTMConstant node = new ASTMConstant(ParserTreeConstants.JJTCONSTANT); 36 node.setValue(value); 37 return node; 38 } 39 40 41 public ASTVarNode buildVariableNode(Variable var) throws ParseException 42 { 43 ASTMVarNode node = new ASTMVarNode(ParserTreeConstants.JJTVARNODE); 44 node.setVar(var); 45 return node; 46 } 47 48 49 56 57 public ASTFunNode buildFunctionNode(String name,PostfixMathCommandI pfmc,Node[] arguments) throws ParseException 58 { 59 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 60 res.setFunction(name,pfmc); 61 copyChildren(res,arguments); 62 res.setDim(calcDim(name,pfmc,arguments)); 63 return res; 64 } 65 66 69 public Dimensions calcDim(String name,PostfixMathCommandI pfmc,Node arguments[]) 70 throws ParseException 71 { 72 MatrixNodeI children[] = new MatrixNodeI[arguments.length]; 73 for(int i=0;i<arguments.length;++i) 74 children[i] = (MatrixNodeI) arguments[i]; 75 76 if(pfmc instanceof BinaryOperatorI) 77 { 78 if(children.length!=2) throw new ParseException("Operator "+name+" must have two elements, it has "+children.length); 79 BinaryOperatorI bin = (BinaryOperatorI) pfmc; 80 Dimensions dim = bin.calcDim(children[0].getDim(),children[1].getDim()); 81 return dim; 82 } 83 else if(pfmc instanceof UnaryOperatorI) 84 { 85 if(children.length!=1) throw new ParseException("Operator "+name+" must have one elements, it has "+children.length); 86 UnaryOperatorI uni = (UnaryOperatorI) pfmc; 87 Dimensions dim = uni.calcDim(children[0].getDim()); 88 return dim; 89 } 90 else if(pfmc instanceof NaryOperatorI) 91 { 92 Dimensions dims[] = new Dimensions[children.length]; 93 for(int i=0;i<children.length;++i) 94 dims[i]=children[i].getDim(); 95 NaryOperatorI uni = (NaryOperatorI) pfmc; 97 Dimensions dim = uni.calcDim(dims); 98 return dim; 99 } 100 else 101 { 102 return Dimensions.ONE; 103 } 105 } 106 107 110 public Dimensions calcDim(Operator op,Node arguments[]) throws ParseException 111 { 112 return calcDim(op.getName(),op.getPFMC(),arguments); 113 } 114 115 123 public ASTFunNode buildFunctionNode(ASTFunNode node,Node[] children) throws ParseException 124 { 125 if(node instanceof ASTMFunNode) 126 { 127 if(node.isOperator()) 128 return buildOperatorNode(node.getOperator(),children, 129 ((ASTMFunNode) node).getDim()); 130 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 131 res.setFunction(node.getName(),node.getPFMC()); 132 copyChildren(res,children); 133 res.setDim(((ASTMFunNode) node).getDim()); 134 return res; 135 } 136 if(node.isOperator()) 138 return buildOperatorNode(node.getOperator(),children); 139 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 140 res.setFunction(node.getName(),node.getPFMC()); 141 copyChildren(res,children); 142 res.setDim(calcDim(node.getName(),node.getPFMC(),children)); 143 return res; 144 } 145 146 147 154 155 public ASTFunNode buildOperatorNode(Operator op,Node[] arguments) throws ParseException 156 { 157 158 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 159 res.setOperator(op); 160 copyChildren(res,arguments); 161 res.setDim(calcDim(op,arguments)); 162 return res; 163 } 164 165 166 public ASTFunNode buildOperatorNode(Operator op,Node[] arguments,Dimensions dim) 167 { 168 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 169 res.setOperator(op); 170 res.setDim(dim); 171 copyChildren(res,arguments); 172 return res; 173 } 174 175 179 public ASTFunNode buildUnfinishedOperatorNode(Operator op) 180 { 181 ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE); 182 res.setOperator(op); 183 return res; 184 } 185 } 186 | Popular Tags |