summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/ConditionalExpression.java
blob: 424c41658eee5697dcf84b7bb4e911f029e05374 (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
/*
 * ConditionalExpression.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>ConditionalExpression</code> class represents
 * a conditional expression like:
 * <br><blockquote><pre>
 *     (i == 1) ? 3 : 4
 * </pre></blockquote><br>
 * This consists of a conditional part, true case part, and
 * false case part.
 * Each part of them is an expression.
 * <br>
 * If the operator in the expression of the operands has week unity,
 * this automatically produces the code in which the operands
 * are enclosed by parenthesises.
 * <br>
 * In the case the conditional part is <code>f = f()</code>,
 * the true case part is <code>"red"</code>
 * and the false case part is <code>str = "blue"</code>
 * this produces the code :
 * <br><blockquote><pre>
 *     (f = f()) ? "red" : (str = "blue")
 * </pre></blockquote><br>
 *
 * @see io.devnulllabs.openjava.ptree.Expression
 */
public class ConditionalExpression extends NonLeaf implements Expression {
    /**
     * Allocates a new conditional expression object.
     *
     * @param  condition  the conditional part of this expression.
     * @param  truecase  the expression to be evaluated when conditional
     *                   part is true.
     * @param  falsecase  the expression to be evaluated when conditional
     *                    part is false.
     */
    public ConditionalExpression(
        Expression condition,
        Expression truecase,
        Expression falsecase) {
        super();
        set(condition, truecase, falsecase);
    }

    ConditionalExpression() {
        super();
    }

    /**
     * Gets the conditional part of this conditional expression.
     *
     * @return  the expression of this conditional part.
     */
    public Expression getCondition() {
        return (Expression) elementAt(0);
    }

    /**
     * Sets the conditional part of this conditional expression.
     *
     * @param  expr  the expression to set as this conditional part.
     */
    public void setCondition(Expression expr) {
        setElementAt(expr, 0);
    }

    /**
     * Gets the true case part of this conditional expression.
     *
     * @return  the expression of this true case part.
     */
    public Expression getTrueCase() {
        return (Expression) elementAt(1);
    }

    /**
     * Sets the true case part of this conditional expression.
     *
     * @param  expr  the expression to set as this true part.
     */
    public void setTrueCase(Expression expr) {
        setElementAt(expr, 1);
    }

    /**
     * Gets the false case part of this.
     *
     * @return  the expression of this false case part.
     */
    public Expression getFalseCase() {
        return (Expression) elementAt(2);
    }

    /**
     * Sets the false case part of this.
     *
     * @param  expr  the expression to set as this false part.
     */
    public void setFalseCase(Expression expr) {
        setElementAt(expr, 2);
    }

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

    public OJClass getType(Environment env) throws Exception {
        return BinaryExpression.chooseType(
            getTrueCase().getType(env),
            getFalseCase().getType(env));
    }

}