Skip to content

Commit

Permalink
Fix and tweak type printers
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Jan 14, 2024
1 parent 77a3b8a commit 3ab2607
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/type_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,15 @@ impl fmt::Display for Scheme {
}

for (pred_idx, pred) in preds.iter().enumerate() {
write!(f, "{} {}", pred.class, pred.ty)?;
write!(f, "{} ", pred.class)?;
let pred_parens = matches!(pred.ty.deref(), Ty::App(_, _));
if pred_parens {
write!(f, "(")?;
}
write!(f, "{}", pred.ty)?;
if pred_parens {
write!(f, ")")?;
}
if pred_idx != num_preds - 1 {
write!(f, ", ")?;
}
Expand Down
45 changes: 27 additions & 18 deletions src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ impl TyRef {
(args, ret)
}

fn is_fun_ty(&self) -> bool {
if let Ty::App(ty1, _) = self.deref() {
if let Ty::App(ty1_, _) = ty1.deref() {
if let Ty::Con(con) = ty1_.deref() {
if con == &id::arrow_ty_id() {
return true;
}
}
}
}
false
}

pub(crate) fn subst_gens(&self, gens: &[TyRef]) -> TyRef {
match self.deref() {
Ty::Var(_) | Ty::Con(_) => self.clone(),
Expand Down Expand Up @@ -344,22 +357,19 @@ impl<'a> fmt::Debug for LinkCellDebug<'a> {
}
}

fn ty_ref_needs_parens(ty: &TyRef) -> bool {
match ty.deref() {
Ty::Var(_) | Ty::Con(_) | Ty::Gen(_) => false,
Ty::App(_, _) => true,
}
}

impl fmt::Display for TyRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (fun_args, fun_ret) = self.split_fun_ty();
for arg in fun_args {
if ty_ref_needs_parens(&arg) {
write!(f, "({}) -> ", arg)?;
} else {
write!(f, "{} -> ", arg)?;
let parens = arg.is_fun_ty();
if parens {
write!(f, "(")?;
}
write!(f, "{}", arg)?;
if parens {
write!(f, ")")?;
}
write!(f, " -> ")?;
}
fmt::Display::fmt(fun_ret.deref(), f)
}
Expand All @@ -373,16 +383,15 @@ impl fmt::Display for Ty {
Ty::Con(id) => fmt::Display::fmt(id, f),

Ty::App(ty1, ty2) => {
let ty1_parens = ty_ref_needs_parens(ty1);
let ty2_parens = ty_ref_needs_parens(ty2);
if ty1_parens {
write!(f, "(")?;
if let Ty::Con(con) = ty1.deref() {
if con == &id::list_ty_id() {
return write!(f, "[{}]", ty2);
}
}

fmt::Display::fmt(ty1, f)?;
if ty1_parens {
write!(f, ")")?;
}
write!(f, " ")?;
let ty2_parens = matches!(ty2.deref(), Ty::App(_, _));
if ty2_parens {
write!(f, "(")?;
}
Expand Down

0 comments on commit 3ab2607

Please sign in to comment.