use crate::ast::identifiers::global_id::DefId;
use crate::ast::resugared::*;
use crate::ast::visitors::*;
use crate::ast::*;
use crate::printer::*;
use std::collections::HashSet;
pub struct BinOp {
pub known_ops: HashSet<DefId>,
}
impl BinOp {
pub fn new(known_ops: &[DefId]) -> Self {
Self {
known_ops: HashSet::from_iter(known_ops.iter().cloned()),
}
}
}
impl AstVisitorMut for BinOp {
fn enter_expr_kind(&mut self, x: &mut ExprKind) {
let ExprKind::App {
head,
args,
generic_args,
bounds_impls,
trait_,
}: &mut ExprKind = x
else {
return;
};
let ExprKind::GlobalId(id) = &*head.kind else {
return;
};
let [lhs, rhs] = &args[..] else { return };
if self.known_ops.iter().any(|defid| id == defid) {
*x = ExprKind::Resugared(ResugaredExprKind::BinOp {
op: id.clone(),
lhs: lhs.clone(),
rhs: rhs.clone(),
generic_args: generic_args.clone(),
bounds_impls: bounds_impls.clone(),
trait_: trait_.clone(),
});
}
}
}
impl Resugaring for BinOp {
fn name(&self) -> String {
"binop".to_string()
}
}