KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > BinaryOperator


1 /* BinaryOperator Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: BinaryOperator.java,v 4.12.2.1 2002/05/28 17:34:05 hoenicke Exp $
18  */

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.decompiler.TabbedPrintWriter;
23
24 public class BinaryOperator extends Operator {
25
26     public BinaryOperator(Type type, int op) {
27         super(type, op);
28     initOperands(2);
29     }
30     
31     public int getPriority() {
32         switch (operatorIndex) {
33         case 1: case 2:
34             return 610;
35         case 3: case 4: case 5:
36             return 650;
37         case 6: case 7: case 8:
38             return 600;
39         case 9:
40             return 450;
41         case 10:
42             return 410;
43         case 11:
44             return 420;
45         case 12: case 13: case 14: case 15: case 16: case 17:
46         case 18: case 19: case 20: case 21: case 22: case 23:
47             return 100;
48         case LOG_OR_OP:
49             return 310;
50         case LOG_AND_OP:
51             return 350;
52         }
53         throw new RuntimeException JavaDoc("Illegal operator");
54     }
55
56     public void updateSubTypes() {
57     subExpressions[0].setType(Type.tSubType(type));
58     subExpressions[1].setType(Type.tSubType(type));
59     }
60
61     public void updateType() {
62     Type leftType = Type.tSuperType(subExpressions[0].getType());
63     Type rightType = Type.tSuperType(subExpressions[1].getType());
64     subExpressions[0].setType(Type.tSubType(rightType));
65     subExpressions[1].setType(Type.tSubType(leftType));
66     updateParentType(leftType.intersection(rightType));
67     }
68
69     public Expression negate() {
70         if (getOperatorIndex() == LOG_AND_OP ||
71         getOperatorIndex() == LOG_OR_OP) {
72             setOperatorIndex(getOperatorIndex() ^ 1);
73             for (int i=0; i< 2; i++) {
74         subExpressions[i] = subExpressions[i].negate();
75                 subExpressions[i].parent = this;
76             }
77             return this;
78         }
79     return super.negate();
80     }
81
82     public boolean opEquals(Operator o) {
83     return (o instanceof BinaryOperator) &&
84         o.operatorIndex == operatorIndex;
85     }
86
87     public void dumpExpression(TabbedPrintWriter writer)
88     throws java.io.IOException JavaDoc {
89     subExpressions[0].dumpExpression(writer, getPriority());
90     writer.breakOp();
91     writer.print(getOperatorString());
92     subExpressions[1].dumpExpression(writer, getPriority()+1);
93     }
94 }
95
Popular Tags