diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index 44ed514..0538449 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -553,6 +553,63 @@ pub fn run_test( panic!("test failed"); } +#[derive(Default)] +pub struct Options { + pub fmt: bool, + pub fmt_current: bool, +} + +pub fn run_compiler( + root_file: &str, + options: Options, + out: &mut impl std::io::Write, +) -> io::Result<()> { + let parsed = parse_from_fs(1, root_file)?; + + fn format_to_stdout(ast: parser::Ast) -> std::io::Result<()> { + let source = std::fs::read_to_string(&*ast.path)?; + parser::with_fmt_source(&source, || { + for expr in ast.exprs() { + use std::io::Write; + writeln!(std::io::stdout(), "{expr}")?; + } + std::io::Result::Ok(()) + }) + } + + fn format_ast(ast: parser::Ast) -> std::io::Result<()> { + let source = std::fs::read_to_string(&*ast.path)?; + let mut output = Vec::new(); + parser::with_fmt_source(&source, || { + for expr in ast.exprs() { + use std::io::Write; + writeln!(output, "{expr}")?; + } + std::io::Result::Ok(()) + })?; + + std::fs::write(&*ast.path, output)?; + + Ok(()) + } + + if options.fmt { + for parsed in parsed { + format_ast(parsed)?; + } + } else if options.fmt_current { + format_to_stdout(parsed.into_iter().next().unwrap())?; + } else { + let mut codegen = codegen::Codegen::default(); + codegen.files = parsed; + + codegen.generate(); + codegen.dump(out)?; + } + + Ok(()) +} + #[cfg(test)] mod test { use std::sync::Arc; diff --git a/hblang/src/main.rs b/hblang/src/main.rs index 3771633..d4f06bf 100644 --- a/hblang/src/main.rs +++ b/hblang/src/main.rs @@ -1,7 +1,6 @@ fn main() -> std::io::Result<()> { let args = std::env::args().collect::>(); let args = args.iter().map(String::as_str).collect::>(); - let root = args.get(1).copied().unwrap_or("main.hb"); if args.contains(&"--help") || args.contains(&"-h") { println!("Usage: hbc [OPTIONS...] "); @@ -9,48 +8,12 @@ fn main() -> std::io::Result<()> { return Err(std::io::ErrorKind::Other.into()); } - let parsed = hblang::parse_from_fs(1, root)?; - - fn format_to_stdout(ast: hblang::parser::Ast) -> std::io::Result<()> { - let source = std::fs::read_to_string(&*ast.path)?; - hblang::parser::with_fmt_source(&source, || { - for expr in ast.exprs() { - use std::io::Write; - writeln!(std::io::stdout(), "{expr}")?; - } - std::io::Result::Ok(()) - }) - } - - fn format_ast(ast: hblang::parser::Ast) -> std::io::Result<()> { - let source = std::fs::read_to_string(&*ast.path)?; - let mut output = Vec::new(); - hblang::parser::with_fmt_source(&source, || { - for expr in ast.exprs() { - use std::io::Write; - writeln!(output, "{expr}")?; - } - std::io::Result::Ok(()) - })?; - - std::fs::write(&*ast.path, output)?; - - Ok(()) - } - - if args.contains(&"--fmt") { - for parsed in parsed { - format_ast(parsed)?; - } - } else if args.contains(&"--fmt-current") { - format_to_stdout(parsed.into_iter().next().unwrap())?; - } else { - let mut codegen = hblang::codegen::Codegen::default(); - codegen.files = parsed; - - codegen.generate(); - codegen.dump(&mut std::io::stdout())?; - } - - Ok(()) + hblang::run_compiler( + args.get(1).copied().unwrap_or("main.hb"), + hblang::Options { + fmt: args.contains(&"--fmt"), + fmt_current: args.contains(&"--fmt-current"), + }, + &mut std::io::stdout(), + ) }