1use std::path::Path;
4use std::process::{self, ExitCode};
5use std::rc::Rc;
6
7use nifty_interp::RuntimeError;
8
9use crate::frontend::Sources;
10use crate::{CommandResult, EXIT_HAS_ERRORS, report};
11
12pub(crate) fn run(target: &Path) -> CommandResult {
13 let sources = Rc::new(Sources::read(target)?);
14 let program = match sources.compile() {
15 Ok(program) => program,
16 Err(diagnostics) => {
17 report::print(&sources, &diagnostics)?;
18 return Ok(ExitCode::from(EXIT_HAS_ERRORS));
19 }
20 };
21
22 let on_error = Rc::new({
24 let sources = Rc::clone(&sources);
25 move |error: RuntimeError| {
26 report::runtime_error(&sources, &error);
27 process::exit(i32::from(EXIT_HAS_ERRORS));
28 }
29 });
30 match nifty_interp::run(program, nifty_platform::platform_backend(), on_error) {
31 Ok(()) => Ok(ExitCode::SUCCESS),
32 Err(error) => {
33 report::runtime_error(&sources, &error);
34 Ok(ExitCode::from(EXIT_HAS_ERRORS))
35 }
36 }
37}