able-script/ablescript_cli/src/main.rs

66 lines
1.9 KiB
Rust
Raw Normal View History

2021-06-16 10:36:52 -05:00
#![forbid(unsafe_code, clippy::unwrap_used)]
2021-04-11 17:22:06 -05:00
2022-04-12 07:16:34 -05:00
mod repl;
use ablescript::interpret::ExecEnv;
use ablescript::parser::Parser;
2022-04-12 07:16:34 -05:00
use clap::{Arg, Command};
use std::process::exit;
2021-05-03 19:33:21 -05:00
fn main() {
2021-05-05 07:33:40 -05:00
// variables::test(); // NOTE(Able): Add this as a test case
2022-04-12 07:16:34 -05:00
let matches = Command::new("AbleScript")
2021-04-11 14:11:39 -05:00
.version(env!("CARGO_PKG_VERSION"))
2021-04-11 11:47:35 -05:00
.author("Able <abl3theabove@gmail.com>")
.about("AbleScript interpreter")
2021-04-11 11:47:35 -05:00
.arg(
2022-04-12 07:16:34 -05:00
Arg::new("file")
.short('f')
2021-04-11 11:47:35 -05:00
.long("file")
.value_name("FILE")
2021-04-11 15:11:23 -05:00
.help("Set the path to interpret from")
2021-04-11 11:47:35 -05:00
.takes_value(true),
)
2021-06-16 10:29:27 -05:00
.arg(
2022-04-12 07:16:34 -05:00
Arg::new("debug")
2021-06-16 10:29:27 -05:00
.long("debug")
.help("Enable debug AST printing"),
)
2021-04-11 11:47:35 -05:00
.get_matches();
2021-04-13 18:01:19 -05:00
2021-06-16 10:29:27 -05:00
let ast_print = matches.is_present("debug");
2021-04-11 11:47:35 -05:00
match matches.value_of("file") {
Some(file_path) => {
// Read file
let source = match std::fs::read_to_string(file_path) {
Ok(s) => s,
Err(e) => {
println!("Failed to read file \"{}\": {}", file_path, e);
exit(1)
}
};
// Parse & evaluate
let mut parser = Parser::new(&source);
2021-06-16 10:29:27 -05:00
if let Err(e) = parser.init().and_then(|ast| {
if ast_print {
println!("{:#?}", ast);
}
ExecEnv::new().eval_stmts(&ast)
}) {
println!(
"Error `{:?}` occurred at span: {:?} = `{:?}`",
e.kind,
e.span.clone(),
&source[e.span]
);
}
2021-04-11 11:47:35 -05:00
}
None => {
println!("Hi [AbleScript {}]", env!("CARGO_PKG_VERSION"));
2021-06-16 10:29:27 -05:00
repl::repl(ast_print);
2021-04-11 11:47:35 -05:00
}
}
}