add make::expr_op

This commit is contained in:
Yoshua Wuyts 2021-08-10 19:48:32 +02:00
parent c943057038
commit 20c3b20db3
2 changed files with 39 additions and 0 deletions

View File

@ -350,6 +350,42 @@ impl ast::BinExpr {
} }
} }
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BinOp::BooleanOr => write!(f, "||"),
BinOp::BooleanAnd => write!(f, "&&"),
BinOp::EqualityTest => write!(f, "=="),
BinOp::NegatedEqualityTest => write!(f, "!="),
BinOp::LesserEqualTest => write!(f, "<="),
BinOp::GreaterEqualTest => write!(f, ">="),
BinOp::LesserTest => write!(f, "<"),
BinOp::GreaterTest => write!(f, ">"),
BinOp::Addition => write!(f, "+"),
BinOp::Multiplication => write!(f, "*"),
BinOp::Subtraction => write!(f, "-"),
BinOp::Division => write!(f, "/"),
BinOp::Remainder => write!(f, "%"),
BinOp::LeftShift => write!(f, "<<"),
BinOp::RightShift => write!(f, ">>"),
BinOp::BitwiseXor => write!(f, "^"),
BinOp::BitwiseOr => write!(f, "|"),
BinOp::BitwiseAnd => write!(f, "&"),
BinOp::Assignment => write!(f, "="),
BinOp::AddAssign => write!(f, "+="),
BinOp::DivAssign => write!(f, "/="),
BinOp::MulAssign => write!(f, "*="),
BinOp::RemAssign => write!(f, "%="),
BinOp::ShrAssign => write!(f, ">>="),
BinOp::ShlAssign => write!(f, "<<="),
BinOp::SubAssign => write!(f, "-"),
BinOp::BitOrAssign => write!(f, "|="),
BinOp::BitAndAssign => write!(f, "&="),
BinOp::BitXorAssign => write!(f, "^="),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum RangeOp { pub enum RangeOp {
/// `..` /// `..`

View File

@ -264,6 +264,9 @@ pub fn expr_path(path: ast::Path) -> ast::Expr {
pub fn expr_continue() -> ast::Expr { pub fn expr_continue() -> ast::Expr {
expr_from_text("continue") expr_from_text("continue")
} }
pub fn expr_op(op: ast::BinOp, lhs: ast::Expr, rhs: ast::Expr) -> ast::Expr {
expr_from_text(&format!("{} {} {}", lhs, op, rhs))
}
pub fn expr_break(expr: Option<ast::Expr>) -> ast::Expr { pub fn expr_break(expr: Option<ast::Expr>) -> ast::Expr {
match expr { match expr {
Some(expr) => expr_from_text(&format!("break {}", expr)), Some(expr) => expr_from_text(&format!("break {}", expr)),