KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > XOperatorSet


1 /* @author rich
2  * Created on 26-Jul-2003
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;
9 import org.nfunk.jep.*;
10
11 /**
12  * An OperatorSet where the operators have information about their commutativity etc.
13  *
14  * @see XOperator
15  * @author Rich Morris
16  * Created on 26-Jul-2003
17  */

18 public class XOperatorSet extends OperatorSet {
19     
20     private void annotateOperators(OperatorSet o) {
21     OP_GT = new XOperator(o.getGT(),XOperator.BINARY+XOperator.LEFT+XOperator.TRANSITIVE);
22     OP_LT = new XOperator(o.getLT(),XOperator.BINARY+XOperator.LEFT+XOperator.TRANSITIVE);
23     OP_EQ = new XOperator(o.getEQ(),XOperator.BINARY+XOperator.LEFT+XOperator.EQUIVILENCE);
24     OP_LE = new XOperator(o.getLE(),XOperator.BINARY+XOperator.LEFT+XOperator.REFLEXIVE+XOperator.TRANSITIVE);
25     OP_GE = new XOperator(o.getGE(),XOperator.BINARY+XOperator.LEFT+XOperator.REFLEXIVE+XOperator.TRANSITIVE);
26     OP_NE = new XOperator(o.getNE(),XOperator.BINARY+XOperator.LEFT+XOperator.SYMMETRIC);
27
28     OP_AND = new XOperator(o.getAnd(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE+XOperator.USE_BINDING_FOR_PRINT);
29     OP_OR = new XOperator(o.getOr(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE);
30     OP_NOT = new XOperator(o.getNot(),XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE);
31
32     OP_ADD = new XOperator(o.getAdd(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE);
33     OP_SUBTRACT = new XOperator(o.getSubtract(),XOperator.BINARY+XOperator.LEFT+XOperator.COMPOSITE+XOperator.USE_BINDING_FOR_PRINT);
34     OP_UMINUS = new XOperator(o.getUMinus(),XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE);
35
36     OP_MULTIPLY = new XOperator(o.getMultiply(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE);
37     OP_DIVIDE = new XOperator(o.getDivide(),XOperator.BINARY+XOperator.LEFT+XOperator.COMPOSITE);
38     OP_MOD = new XOperator(o.getMod(),XOperator.BINARY+XOperator.LEFT);
39     /** unary division i.e. 1/x or x^(-1) **/
40     OP_UDIVIDE = new XOperator("UDivide","^-1",null,XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE);
41
42     OP_POWER = new XOperator(o.getPower(),XOperator.BINARY+XOperator.LEFT);
43
44     OP_ASSIGN = new XOperator("=",new XAssign(),XOperator.BINARY+XOperator.RIGHT); //
45

46     OP_DOT = new XOperator(o.getDot(),XOperator.BINARY+XOperator.RIGHT); //
47
OP_CROSS = new XOperator(o.getCross(),XOperator.BINARY+XOperator.RIGHT); //
48
OP_LIST = new XOperator(o.getList(),XOperator.NARY+XOperator.RIGHT); //
49

50     setPrecedenceTable(new Operator[][]
51         { {OP_UMINUS},
52             {OP_NOT},
53             {OP_POWER},
54             {OP_MULTIPLY,OP_DIVIDE,OP_MOD,OP_DOT,OP_CROSS},
55             {OP_ADD,OP_SUBTRACT},
56             {OP_LT,OP_LE},
57             {OP_GT,OP_GE},
58             {OP_EQ},
59             {OP_NE},
60             {OP_AND},
61             {OP_OR},
62             {OP_ASSIGN},
63             });
64     //printOperators();
65

66     //
67
((XOperator) OP_ADD).setInverseOp(OP_UMINUS);
68     ((XOperator) OP_ADD).setBinaryInverseOp(OP_SUBTRACT);
69     ((XOperator) OP_SUBTRACT).setRootOp(OP_ADD);
70     ((XOperator) OP_SUBTRACT).setInverseOp(OP_UMINUS);
71     ((XOperator) OP_UMINUS).setRootOp(OP_ADD);
72     ((XOperator) OP_UMINUS).setBinaryInverseOp(OP_SUBTRACT);
73         
74     ((XOperator) OP_MULTIPLY).setInverseOp(OP_UDIVIDE);
75     ((XOperator) OP_MULTIPLY).setBinaryInverseOp(OP_DIVIDE);
76     ((XOperator) OP_DIVIDE).setRootOp(OP_MULTIPLY);
77     ((XOperator) OP_DIVIDE).setInverseOp(OP_UDIVIDE);
78     ((XOperator) OP_UDIVIDE).setRootOp(OP_MULTIPLY);
79     ((XOperator) OP_UDIVIDE).setBinaryInverseOp(OP_DIVIDE);
80         
81     // Set distribuative over
82
((XOperator) OP_UMINUS).setDistributiveOver(OP_ADD); // -(a+b) -> (-a) + (-b)
83
((XOperator) OP_UMINUS).setDistributiveOver(OP_SUBTRACT); // -(a-b) -> (-a) - (-b)
84

85     ((XOperator) OP_MULTIPLY).setDistributiveOver(OP_ADD); // a*(b+c) -> a*b + a*c
86
((XOperator) OP_MULTIPLY).setDistributiveOver(OP_SUBTRACT); // a*(b-c) -> a*b - a*c
87
((XOperator) OP_MULTIPLY).setDistributiveOver(OP_UMINUS); // a*(-b) -> -(a*b)
88
}
89
90     /** Creates the operator set from a given set. Will
91      * use the names and pfmc's but adds info about the operators properties.
92      * Note changes pfmc for = from Assign to XAssign */

93     public XOperatorSet(OperatorSet opSet)
94     {
95         annotateOperators(opSet);
96     }
97
98     /** Create the standard set of operators. */
99     public XOperatorSet()
100     {
101         annotateOperators(this);
102     }
103     
104     /**
105      * Sets the precedences of the operators according to order in the supplied array.
106      * For example
107      * <pre>
108      * setPrecedenceTable(new Operator[][]
109      * { {OP_UMINUS},
110      * {OP_NOT},
111      * {OP_MUL,OP_DIV,OP_MOD},
112      * {OP_PLUS,OP_MINUS},
113      * {OP_LT,OP_LE},
114      * {OP_GT,OP_GE},
115      * {OP_EQ},
116      * {OP_NE},
117      * {OP_AND},
118      * {OP_OR},
119      * });
120      * </pre>
121      */

122
123     public static final void setPrecedenceTable(Operator[][] precArray)
124     {
125         for(int i=0;i<precArray.length;++i)
126             for(int j=0;j<precArray[i].length;++j)
127                 ((XOperator) precArray[i][j]).setPrecedence(i);
128     }
129     
130     /** Prints all the opperators, with verbose representations of each operators properties.
131      *
132      */

133     public void printOperators()
134     {
135         Operator ops[] = getOperators();
136         int maxPrec = -1;
137         for(int i=0;i<ops.length;++i)
138             if(((XOperator) ops[i]).getPrecedence()>maxPrec) maxPrec=((XOperator) ops[i]).getPrecedence();
139         for(int j=-1;j<=maxPrec;++j)
140             for(int i=0;i<ops.length;++i)
141                 if(((XOperator) ops[i]).getPrecedence()==j)
142                     System.out.println(((XOperator) ops[i]).toFullString());
143     }
144
145 }
146
Popular Tags