1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
11 abstract class JBinaryExpression extends JExpression {
12
15 protected String operator;
16
17
20 protected JExpression lhs;
21
22
25 protected JExpression rhs;
26
27
35 protected JBinaryExpression(int line, String operator, JExpression lhs, JExpression rhs) {
36 super(line);
37 this.operator = operator;
38 this.lhs = lhs;
39 this.rhs = rhs;
40 }
41
42
45 public void toJSON(JSONElement json) {
46 JSONElement e = new JSONElement();
47 json.addChild("JBinaryExpression:" + line, e);
48 e.addAttribute("operator", operator);
49 e.addAttribute("type", type == null ? "" : type.toString());
50 JSONElement e1 = new JSONElement();
51 e.addChild("Operand1", e1);
52 lhs.toJSON(e1);
53 JSONElement e2 = new JSONElement();
54 e.addChild("Operand2", e2);
55 rhs.toJSON(e2);
56 }
57 }
58
59
62 class JMultiplyOp extends JBinaryExpression {
63
70 public JMultiplyOp(int line, JExpression lhs, JExpression rhs) {
71 super(line, "*", lhs, rhs);
72 }
73
74
77 public JExpression analyze(Context context) {
78 lhs = (JExpression) lhs.analyze(context);
79 rhs = (JExpression) rhs.analyze(context);
80 lhs.type().mustMatchExpected(line(), Type.INT);
81 rhs.type().mustMatchExpected(line(), Type.INT);
82 type = Type.INT;
83 return this;
84 }
85
86
89 public void codegen(CLEmitter output) {
90 lhs.codegen(output);
91 rhs.codegen(output);
92 output.addNoArgInstruction(IMUL);
93 }
94 }
95
96
100 class JPlusOp extends JBinaryExpression {
101
108 public JPlusOp(int line, JExpression lhs, JExpression rhs) {
109 super(line, "+", lhs, rhs);
110 }
111
112
115 public JExpression analyze(Context context) {
116 lhs = (JExpression) lhs.analyze(context);
117 rhs = (JExpression) rhs.analyze(context);
118 if (lhs.type() == Type.STRING || rhs.type() == Type.STRING) {
119 return (new JStringConcatenationOp(line, lhs, rhs)).analyze(context);
120 } else if (lhs.type() == Type.INT && rhs.type() == Type.INT) {
121 type = Type.INT;
122 } else {
123 type = Type.ANY;
124 JAST.compilationUnit.reportSemanticError(line(), "Invalid operand types for +");
125 }
126 return this;
127 }
128
129
132 public void codegen(CLEmitter output) {
133 lhs.codegen(output);
134 rhs.codegen(output);
135 output.addNoArgInstruction(IADD);
136 }
137 }
138
139
142 class JSubtractOp extends JBinaryExpression {
143
150 public JSubtractOp(int line, JExpression lhs, JExpression rhs) {
151 super(line, "-", lhs, rhs);
152 }
153
154
157 public JExpression analyze(Context context) {
158 lhs = (JExpression) lhs.analyze(context);
159 rhs = (JExpression) rhs.analyze(context);
160 lhs.type().mustMatchExpected(line(), Type.INT);
161 rhs.type().mustMatchExpected(line(), Type.INT);
162 type = Type.INT;
163 return this;
164 }
165
166
169 public void codegen(CLEmitter output) {
170 lhs.codegen(output);
171 rhs.codegen(output);
172 output.addNoArgInstruction(ISUB);
173 }
174 }
175
176
179 class JDivideOp extends JBinaryExpression {
180
187 public JDivideOp(int line, JExpression lhs, JExpression rhs) {
188 super(line, "/", lhs, rhs);
189 }
190
191
194 public JExpression analyze(Context context) {
195 return this;
197 }
198
199
202 public void codegen(CLEmitter output) {
203 }
205 }
206
207
210 class JRemainderOp extends JBinaryExpression {
211
218 public JRemainderOp(int line, JExpression lhs, JExpression rhs) {
219 super(line, "%", lhs, rhs);
220 }
221
222
225 public JExpression analyze(Context context) {
226 return this;
228 }
229
230
233 public void codegen(CLEmitter output) {
234 }
236 }
237
238
241 class JOrOp extends JBinaryExpression {
242
249 public JOrOp(int line, JExpression lhs, JExpression rhs) {
250 super(line, "|", lhs, rhs);
251 }
252
253
256 public JExpression analyze(Context context) {
257 return this;
259 }
260
261
264 public void codegen(CLEmitter output) {
265 }
267 }
268
269
272 class JXorOp extends JBinaryExpression {
273
280 public JXorOp(int line, JExpression lhs, JExpression rhs) {
281 super(line, "^", lhs, rhs);
282 }
283
284
287 public JExpression analyze(Context context) {
288 return this;
290 }
291
292
295 public void codegen(CLEmitter output) {
296 }
298 }
299
300
303 class JAndOp extends JBinaryExpression {
304
311 public JAndOp(int line, JExpression lhs, JExpression rhs) {
312 super(line, "&", lhs, rhs);
313 }
314
315
318 public JExpression analyze(Context context) {
319 return this;
321 }
322
323
326 public void codegen(CLEmitter output) {
327 }
329 }
330
331
334 class JALeftShiftOp extends JBinaryExpression {
335
342 public JALeftShiftOp(int line, JExpression lhs, JExpression rhs) {
343 super(line, "<<", lhs, rhs);
344 }
345
346
349 public JExpression analyze(Context context) {
350 return this;
352 }
353
354
357 public void codegen(CLEmitter output) {
358 }
360 }
361
362
365 class JARightShiftOp extends JBinaryExpression {
366
373 public JARightShiftOp(int line, JExpression lhs, JExpression rhs) {
374 super(line, ">>", lhs, rhs);
375 }
376
377
380 public JExpression analyze(Context context) {
381 return this;
383 }
384
385
388 public void codegen(CLEmitter output) {
389 }
391 }
392
393
396 class JLRightShiftOp extends JBinaryExpression {
397
404 public JLRightShiftOp(int line, JExpression lhs, JExpression rhs) {
405 super(line, ">>>", lhs, rhs);
406 }
407
408
411 public JExpression analyze(Context context) {
412 return this;
414 }
415
416
419 public void codegen(CLEmitter output) {
420 }
422 }
423