adding better api

This commit is contained in:
mlokr 2024-07-06 14:58:50 +02:00
parent 91907a90ff
commit dc0562553d
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
2 changed files with 65 additions and 45 deletions

View file

@ -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;

View file

@ -1,7 +1,6 @@
fn main() -> std::io::Result<()> {
let args = std::env::args().collect::<Vec<_>>();
let args = args.iter().map(String::as_str).collect::<Vec<_>>();
let root = args.get(1).copied().unwrap_or("main.hb");
if args.contains(&"--help") || args.contains(&"-h") {
println!("Usage: hbc [OPTIONS...] <FILE>");
@ -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(),
)
}