summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/UnaryExpression.java
blob: 64caa3e929ba11ad17ac2a813a105d011f290845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * UnaryExpression.java 1.0
 *
 * Jun 20, 1997 by mich
 * Sep 29, 1997 by bv
 * Oct 10, 1997 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 10, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import io.devnulllabs.openjava.mop.Environment;
import io.devnulllabs.openjava.mop.OJClass;
import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The <code>UnaryExpression</code> class presents for an expression which
 * consists of unary operator with one Expression.
 * <br>
 * The unary expressions are :
 * <br><blockquote>
 * <code>expr++</code>, <code>expr--</code>,
 * <code>++expr</code>, <code>--expr</code>,
 * <code>^expr</code>, <code>!expr</code>,
 * <code>+expr</code> or <code>-expr</code>
 * </blockquote><br>
 * ,where <code>expr<code> is an expression.
 * <br>
 * If the operator in the expression of the operand has week unity,
 * this automatically produces the code in which the operand
 * is enclosed by parenthesises.
 * <br>
 * In the case the operand is <code>y = x</code> and
 * the urary operator is <code>+</code>,
 * this produces the code :
 * <br><blockquote><pre>
 *     +(y = x)
 * </pre></blockquote><br>
 *
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 * @see io.devnulllabs.openjava.ptree.Expression
 */
public class UnaryExpression extends NonLeaf implements Expression {
    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     i++
     * </pre></blockquote><br>
     */
    public static final int POST_INCREMENT = 0;

    /**
     * Post decrement operator like:
     * <br><blockquote><pre>
     *     i--
     * </pre></blockquote><br>
     */
    public static final int POST_DECREMENT = 1;

    /**
     * Pre increment operator like:
     * <br><blockquote><pre>
     *     ++i
     * </pre></blockquote><br>
     */
    public static final int PRE_INCREMENT = 2;

    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     --i
     * </pre></blockquote><br>
     */
    public static final int PRE_DECREMENT = 3;

    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     ~i
     * </pre></blockquote><br>
     */
    public static final int BIT_NOT = 4;

    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     ! c
     * </pre></blockquote><br>
     */
    public static final int NOT = 5;

    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     +i
     * </pre></blockquote><br>
     */
    public static final int PLUS = 6;

    /**
     * Post increment operator like:
     * <br><blockquote><pre>
     *     -i
     * </pre></blockquote><br>
     */
    public static final int MINUS = 7;

    private static final String opr_string[] =
        { "++", "--", "++", "--", "~", "!", "+", "-" };

    /** operator */
    private int opr = -1;

    /**
     * Allocates a new object.
     *
     * @param  opr  the operator of this unary expression.
     * @param  expr  the expression.
     */
    public UnaryExpression(int opr, Expression expr) {
        super();
        set((ParseTree) expr);
        this.opr = opr;
    }

    /**
     * Allocates a new object.
     *
     * @param  expr  the expression.
     * @param  opr  the operator of this unary expression.
     */
    public UnaryExpression(Expression expr, int opr) {
        super();
        set((ParseTree) expr);
        this.opr = opr;
    }

    UnaryExpression() {
        super();
    }

    public ParseTree makeRecursiveCopy() {
        UnaryExpression result = (UnaryExpression) super.makeRecursiveCopy();
        result.opr = this.opr;
        return result;
    }

    public ParseTree makeCopy() {
        UnaryExpression result = (UnaryExpression) super.makeCopy();
        result.opr = this.opr;
        return result;
    }

    /**
     * Gets the expression operated in this expression.
     *
     * @return  the expression.
     */
    public Expression getExpression() {
        return (Expression) elementAt(0);
    }

    /**
     * Sets the expression operated in this expression.
     *
     * @param  expr  the expression to set.
     */
    public void setExpression(Expression expr) {
        setElementAt(expr, 0);
    }

    /**
     * Gets the operator of this unary expression.
     *
     * @return  the operator.
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#POST_INCREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#POST_DECREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PRE_INCREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PRE_DECREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#BIT_NOT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#NOT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PLUS
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#MINUS
     */
    public int getOperator() {
        return opr;
    }

    /**
     * Sets the operator of this unary expression.
     *
     * @param  opr  the operator id to set.
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#POST_INCREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#POST_DECREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PRE_INCREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PRE_DECREMENT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#BIT_NOT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#NOT
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#PLUS
     * @see io.devnulllabs.openjava.ptree.UnaryExpression#MINUS
     */
    public void setOperator(int opr) {
        this.opr = opr;
    }

    /**
     * Tests if the operator of unary expression is a postfix operator.
     *
     * @return  true  if the operator is postfix.
     */
    public boolean isPostfix() {
        if (opr == POST_DECREMENT || opr == POST_INCREMENT)
            return true;
        return false;
    }

    /**
     * Tests if the operator of unary expression is a prefix operator.
     *
     * @return  true  if the operator is prefix.
     */
    public boolean isPrefix() {
        return !isPostfix();
    }

    public String operatorString() {
        return opr_string[opr];
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

    public OJClass getType(Environment env) throws Exception {
        return getExpression().getType(env);
    }

}