feat: 3 new error cases and more generic pretty printing

This commit is contained in:
rvcas
2022-07-15 23:26:57 -04:00
committed by Kasey White
parent 598c5364fe
commit f332dfeb38
10 changed files with 118 additions and 55 deletions

View File

@@ -32,8 +32,11 @@ pub enum UplcCommand {
out: Option<String>,
},
/// Format an Untyped Plutus Core program
Fmt { input: PathBuf },
Fmt {
input: PathBuf,
#[clap(short, long)]
print: bool,
},
/// Evaluate an Untyped Plutus Core program
Eval {
input: PathBuf,

View File

@@ -1,4 +1,4 @@
use std::fs;
use std::{fmt::Write as _, fs};
use uplc::{
ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term},
@@ -27,7 +27,7 @@ fn main() -> anyhow::Result<()> {
let mut output = String::new();
for (i, byte) in bytes.iter().enumerate() {
output.push_str(&format!("{:08b}", byte));
let _ = write!(output, "{:08b}", byte);
if (i + 1) % 4 == 0 {
output.push('\n');
@@ -47,14 +47,18 @@ fn main() -> anyhow::Result<()> {
fs::write(&out_name, &bytes)?;
}
}
UplcCommand::Fmt { input } => {
UplcCommand::Fmt { input, print } => {
let code = std::fs::read_to_string(&input)?;
let program = parser::program(&code)?;
let pretty = program.to_pretty();
fs::write(&input, pretty)?;
if print {
println!("{}", pretty);
} else {
fs::write(&input, pretty)?;
}
}
UplcCommand::Unflat { input, print, out } => {
let bytes = std::fs::read(&input)?;
@@ -105,7 +109,7 @@ fn main() -> anyhow::Result<()> {
}
}
println!("Costs - memory: {} & cpu: {}", cost.mem, cost.cpu);
println!("\nCosts - memory: {} & cpu: {}", cost.mem, cost.cpu);
}
},
}