/* * 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 ConditionalExpression class represents * a conditional expression like: *
 *     (i == 1) ? 3 : 4
 * 

* This consists of a conditional part, true case part, and * false case part. * Each part of them is an expression. *
* 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. *
* In the case the conditional part is f = f(), * the true case part is "red" * and the false case part is str = "blue" * this produces the code : *
 *     (f = f()) ? "red" : (str = "blue")
 * 

* * @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)); } }