KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > rewriteRules > ExpandBrackets


1 /* @author rich
2  * Created on 01-Oct-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

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 /**
15  * @author Rich Morris
16  * Created on 01-Oct-2004
17  */

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     /**
26      *
27      */

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     /* (non-Javadoc)
36      * @see org.lsmp.djep.xjep.RewriteRuleI#test(org.nfunk.jep.Node, org.nfunk.jep.Node[])
37      */

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     /* (non-Javadoc)
57      * @see org.lsmp.djep.xjep.RewriteRuleI#apply(org.nfunk.jep.Node, org.nfunk.jep.Node[])
58      */

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         { /* (a+b)*c --> (a*c)+(b*c) */
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         { /* a*(b+c) -> (a*b)+(a*c) */
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