Source code: openjava/ptree/ConditionalExpression.java
1 /*
2 * ConditionalExpression.java 1.0
3 *
4 *
5 * Jun 20, 1997 by mich
6 * Sep 29, 1997 by bv
7 * Oct 10, 1997 by mich
8 *
9 * @see openjava.ptree.ParseTree
10 * @version 1.0 last updated: Oct 10, 1997
11 * @author Michiaki Tatsubori
12 */
13 package openjava.ptree;
14
15 import openjava.mop.Environment;
16 import openjava.mop.OJClass;
17 import openjava.ptree.util.ParseTreeVisitor;
18
19 /**
20 * The <code>ConditionalExpression</code> class represents
21 * a conditional expression like:
22 * <br><blockquote><pre>
23 * (i == 1) ? 3 : 4
24 * </pre></blockquote><br>
25 * This consists of a conditional part, true case part, and
26 * false case part.
27 * Each part of them is an expression.
28 * <br>
29 * If the operator in the expression of the operands has week unity,
30 * this automatically produces the code in which the operands
31 * are enclosed by parenthesises.
32 * <br>
33 * In the case the conditional part is <code>f = f()</code>,
34 * the true case part is <code>"red"</code>
35 * and the false case part is <code>str = "blue"</code>
36 * this produces the code :
37 * <br><blockquote><pre>
38 * (f = f()) ? "red" : (str = "blue")
39 * </pre></blockquote><br>
40 *
41 * @see openjava.ptree.Expression
42 */
43 public class ConditionalExpression extends NonLeaf implements Expression {
44 /**
45 * Allocates a new conditional expression object.
46 *
47 * @param condition the conditional part of this expression.
48 * @param truecase the expression to be evaluated when conditional
49 * part is true.
50 * @param falsecase the expression to be evaluated when conditional
51 * part is false.
52 */
53 public ConditionalExpression(
54 Expression condition,
55 Expression truecase,
56 Expression falsecase) {
57 super();
58 set(condition, truecase, falsecase);
59 }
60
61 ConditionalExpression() {
62 super();
63 }
64
65 /**
66 * Gets the conditional part of this conditional expression.
67 *
68 * @return the expression of this conditional part.
69 */
70 public Expression getCondition() {
71 return (Expression) elementAt(0);
72 }
73
74 /**
75 * Sets the conditional part of this conditional expression.
76 *
77 * @param expr the expression to set as this conditional part.
78 */
79 public void setCondition(Expression expr) {
80 setElementAt(expr, 0);
81 }
82
83 /**
84 * Gets the true case part of this conditional expression.
85 *
86 * @return the expression of this true case part.
87 */
88 public Expression getTrueCase() {
89 return (Expression) elementAt(1);
90 }
91
92 /**
93 * Sets the true case part of this conditional expression.
94 *
95 * @param expr the expression to set as this true part.
96 */
97 public void setTrueCase(Expression expr) {
98 setElementAt(expr, 1);
99 }
100
101 /**
102 * Gets the false case part of this.
103 *
104 * @return the expression of this false case part.
105 */
106 public Expression getFalseCase() {
107 return (Expression) elementAt(2);
108 }
109
110 /**
111 * Sets the false case part of this.
112 *
113 * @param expr the expression to set as this false part.
114 */
115 public void setFalseCase(Expression expr) {
116 setElementAt(expr, 2);
117 }
118
119 public void accept(ParseTreeVisitor v) throws ParseTreeException {
120 v.visit(this);
121 }
122
123 public OJClass getType(Environment env) throws Exception {
124 return BinaryExpression.chooseType(
125 getTrueCase().getType(env),
126 getFalseCase().getType(env));
127 }
128
129 }