Source code: openjava/ptree/UnaryExpression.java
1 /*
2 * UnaryExpression.java 1.0
3 *
4 * Jun 20, 1997 by mich
5 * Sep 29, 1997 by bv
6 * Oct 10, 1997 by mich
7 *
8 * @see openjava.ptree.ParseTree
9 * @version 1.0 last updated: Oct 10, 1997
10 * @author Michiaki Tatsubori
11 */
12 package openjava.ptree;
13
14 import openjava.mop.Environment;
15 import openjava.mop.OJClass;
16 import openjava.ptree.util.ParseTreeVisitor;
17
18 /**
19 * The <code>UnaryExpression</code> class presents for an expression which
20 * consists of unary operator with one Expression.
21 * <br>
22 * The unary expressions are :
23 * <br><blockquote>
24 * <code>expr++</code>, <code>expr--</code>,
25 * <code>++expr</code>, <code>--expr</code>,
26 * <code>^expr</code>, <code>!expr</code>,
27 * <code>+expr</code> or <code>-expr</code>
28 * </blockquote><br>
29 * ,where <code>expr<code> is an expression.
30 * <br>
31 * If the operator in the expression of the operand has week unity,
32 * this automatically produces the code in which the operand
33 * is enclosed by parenthesises.
34 * <br>
35 * In the case the operand is <code>y = x</code> and
36 * the urary operator is <code>+</code>,
37 * this produces the code :
38 * <br><blockquote><pre>
39 * +(y = x)
40 * </pre></blockquote><br>
41 *
42 * @see openjava.ptree.NonLeaf
43 * @see openjava.ptree.Expression
44 */
45 public class UnaryExpression extends NonLeaf implements Expression {
46 /**
47 * Post increment operator like:
48 * <br><blockquote><pre>
49 * i++
50 * </pre></blockquote><br>
51 */
52 public static final int POST_INCREMENT = 0;
53
54 /**
55 * Post decrement operator like:
56 * <br><blockquote><pre>
57 * i--
58 * </pre></blockquote><br>
59 */
60 public static final int POST_DECREMENT = 1;
61
62 /**
63 * Pre increment operator like:
64 * <br><blockquote><pre>
65 * ++i
66 * </pre></blockquote><br>
67 */
68 public static final int PRE_INCREMENT = 2;
69
70 /**
71 * Post increment operator like:
72 * <br><blockquote><pre>
73 * --i
74 * </pre></blockquote><br>
75 */
76 public static final int PRE_DECREMENT = 3;
77
78 /**
79 * Post increment operator like:
80 * <br><blockquote><pre>
81 * ~i
82 * </pre></blockquote><br>
83 */
84 public static final int BIT_NOT = 4;
85
86 /**
87 * Post increment operator like:
88 * <br><blockquote><pre>
89 * ! c
90 * </pre></blockquote><br>
91 */
92 public static final int NOT = 5;
93
94 /**
95 * Post increment operator like:
96 * <br><blockquote><pre>
97 * +i
98 * </pre></blockquote><br>
99 */
100 public static final int PLUS = 6;
101
102 /**
103 * Post increment operator like:
104 * <br><blockquote><pre>
105 * -i
106 * </pre></blockquote><br>
107 */
108 public static final int MINUS = 7;
109
110 private static final String opr_string[] =
111 { "++", "--", "++", "--", "~", "!", "+", "-" };
112
113 /** operator */
114 private int opr = -1;
115
116 /**
117 * Allocates a new object.
118 *
119 * @param opr the operator of this unary expression.
120 * @param expr the expression.
121 */
122 public UnaryExpression(int opr, Expression expr) {
123 super();
124 set((ParseTree) expr);
125 this.opr = opr;
126 }
127
128 /**
129 * Allocates a new object.
130 *
131 * @param expr the expression.
132 * @param opr the operator of this unary expression.
133 */
134 public UnaryExpression(Expression expr, int opr) {
135 super();
136 set((ParseTree) expr);
137 this.opr = opr;
138 }
139
140 UnaryExpression() {
141 super();
142 }
143
144 public ParseTree makeRecursiveCopy() {
145 UnaryExpression result = (UnaryExpression) super.makeRecursiveCopy();
146 result.opr = this.opr;
147 return result;
148 }
149
150 public ParseTree makeCopy() {
151 UnaryExpression result = (UnaryExpression) super.makeCopy();
152 result.opr = this.opr;
153 return result;
154 }
155
156 /**
157 * Gets the expression operated in this expression.
158 *
159 * @return the expression.
160 */
161 public Expression getExpression() {
162 return (Expression) elementAt(0);
163 }
164
165 /**
166 * Sets the expression operated in this expression.
167 *
168 * @param expr the expression to set.
169 */
170 public void setExpression(Expression expr) {
171 setElementAt(expr, 0);
172 }
173
174 /**
175 * Gets the operator of this unary expression.
176 *
177 * @return the operator.
178 * @see openjava.ptree.UnaryExpression#POST_INCREMENT
179 * @see openjava.ptree.UnaryExpression#POST_DECREMENT
180 * @see openjava.ptree.UnaryExpression#PRE_INCREMENT
181 * @see openjava.ptree.UnaryExpression#PRE_DECREMENT
182 * @see openjava.ptree.UnaryExpression#BIT_NOT
183 * @see openjava.ptree.UnaryExpression#NOT
184 * @see openjava.ptree.UnaryExpression#PLUS
185 * @see openjava.ptree.UnaryExpression#MINUS
186 */
187 public int getOperator() {
188 return opr;
189 }
190
191 /**
192 * Sets the operator of this unary expression.
193 *
194 * @param opr the operator id to set.
195 * @see openjava.ptree.UnaryExpression#POST_INCREMENT
196 * @see openjava.ptree.UnaryExpression#POST_DECREMENT
197 * @see openjava.ptree.UnaryExpression#PRE_INCREMENT
198 * @see openjava.ptree.UnaryExpression#PRE_DECREMENT
199 * @see openjava.ptree.UnaryExpression#BIT_NOT
200 * @see openjava.ptree.UnaryExpression#NOT
201 * @see openjava.ptree.UnaryExpression#PLUS
202 * @see openjava.ptree.UnaryExpression#MINUS
203 */
204 public void setOperator(int opr) {
205 this.opr = opr;
206 }
207
208 /**
209 * Tests if the operator of unary expression is a postfix operator.
210 *
211 * @return true if the operator is postfix.
212 */
213 public boolean isPostfix() {
214 if (opr == POST_DECREMENT || opr == POST_INCREMENT)
215 return true;
216 return false;
217 }
218
219 /**
220 * Tests if the operator of unary expression is a prefix operator.
221 *
222 * @return true if the operator is prefix.
223 */
224 public boolean isPrefix() {
225 return !isPostfix();
226 }
227
228 public String operatorString() {
229 return opr_string[opr];
230 }
231
232 public void accept(ParseTreeVisitor v) throws ParseTreeException {
233 v.visit(this);
234 }
235
236 public OJClass getType(Environment env) throws Exception {
237 return getExpression().getType(env);
238 }
239
240 }