feat: new fmt command and pretty printing works

This commit is contained in:
rvcas
2022-06-18 22:53:02 -04:00
committed by Lucas
parent 6a39d4349a
commit 6aae184848
6 changed files with 45 additions and 13 deletions

View File

@@ -162,10 +162,14 @@ impl Converter {
pub fn debruijn_to_name(&mut self, term: Term<DeBruijn>) -> Result<Term<Name>, Error> {
let converted_term = match term {
Term::Var(index) => Term::Var(Name {
text: String::from("i"),
unique: self.get_unique(index)?,
}),
Term::Var(index) => {
let unique = self.get_unique(index)?;
Term::Var(Name {
text: format!("i_{}", unique),
unique,
})
}
Term::Delay(term) => Term::Delay(Box::new(self.debruijn_to_name(*term)?)),
Term::Lambda {
parameter_name,
@@ -176,7 +180,7 @@ impl Converter {
let unique = self.get_unique(parameter_name)?;
let name = Name {
text: String::from("i"),
text: format!("i_{}", unique),
unique,
};

View File

@@ -8,7 +8,21 @@ impl Program<Name> {
self.to_doc().render(80, &mut w).unwrap();
String::from_utf8(w).unwrap()
String::from_utf8(w)
.unwrap()
.lines()
// This is a hack to deal with blank newlines
// that end up with a bunch of useless whitespace
// because of the nesting
.map(|l| {
if l.chars().all(|c| c.is_whitespace()) {
"".to_string()
} else {
l.to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn to_doc(&self) -> RcDoc<()> {
@@ -29,7 +43,7 @@ impl Program<Name> {
impl Term<Name> {
fn to_doc(&self) -> RcDoc<()> {
match self {
Term::Var(name) => RcDoc::text(format!("{}_{}", name.text, name.unique)),
Term::Var(name) => RcDoc::text(&name.text),
Term::Delay(term) => RcDoc::text("(")
.append(
RcDoc::text("delay")
@@ -46,10 +60,7 @@ impl Term<Name> {
.append(
RcDoc::text("lam")
.append(RcDoc::line())
.append(RcDoc::text(format!(
"{}_{}",
parameter_name.text, parameter_name.unique
)))
.append(RcDoc::text(&parameter_name.text))
.append(RcDoc::line())
.append(body.to_doc())
.nest(2),

View File

@@ -24,6 +24,10 @@ fn integer() {
let name_program: Program<Name> = decoded_program.try_into().unwrap();
assert_eq!(parsed_program, name_program);
let pretty = name_program.to_pretty();
assert_eq!(pretty, code);
}
#[test]

View File

@@ -1,3 +1,4 @@
(program 11.22.33
(con integer 11)
(program
11.22.33
(con integer 11)
)