Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: move compilation to build-subcommand #16

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,16 @@ use std::{fs::read_to_string, io, process::exit};
use thiserror::Error;

fn main() {
let cli = argh::from_env();
if let Err(e) = run(cli) {
let cli: Cli = argh::from_env();
let res = match cli.command {
Commands::Build(cmd) => build(cmd),
};
if let Err(e) = res {
eprintln!("error: {}", e);
exit(1);
}
}

fn run(cli: Cli) -> HuffResult {
let src = read_to_string(&cli.filename)?;
let filename: String = cli.filename;
match parse(&src) {
Ok(ast) => println!("{:?}", ast),
Err(errs) => errs.into_iter().for_each(|e| {
Report::build(ReportKind::Error, filename.clone(), e.span().start)
.with_config(Config::default().with_index_type(IndexType::Byte))
.with_message(e.reason())
.with_label(
Label::new((filename.clone(), e.span().into_range()))
.with_message(e.reason())
.with_color(Color::Red),
)
.finish()
.print(sources([(filename.clone(), &src)]))
.unwrap()
}),
}

Ok(())
}

#[derive(Error, Debug)]
enum HuffError {
/// Wrapper around `io::Error`
Expand All @@ -44,12 +24,50 @@ enum HuffError {
// Parser(Report),
// Parser(#[from] ParseError<usize, Token<'src>, huff_ast::Error>),
}

type HuffResult = Result<(), HuffError>;

fn build(cmd: BuildCommand) -> HuffResult {
let src = read_to_string(&cmd.filename)?;
let filename: String = cmd.filename;
match parse(&src) {
Ok(ast) => println!("{:?}", ast),
Err(errs) => {
errs.into_iter().for_each(|e| {
Report::build(ReportKind::Error, filename.clone(), e.span().start)
.with_config(Config::default().with_index_type(IndexType::Byte))
// .with_message(e.reason())
.with_label(
Label::new((filename.clone(), e.span().into_range()))
.with_message(e.reason())
.with_color(Color::Red),
)
.finish()
.print(sources([(filename.clone(), &src)]))
.unwrap()
});
}
}

Ok(())
}

#[derive(FromArgs)]
/// Huff Language Compiler
struct Cli {
#[argh(subcommand)]
command: Commands,
}

#[derive(FromArgs)]
#[argh(subcommand)]
enum Commands {
Build(BuildCommand),
}

#[derive(FromArgs)]
#[argh(subcommand, name = "build")]
/// Build compiles a huff file.
struct BuildCommand {
/// filename
#[argh(positional)]
filename: String,
Expand Down