feat: better clap commands

This commit is contained in:
rvcas 2022-06-06 23:55:01 -04:00
parent 895f279be0
commit b47030b86d
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
3 changed files with 41 additions and 46 deletions

View File

@ -1,10 +1,19 @@
use std::path::PathBuf;
use clap::Parser;
use clap::{Parser, Subcommand};
#[derive(Parser)]
pub struct Cli {
pub input: PathBuf,
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub enum Cli {
#[clap(subcommand)]
Uplc(UplcCommand),
}
#[derive(Subcommand)]
pub enum UplcCommand {
Flat { input: PathBuf },
Unflat { input: PathBuf },
}
impl Default for Cli {

View File

@ -1,59 +1,45 @@
use uplc::{
ast::{DeBruijn, NamedDeBruijn, Program},
ast::{DeBruijn, FakeNamedDeBruijn, Program},
parser,
};
use neptune::Cli;
use neptune::{Cli, UplcCommand};
fn main() -> anyhow::Result<()> {
let args = Cli::default();
let code = std::fs::read_to_string(&args.input)?;
match args {
Cli::Uplc(uplc) => match uplc {
UplcCommand::Flat { input } => {
let code = std::fs::read_to_string(&input)?;
let program = parser::program(&code)?;
println!("\nName:");
let program = Program::<DeBruijn>::try_from(program)?;
let bytes = program.to_flat()?;
for (i, byte) in bytes.iter().enumerate() {
print!("{:08b}", byte);
if (i + 1) % 4 == 0 {
println!();
} else {
print!(" ");
}
}
println!();
}
UplcCommand::Unflat { input } => {
let bytes = std::fs::read(&input)?;
let program = Program::<FakeNamedDeBruijn>::from_flat(&bytes)?;
println!("{:#?}", program);
let flat_bytes = program.to_flat()?;
print!("\nflat bits:\n");
for byte in flat_bytes {
print!("{:08b} ", byte);
}
println!();
let program_nd: Program<NamedDeBruijn> = program.try_into()?;
println!("\nNamed De Bruijn:");
println!("{:#?}", program_nd);
let flat_bytes = program_nd.to_flat()?;
print!("\nflat bits:\n");
for byte in flat_bytes {
print!("{:08b} ", byte);
},
}
println!();
let program_d: Program<DeBruijn> = program_nd.into();
println!("\nDe Bruijn:");
println!("{:#?}", program_d);
let flat_bytes = program_d.to_flat()?;
print!("\nflat bits:\n");
for byte in flat_bytes {
print!("{:08b} ", byte);
}
println!();
Ok(())
}

View File

@ -1,7 +1,7 @@
(program 11.22.33
[
(
lam x (lam x y)) (con string "PT8"
lam x (lam x x)) (con string "PT8"
)
]
)