feat: new fmt command and pretty printing works
This commit is contained in:
parent
6a39d4349a
commit
6aae184848
|
@ -31,6 +31,9 @@ pub enum UplcCommand {
|
|||
#[clap(short, long)]
|
||||
out: Option<String>,
|
||||
},
|
||||
Fmt {
|
||||
input: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for Args {
|
||||
|
|
|
@ -47,6 +47,15 @@ fn main() -> anyhow::Result<()> {
|
|||
fs::write(&out_name, &bytes)?;
|
||||
}
|
||||
}
|
||||
UplcCommand::Fmt { input } => {
|
||||
let code = std::fs::read_to_string(&input)?;
|
||||
|
||||
let program = parser::program(&code)?;
|
||||
|
||||
let pretty = program.to_pretty();
|
||||
|
||||
fs::write(&input, pretty)?;
|
||||
}
|
||||
UplcCommand::Unflat { input, print, out } => {
|
||||
let bytes = std::fs::read(&input)?;
|
||||
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
@ -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(¶meter_name.text))
|
||||
.append(RcDoc::line())
|
||||
.append(body.to_doc())
|
||||
.nest(2),
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
(program 11.22.33
|
||||
(program
|
||||
11.22.33
|
||||
(con integer 11)
|
||||
)
|
Loading…
Reference in New Issue