summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/AssignmentExpression.java
blob: 9c4d538278a7289c84d70cf113ff64a666b3c00f (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
/*
 * AssignmentExpression.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>AssignmentExpression</code> class represents
 * an assignment expression with an assignment operator.
 *
 * @see io.devnulllabs.openjava.ptree.Expression
 */
public class AssignmentExpression extends NonLeaf implements Expression {
    public final static int EQUALS = 0;
    public final static int MULT = 1;
    public final static int DIVIDE = 2;
    public final static int MOD = 3;
    public final static int ADD = 4;
    public final static int SUB = 5;
    public final static int SHIFT_L = 6;
    public final static int SHIFT_R = 7;
    public final static int SHIFT_RR = 8;
    public final static int AND = 9;
    public final static int XOR = 10;
    public final static int OR = 11;

    final static String opr_string[] =
        {
            "=",
            "*=",
            "/=",
            "%=",
            "+=",
            "-=",
            "<<=",
            ">>=",
            ">>>=",
            "&=",
            "^=",
            "|=" };

    private int opr = -1;

    /**
     * Allocates a new object.
     *
     * @param  lexp  the left expression.
     * @param  opr  the id number of the operator.
     * @param  rexp  the right expression.
     */
    public AssignmentExpression(Expression lexp, int opr, Expression rexp) {
        super();
        set((ParseTree) lexp, (ParseTree) rexp);
        this.opr = opr;
    }

    public AssignmentExpression(Expression lexp, String opr, Expression rexp) {
        this(lexp, 0, rexp);
        for (int i = 0; i < opr_string.length; ++i) {
            if (opr_string[i].equals(opr))
                this.opr = i;
        }
    }

    public AssignmentExpression() {
        super();
    }

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

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

    /**
     * Gets the expression of the left operand.
     *
     * @return  the left expression.
     */
    public Expression getLeft() {
        return (Expression) elementAt(0);
    }

    /**
     * Sets the expression of the left operand.
     *
     * @param  lexpr  the left expression.
     */
    public void setLeft(Expression lexpr) {
        setElementAt(lexpr, 0);
    }

    /**
     * Gets the expression of the right operand.
     *
     * @return  the right expression.
     */
    public Expression getRight() {
        return (Expression) elementAt(1);
    }

    /**
     * Sets the expression of the right operand.
     *
     * @param  rexpr  the right expression.
     */
    public void setRight(Expression rexpr) {
        setElementAt(rexpr, 1);
    }

    /**
     * Gets the id number of the operator.
     *
     * @return  the id number of the operator.
     */
    public int getOperator() {
        return this.opr;
    }

    /**
     * Sets the id number of the operator.
     *
     * @param  opr  the id number of the operator.
     */
    public void setOperator(int opr) {
        this.opr = opr;
    }

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

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

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