use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TernaryExpression {
pub condition: Expression,
pub if_true: Expression,
pub if_false: Expression,
pub span: Span,
pub id: NodeID,
}
impl fmt::Display for TernaryExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let maybe_paren = |f: &mut fmt::Formatter, expr: &Expression| {
if expr.precedence() > 0 { write!(f, "{expr}") } else { write!(f, "({expr})") }
};
maybe_paren(f, &self.condition)?;
write!(f, " ? ")?;
maybe_paren(f, &self.if_true)?;
write!(f, " : ")?;
maybe_paren(f, &self.if_false)
}
}
impl From<TernaryExpression> for Expression {
fn from(value: TernaryExpression) -> Self {
Expression::Ternary(Box::new(value))
}
}
crate::simple_node_impl!(TernaryExpression);