Skip to content

Commit

Permalink
Refactor move pretty to display crate
Browse files Browse the repository at this point in the history
  • Loading branch information
imaqtkatt committed Apr 12, 2024
1 parent b07fb4e commit aa08d08
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl RunOpts {
let res_term = net_to_term(&net, book, labels, self.linear, &mut diags);
eprint!("{diags}");
if self.pretty {
println!("{}\n---------------------------------------", res_term.pretty(0));
println!("{}\n---------------------------------------", res_term.display_pretty(0));
} else {
println!("{}\n---------------------------------------", res_term);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {

eprint!("{diagnostics}");
if pretty {
println!("{}", res_term.pretty(0))
println!("{}", res_term.display_pretty(0))
} else {
println!("{}", res_term);
}
Expand Down
114 changes: 114 additions & 0 deletions src/term/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,117 @@ impl Tag {
fn var_as_str(nam: &Option<Name>) -> &str {
nam.as_ref().map_or("*", Name::deref)
}

impl Term {
pub fn display_pretty(&self, tab: usize) -> impl fmt::Display + '_ {
DisplayFn(move |f| match self {
Term::Lam { tag, nam, bod } => {
write!(f, "{}λ{} {}", tag.display_padded(), var_as_str(nam), bod.display_pretty(tab))
}

Term::Var { nam } => write!(f, "{nam}"),

Term::Chn { tag, nam, bod } => {
write!(f, "{}λ${} {}", tag, var_as_str(nam), bod.display_pretty(tab))
}

Term::Lnk { nam } => write!(f, "${nam}"),

Term::Let { nam, val, nxt } => {
write!(f, "let {} = {};\n{}", var_as_str(nam), val.display_pretty(tab), nxt.display_pretty(tab))
}

Term::Use { nam, val, nxt } => {
write!(f, "use {} = {};\n{}", var_as_str(nam), val.display_pretty(tab), nxt.display_pretty(tab))
}

Term::App { tag, fun, arg } => {
write!(f, "{}({} {})", tag.display_padded(), fun.display_pretty(tab), arg.display_pretty(tab))
}

Term::Ltp { bnd, val, nxt } => {
write!(
f,
"let ({}) = {};\n{}",
DisplayJoin(|| bnd.iter().map(var_as_str), ", "),
val.display_pretty(tab),
nxt.display_pretty(tab),
)
}

Term::Tup { els } => write!(f, "({})", DisplayJoin(|| els.iter().map(|e| e.display_pretty(tab)), " ")),

Term::Dup { tag, bnd, val, nxt } => {
write!(
f,
"let {}{{{}}} = {};\n{}",
tag.display_padded(),
DisplayJoin(|| bnd.iter().map(var_as_str), " "),
val.display_pretty(tab),
nxt.display_pretty(tab),
)
}

Term::Sup { tag, els } => {
write!(
f,
"{}{{{}}}",
tag.display_padded(),
DisplayJoin(|| els.iter().map(|e| e.display_pretty(tab)), " ")
)
}

Term::Lst { els } => write!(f, "[{}]", DisplayJoin(|| els.iter().map(|e| e.display_pretty(tab)), " ")),

Term::Opx { opr, fst, snd } => {
write!(f, "({} {} {})", opr, fst.display_pretty(tab), snd.display_pretty(tab))
}

Term::Mat { arg, with, rules } => {
let with: Box<dyn std::fmt::Display> = if with.is_empty() {
Box::new(DisplayFn(|f| write!(f, "")))
} else {
Box::new(DisplayFn(|f| {
write!(f, "with {}", DisplayJoin(|| with.iter().map(|e| e.to_string()), ", "))
}))
};
writeln!(f, "match {} {}{{", arg.display_pretty(tab), with)?;
for rule in rules {
writeln!(
f,
"{:tab$}{}: {}",
"",
var_as_str(&rule.0),
rule.2.display_pretty(tab + 2),
tab = tab + 2
)?;
}
write!(f, "{:tab$}}}", "")?;
Ok(())
}

Term::Swt { arg, with, rules } => {
let with: Box<dyn std::fmt::Display> = if with.is_empty() {
Box::new(DisplayFn(|f| write!(f, "")))
} else {
Box::new(DisplayFn(|f| {
write!(f, "with {}", DisplayJoin(|| with.iter().map(|e| e.to_string()), ", "))
}))
};
writeln!(f, "switch {} {}{{", arg.display_pretty(tab), with)?;
for rule in rules {
writeln!(f, "{:tab$}{}: {}", "", rule.0, rule.1.display_pretty(tab + 2), tab = tab + 2)?;
}
write!(f, "{:tab$}}}", "")?;
Ok(())
}

Term::Nat { val } => write!(f, "#{val}"),
Term::Num { val } => write!(f, "{val}"),
Term::Str { val } => write!(f, "{val:?}"),
Term::Ref { nam } => write!(f, "{nam}"),
Term::Era => write!(f, "*"),
Term::Err => write!(f, "<Error>"),
})
}
}
1 change: 0 additions & 1 deletion src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub mod display;
pub mod load_book;
pub mod net_to_term;
pub mod parser;
pub mod pretty;
pub mod term_to_net;
pub mod transform;

Expand Down
118 changes: 0 additions & 118 deletions src/term/pretty.rs

This file was deleted.

0 comments on commit aa08d08

Please sign in to comment.