00001 package edu.ksu.cis.bandera.jjjc.optimizer;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.io.*;
00036 import java.util.*;
00037
00038 import edu.ksu.cis.bandera.jjjc.node.*;
00039 import edu.ksu.cis.bandera.jjjc.lexer.*;
00040 import edu.ksu.cis.bandera.jjjc.parser.*;
00041 import edu.ksu.cis.bandera.jjjc.analysis.*;
00042 import edu.ksu.cis.bandera.jjjc.unicodepreprocessor.*;
00043
00044 import ca.mcgill.sable.laleh.java.astfix.*;
00045
00046 public final class PushComplement extends DepthFirstAdapter {
00047 private boolean foundComplement;
00048 private static PushComplement p = new PushComplement();
00049
00050
00051
00052 private PushComplement() {
00053 }
00054
00055
00056
00057
00058 public void caseAArrayAccessExp(AArrayAccessExp node) {
00059 if (foundComplement) {
00060 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00061 }
00062 }
00063
00064
00065
00066
00067 public void caseAAssignmentExp(AAssignmentExp node) {
00068
00069
00070
00071
00072 node.getExp().apply(this);
00073
00074
00075
00076
00077
00078
00079 }
00080
00081
00082
00083
00084 public void caseABinaryExp(ABinaryExp node) {
00085 PBinaryOperator op = node.getBinaryOperator();
00086 boolean prevFoundComplement = foundComplement;
00087 if (foundComplement) {
00088 if (op instanceof AOrBinaryOperator) {
00089 node.setBinaryOperator(new AAndBinaryOperator(new TAnd()));
00090 } else if (op instanceof AAndBinaryOperator) {
00091 node.setBinaryOperator(new AOrBinaryOperator(new TOr()));
00092 } else if (op instanceof AGtBinaryOperator) {
00093 node.setBinaryOperator(new ALteqBinaryOperator(new TLteq()));
00094 foundComplement = false;
00095 } else if (op instanceof ALtBinaryOperator) {
00096 node.setBinaryOperator(new AGteqBinaryOperator(new TGteq()));
00097 foundComplement = false;
00098 } else if (op instanceof ALteqBinaryOperator) {
00099 node.setBinaryOperator(new AGtBinaryOperator(new TGt()));
00100 foundComplement = false;
00101 } else if (op instanceof AGteqBinaryOperator) {
00102 node.setBinaryOperator(new ALtBinaryOperator(new TLt()));
00103 foundComplement = false;
00104 } else if (op instanceof AEqBinaryOperator) {
00105 node.setBinaryOperator(new ANeqBinaryOperator(new TNeq()));
00106 foundComplement = false;
00107 } else if (op instanceof ANeqBinaryOperator) {
00108 node.setBinaryOperator(new AEqBinaryOperator(new TEq()));
00109 foundComplement = false;
00110 }
00111 }
00112 node.getFirst().apply(this);
00113 node.getSecond().apply(this);
00114 foundComplement = prevFoundComplement;
00115 }
00116
00117
00118
00119
00120 public void caseAFalseBooleanLiteral(AFalseBooleanLiteral node) {
00121 if (foundComplement)
00122 node.replaceBy(new ATrueBooleanLiteral(new TTrue()));
00123 }
00124
00125
00126
00127
00128 public void caseAFieldAccessExp(AFieldAccessExp node) {
00129 if (foundComplement) {
00130 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00131 }
00132 }
00133
00134
00135
00136
00137 public void caseAInstanceofExp(AInstanceofExp node) {
00138 if (foundComplement) {
00139 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00140 }
00141 }
00142
00143
00144
00145
00146 public void caseANameExp(ANameExp node) {
00147 if (foundComplement) {
00148 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00149 }
00150 }
00151
00152
00153
00154
00155 public void caseANameMethodInvocationExp(ANameMethodInvocationExp node) {
00156 boolean prevFoundComplement = foundComplement;
00157 foundComplement = false;
00158
00159 {
00160 Object temp[] = node.getArgumentList().toArray();
00161 for (int i = 0; i < temp.length; i++) {
00162 ((PExp) temp[i]).apply(this);
00163 }
00164 }
00165
00166 foundComplement = prevFoundComplement;
00167
00168 if (foundComplement) {
00169 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00170 }
00171 }
00172
00173
00174
00175
00176 public void caseAPrimaryMethodInvocationExp(APrimaryMethodInvocationExp node) {
00177 boolean prevFoundComplement = foundComplement;
00178 foundComplement = false;
00179
00180 {
00181 Object temp[] = node.getArgumentList().toArray();
00182 for (int i = 0; i < temp.length; i++) {
00183 ((PExp) temp[i]).apply(this);
00184 }
00185 }
00186
00187 foundComplement = prevFoundComplement;
00188
00189 if (foundComplement) {
00190 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00191 }
00192 }
00193
00194
00195
00196
00197 public void caseAQuestionExp(AQuestionExp node) {
00198 boolean prevFoundComplement = foundComplement;
00199
00200 foundComplement = false;
00201 node.getSecond().apply(this);
00202
00203 foundComplement = false;
00204 node.getThird().apply(this);
00205
00206 foundComplement = prevFoundComplement;
00207 node.getFirst().apply(this);
00208 }
00209
00210
00211
00212
00213 public void caseASuperMethodInvocationExp(ASuperMethodInvocationExp node) {
00214 boolean prevFoundComplement = foundComplement;
00215 foundComplement = false;
00216
00217 {
00218 Object temp[] = node.getArgumentList().toArray();
00219 for (int i = 0; i < temp.length; i++) {
00220 ((PExp) temp[i]).apply(this);
00221 }
00222 }
00223
00224 foundComplement = prevFoundComplement;
00225
00226 if (foundComplement) {
00227 node.replaceBy(new AUnaryExp(new AComplementUnaryOperator(new TComplement()), (PExp) node.clone()));
00228 }
00229 }
00230
00231
00232
00233
00234 public void caseATrueBooleanLiteral(ATrueBooleanLiteral node) {
00235 if (foundComplement)
00236 node.replaceBy(new AFalseBooleanLiteral(new TFalse()));
00237 }
00238
00239
00240
00241
00242 public void caseAUnaryExp(AUnaryExp node) {
00243 PExp exp = node.getExp();
00244 if ("!".equals(node.getUnaryOperator().toString().trim())) {
00245 foundComplement = !foundComplement;
00246 node.replaceBy(exp);
00247 exp.apply(this);
00248 foundComplement = !foundComplement;
00249 } else {
00250 exp.apply(this);
00251 }
00252 }
00253
00254
00255
00256 private void init() {
00257 foundComplement = false;
00258 }
00259
00260
00261
00262
00263 public static void push(Node exp) {
00264 p.init();
00265 exp.apply(p);
00266 }
00267 }