| JComparisonExpression.java |
1 // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7 /**
8 * This abstract base class is the AST node for a comparison expression.
9 */
10 abstract class JComparisonExpression extends JBooleanBinaryExpression {
11 /**
12 * Constructs an AST node for a comparison expression.
13 *
14 * @param line line in which the expression occurs in the source file.
15 * @param operator the comparison operator.
16 * @param lhs the lhs operand.
17 * @param rhs the rhs operand.
18 */
19 protected JComparisonExpression(int line, String operator, JExpression lhs, JExpression rhs) {
20 super(line, operator, lhs, rhs);
21 }
22
23 /**
24 * {@inheritDoc}
25 */
26 public JExpression analyze(Context context) {
27 lhs = (JExpression) lhs.analyze(context);
28 rhs = (JExpression) rhs.analyze(context);
29 lhs.type().mustMatchExpected(line(), Type.INT);
30 rhs.type().mustMatchExpected(line(), lhs.type());
31 type = Type.BOOLEAN;
32 return this;
33 }
34 }
35
36 /**
37 * The AST node for a greater-than (>) expression.
38 */
39 class JGreaterThanOp extends JComparisonExpression {
40 /**
41 * Constructs an AST node for a greater-than expression.
42 *
43 * @param line line in which the greater-than expression occurs in the source file.
44 * @param lhs lhs operand.
45 * @param rhs rhs operand.
46 */
47 public JGreaterThanOp(int line, JExpression lhs, JExpression rhs) {
48 super(line, ">", lhs, rhs);
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
55 lhs.codegen(output);
56 rhs.codegen(output);
57 output.addBranchInstruction(onTrue ? IF_ICMPGT : IF_ICMPLE, targetLabel);
58 }
59 }
60
61 /**
62 * The AST node for a less-than-or-equal-to (<=) expression.
63 */
64 class JLessEqualOp extends JComparisonExpression {
65
66 /**
67 * Constructs an AST node for a less-than-or-equal-to expression.
68 *
69 * @param line line in which the less-than-or-equal-to expression occurs in the source file.
70 * @param lhs lhs operand.
71 * @param rhs rhs operand.
72 */
73 public JLessEqualOp(int line, JExpression lhs, JExpression rhs) {
74 super(line, "<=", lhs, rhs);
75 }
76
77 /**
78 * {@inheritDoc}
79 */
80 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
81 lhs.codegen(output);
82 rhs.codegen(output);
83 output.addBranchInstruction(onTrue ? IF_ICMPLE : IF_ICMPGT, targetLabel);
84 }
85 }
86
87 /**
88 * The AST node for a greater-than-or-equal-to (>=) expression.
89 */
90 class JGreaterEqualOp extends JComparisonExpression {
91
92 /**
93 * Constructs an AST node for a greater-than-or-equal-to expression.
94 *
95 * @param line line in which the greater-than-or-equal-to expression occurs in the source file.
96 * @param lhs lhs operand.
97 * @param rhs rhs operand.
98 */
99 public JGreaterEqualOp(int line, JExpression lhs, JExpression rhs) {
100 super(line, ">=", lhs, rhs);
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
107 // TODO
108 }
109 }
110
111 /**
112 * The AST node for a less-than (<) expression.
113 */
114 class JLessThanOp extends JComparisonExpression {
115 /**
116 * Constructs an AST node for a less-than expression.
117 *
118 * @param line line in which the less-than expression occurs in the source file.
119 * @param lhs lhs operand.
120 * @param rhs rhs operand.
121 */
122 public JLessThanOp(int line, JExpression lhs, JExpression rhs) {
123 super(line, "<", lhs, rhs);
124 }
125
126 /**
127 * {@inheritDoc}
128 */
129 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
130 // TODO
131 }
132 }