1 8 package org.lsmp.djep.xjep.rewriteRules; 9 10 import org.lsmp.djep.xjep.RewriteRuleI; 11 import org.nfunk.jep.*; 12 import org.lsmp.djep.xjep.*; 13 14 18 public class ExpandBrackets implements RewriteRuleI { 19 20 private NodeFactory nf; 21 private OperatorSet opSet; 22 private TreeUtils tu; 23 private XJep xj; 24 25 28 public ExpandBrackets(XJep xj) { 29 opSet = xj.getOperatorSet(); 30 tu = xj.getTreeUtils(); 31 nf = xj.getNodeFactory(); 32 this.xj = xj; 33 } 34 private ExpandBrackets() {} 35 38 public boolean test(ASTFunNode node, Node[] children) { 39 if(!node.isOperator()) return false; 40 XOperator op= (XOperator) node.getOperator(); 41 42 if(opSet.getMultiply() == op) 43 { 44 if(tu.getOperator(children[0]) == opSet.getAdd()) 45 return true; 46 if(tu.getOperator(children[0]) == opSet.getSubtract()) 47 return true; 48 if(tu.getOperator(children[1]) == opSet.getAdd()) 49 return true; 50 if(tu.getOperator(children[1]) == opSet.getSubtract()) 51 return true; 52 } 53 return false; 54 } 55 56 59 public Node apply(ASTFunNode node, Node[] children) throws ParseException { 60 OperatorSet opSet = xj.getOperatorSet(); 61 TreeUtils tu = xj.getTreeUtils(); 62 63 Operator lhsOp = tu.getOperator(children[0]); 64 Operator rhsOp = tu.getOperator(children[1]); 65 if(lhsOp == opSet.getAdd() || lhsOp == opSet.getSubtract()) 66 { 67 return nf.buildOperatorNode( 68 lhsOp, 69 nf.buildOperatorNode( 70 opSet.getMultiply(), 71 children[0].jjtGetChild(0), 72 xj.deepCopy(children[1])), 73 nf.buildOperatorNode( 74 opSet.getMultiply(), 75 children[0].jjtGetChild(1), 76 xj.deepCopy(children[1])) 77 ); 78 79 } 80 if(rhsOp == opSet.getAdd() || rhsOp == opSet.getSubtract()) 81 { 82 return nf.buildOperatorNode( 83 rhsOp, 84 nf.buildOperatorNode( 85 opSet.getMultiply(), 86 xj.deepCopy(children[0]), 87 children[1].jjtGetChild(0)), 88 nf.buildOperatorNode( 89 opSet.getMultiply(), 90 xj.deepCopy(children[0]), 91 children[1].jjtGetChild(1)) 92 ); 93 } 94 throw new ParseException("ExpandBrackets at least one child must be + or -"); 95 } 96 97 } 98 | Popular Tags |