use std::path::Path;
use std::process::ExitCode;
use nybl::{NyblHost, NyblLimits};
use nybl_sys::StdHost;
pub fn run_file(path: &str, no_vm: bool) -> ExitCode {
let source = match std::fs::read_to_string(path) {
Ok(s) => s,
Err(e) => {
eprintln!("error reading `{path}`: {e}");
return ExitCode::from(1);
}
};
let module_root = crate::entry_path::module_root(Path::new(path));
let mut check_host = StdHost::new().with_module_root(&module_root);
let resolver = |path: &str| check_host.resolve_module(path);
match nybl::parse_with_warnings_and_resolver(&source, resolver) {
Ok((_stmts, warnings)) => {
for w in &warnings {
eprint!("{}", w.render(&source));
}
}
Err(e) => {
eprint!("{}", e.render(&source));
return ExitCode::from(1);
}
}
let mut host = StdHost::new().with_module_root(module_root);
let result = if no_vm {
nybl::run(&source, &mut host, &NyblLimits::standard())
} else {
nybl_vm::run(&source, &mut host, &NyblLimits::standard())
};
if host.is_broken_pipe() {
return ExitCode::SUCCESS;
}
match result {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprint!("{}", e.render(&source));
ExitCode::from(1)
}
}
}