use crate::common::*;
#[derive(PartialEq, Debug)]
pub(crate) enum Expression<'src> {
Backtick {
contents: String,
token: Token<'src>,
},
Call { thunk: Thunk<'src> },
Concatination {
lhs: Box<Expression<'src>>,
rhs: Box<Expression<'src>>,
},
Conditional {
lhs: Box<Expression<'src>>,
rhs: Box<Expression<'src>>,
then: Box<Expression<'src>>,
otherwise: Box<Expression<'src>>,
inverted: bool,
},
Group { contents: Box<Expression<'src>> },
StringLiteral { string_literal: StringLiteral<'src> },
Variable { name: Name<'src> },
}
impl<'src> Expression<'src> {
pub(crate) fn variables<'expression>(&'expression self) -> Variables<'expression, 'src> {
Variables::new(self)
}
}
impl<'src> Display for Expression<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Expression::Backtick { contents, .. } => write!(f, "`{}`", contents),
Expression::Concatination { lhs, rhs } => write!(f, "{} + {}", lhs, rhs),
Expression::Conditional {
lhs,
rhs,
then,
otherwise,
inverted,
} => write!(
f,
"if {} {} {} {{ {} }} else {{ {} }} ",
lhs,
if *inverted { "!=" } else { "==" },
rhs,
then,
otherwise
),
Expression::StringLiteral { string_literal } => write!(f, "{}", string_literal),
Expression::Variable { name } => write!(f, "{}", name.lexeme()),
Expression::Call { thunk } => write!(f, "{}", thunk),
Expression::Group { contents } => write!(f, "({})", contents),
}
}
}