ablescript/ablescript_cli/src/main.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2023-05-18 18:33:58 +00:00
#![forbid(unsafe_code)]
2021-04-11 22:22:06 +00:00
2022-04-12 12:16:34 +00:00
mod repl;
2023-05-18 18:33:58 +00:00
use ablescript::{interpret::ExecEnv, parser::parse};
use clap::Parser;
use std::{path::PathBuf, process::exit};
2021-05-04 00:33:21 +00:00
fn main() {
2021-05-05 12:33:40 +00:00
// variables::test(); // NOTE(Able): Add this as a test case
2023-05-18 18:33:58 +00:00
let args = Args::parse();
match args.file {
2021-04-11 16:47:35 +00:00
Some(file_path) => {
// Read file
2023-05-18 18:33:58 +00:00
let source = match std::fs::read_to_string(&file_path) {
Ok(s) => s,
Err(e) => {
2023-05-18 18:33:58 +00:00
println!("Failed to read file \"{:?}\": {}", file_path, e);
exit(1)
}
};
// Parse & evaluate
if let Err(e) = parse(&source).and_then(|ast| {
2023-05-18 18:33:58 +00:00
if args.debug {
eprintln!("{:#?}", ast);
2021-06-16 15:29:27 +00:00
}
2022-07-01 22:17:29 +00:00
ExecEnv::<ablescript::host_interface::Standard>::default().eval_stmts(&ast)
2021-06-16 15:29:27 +00:00
}) {
println!(
"Error `{:?}` occurred at span: {:?} = `{:?}`",
e.kind,
e.span.clone(),
&source[e.span]
);
}
2021-04-11 16:47:35 +00:00
}
None => {
println!("Hi [AbleScript {}]", env!("CARGO_PKG_VERSION"));
2023-05-18 18:33:58 +00:00
repl::repl(args.debug);
2021-04-11 16:47:35 +00:00
}
}
}
2023-05-18 18:33:58 +00:00
#[derive(Parser, Debug)]
#[command(name = "AbleScript", version, about)]
struct Args {
/// File to execute
#[arg(short, long)]
file: Option<PathBuf>,
/// Dump AST to console
#[arg(short, long)]
debug: bool,
}