1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
10 abstract class JAssignment extends JBinaryExpression {
11
19 public JAssignment(int line, String operator, JExpression lhs, JExpression rhs) {
20 super(line, operator, lhs, rhs);
21 }
22 }
23
24
27 class JAssignOp extends JAssignment {
28
35 public JAssignOp(int line, JExpression lhs, JExpression rhs) {
36 super(line, "=", lhs, rhs);
37 }
38
39
42 public JExpression analyze(Context context) {
43 if (!(lhs instanceof JLhs)) {
44 JAST.compilationUnit.reportSemanticError(line(), "Illegal lhs for assignment");
45 } else {
46 lhs = (JExpression) ((JLhs) lhs).analyzeLhs(context);
47 }
48 rhs = (JExpression) rhs.analyze(context);
49 rhs.type().mustMatchExpected(line(), lhs.type());
50 type = rhs.type();
51 if (lhs instanceof JVariable) {
52 IDefn defn = ((JVariable) lhs).iDefn();
53 if (defn != null) {
54 ((LocalVariableDefn) defn).initialize();
56 }
57 }
58 return this;
59 }
60
61
64 public void codegen(CLEmitter output) {
65 ((JLhs) lhs).codegenLoadLhsLvalue(output);
66 rhs.codegen(output);
67 if (!isStatementExpression) {
68 ((JLhs) lhs).codegenDuplicateRvalue(output);
69 }
70 ((JLhs) lhs).codegenStore(output);
71 }
72 }
73
74
77 class JPlusAssignOp extends JAssignment {
78
85 public JPlusAssignOp(int line, JExpression lhs, JExpression rhs) {
86 super(line, "+=", lhs, rhs);
87 }
88
89
92 public JExpression analyze(Context context) {
93 if (!(lhs instanceof JLhs)) {
94 JAST.compilationUnit.reportSemanticError(line(), "Illegal lhs for assignment");
95 return this;
96 } else {
97 lhs = (JExpression) ((JLhs) lhs).analyzeLhs(context);
98 }
99 rhs = (JExpression) rhs.analyze(context);
100 if (lhs.type().equals(Type.INT)) {
101 rhs.type().mustMatchExpected(line(), Type.INT);
102 type = Type.INT;
103 } else if (lhs.type().equals(Type.STRING)) {
104 rhs = (new JStringConcatenationOp(line, lhs, rhs)).analyze(context);
105 type = Type.STRING;
106 } else {
107 JAST.compilationUnit.reportSemanticError(line(),
108 "Invalid lhs type for +=: " + lhs.type());
109 }
110 return this;
111 }
112
113
116 public void codegen(CLEmitter output) {
117 ((JLhs) lhs).codegenLoadLhsLvalue(output);
118 if (lhs.type().equals(Type.STRING)) {
119 rhs.codegen(output);
120 } else {
121 ((JLhs) lhs).codegenLoadLhsRvalue(output);
122 rhs.codegen(output);
123 output.addNoArgInstruction(IADD);
124 }
125 if (!isStatementExpression) {
126 ((JLhs) lhs).codegenDuplicateRvalue(output);
127 }
128 ((JLhs) lhs).codegenStore(output);
129 }
130 }
131
132
135 class JMinusAssignOp extends JAssignment {
136
143 public JMinusAssignOp(int line, JExpression lhs, JExpression rhs) {
144 super(line, "-=", lhs, rhs);
145 }
146
147
150 public JExpression analyze(Context context) {
151 return this;
153 }
154
155
158 public void codegen(CLEmitter output) {
159 }
161 }
162
163
166 class JStarAssignOp extends JAssignment {
167
174 public JStarAssignOp(int line, JExpression lhs, JExpression rhs) {
175 super(line, "*=", lhs, rhs);
176 }
177
178
181 public JExpression analyze(Context context) {
182 return this;
184 }
185
186
189 public void codegen(CLEmitter output) {
190 }
192 }
193
194
197 class JDivAssignOp extends JAssignment {
198
205 public JDivAssignOp(int line, JExpression lhs, JExpression rhs) {
206 super(line, "/=", lhs, rhs);
207 }
208
209
212 public JExpression analyze(Context context) {
213 return this;
215 }
216
217
220 public void codegen(CLEmitter output) {
221 }
223 }
224
225
228 class JRemAssignOp extends JAssignment {
229
236 public JRemAssignOp(int line, JExpression lhs, JExpression rhs) {
237 super(line, "%=", lhs, rhs);
238 }
239
240
243 public JExpression analyze(Context context) {
244 return this;
246 }
247
248
251 public void codegen(CLEmitter output) {
252 }
254 }
255
256
259 class JOrAssignOp extends JAssignment {
260
267 public JOrAssignOp(int line, JExpression lhs, JExpression rhs) {
268 super(line, "|=", lhs, rhs);
269 }
270
271
274 public JExpression analyze(Context context) {
275 return this;
277 }
278
279
282 public void codegen(CLEmitter output) {
283 }
285 }
286
287
290 class JAndAssignOp extends JAssignment {
291
298 public JAndAssignOp(int line, JExpression lhs, JExpression rhs) {
299 super(line, "&=", lhs, rhs);
300 }
301
302
305 public JExpression analyze(Context context) {
306 return this;
308 }
309
310
313 public void codegen(CLEmitter output) {
314 }
316 }
317
318
321 class JXorAssignOp extends JAssignment {
322
329 public JXorAssignOp(int line, JExpression lhs, JExpression rhs) {
330 super(line, "^=", lhs, rhs);
331 }
332
333
336 public JExpression analyze(Context context) {
337 return this;
339 }
340
341
344 public void codegen(CLEmitter output) {
345 }
347 }
348
349
352 class JALeftShiftAssignOp extends JAssignment {
353
360 public JALeftShiftAssignOp(int line, JExpression lhs, JExpression rhs) {
361 super(line, "<<=", lhs, rhs);
362 }
363
364
367 public JExpression analyze(Context context) {
368 return this;
370 }
371
372
375 public void codegen(CLEmitter output) {
376 }
378 }
379
380
383 class JARightShiftAssignOp extends JAssignment {
384
391 public JARightShiftAssignOp(int line, JExpression lhs, JExpression rhs) {
392 super(line, ">>=", lhs, rhs);
393 }
394
395
398 public JExpression analyze(Context context) {
399 return this;
401 }
402
403
406 public void codegen(CLEmitter output) {
407 }
409 }
410
411
414 class JLRightShiftAssignOp extends JAssignment {
415
422 public JLRightShiftAssignOp(int line, JExpression lhs, JExpression rhs) {
423 super(line, ">>>=", lhs, rhs);
424 }
425
426
429 public JExpression analyze(Context context) {
430 return this;
432 }
433
434
437 public void codegen(CLEmitter output) {
438 }
440 }
441