mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
79 lines
2.4 KiB
Rust
79 lines
2.4 KiB
Rust
use clap::{ArgAction, Parser};
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct CliArgs {
|
|
#[arg()]
|
|
file_name: Option<String>,
|
|
|
|
#[arg(short, action = ArgAction::SetTrue)]
|
|
interactive: bool,
|
|
|
|
#[arg(long, action = ArgAction::SetTrue)]
|
|
vm: bool,
|
|
}
|
|
|
|
/*
|
|
let source_code = std::fs::read_to_string(script_path).unwrap_or_else(|err| {
|
|
eprintln!("Reading script file {script_path} failed: {err}");
|
|
std::process::exit(66);
|
|
}); */
|
|
|
|
fn main() {
|
|
let cli_args = CliArgs::parse();
|
|
|
|
if cli_args.vm {
|
|
use rlox2_vm::LoxError;
|
|
|
|
let mut vm = rlox2_vm::VM::default();
|
|
|
|
if let Some(file_name) = cli_args.file_name {
|
|
let source = std::fs::read_to_string(&file_name).unwrap_or_else(|err| {
|
|
eprintln!("Reading script file {file_name} failed: {err}");
|
|
std::process::exit(66);
|
|
});
|
|
|
|
if let Err(err) = rlox2_vm::run(&source, &mut vm) {
|
|
eprintln!("{err}");
|
|
match err {
|
|
LoxError::LexerError { .. } | LoxError::CompileError { .. } => std::process::exit(65),
|
|
LoxError::RuntimeError { .. } => std::process::exit(70),
|
|
LoxError::Exit { exit_code } => std::process::exit(exit_code),
|
|
}
|
|
}
|
|
|
|
if !cli_args.interactive {
|
|
return;
|
|
}
|
|
}
|
|
|
|
rlox2_vm::run_repl(&mut vm);
|
|
} else {
|
|
use rlox2_interpreter::LoxError;
|
|
|
|
let mut runtime = rlox2_interpreter::Runtime::default();
|
|
|
|
if let Some(file_name) = cli_args.file_name {
|
|
let source = std::fs::read_to_string(&file_name).unwrap_or_else(|err| {
|
|
eprintln!("Reading script file {file_name} failed: {err}");
|
|
std::process::exit(66);
|
|
});
|
|
|
|
if let Err(err) = rlox2_interpreter::run(&source, &mut runtime) {
|
|
eprintln!("{err}");
|
|
match err {
|
|
LoxError::LexerError { .. } | LoxError::ParserError { .. } | LoxError::ResolverError { .. } => {
|
|
std::process::exit(65)
|
|
}
|
|
LoxError::RuntimeError { .. } => std::process::exit(70),
|
|
LoxError::Exit { exit_code } => std::process::exit(exit_code),
|
|
}
|
|
}
|
|
|
|
if !cli_args.interactive {
|
|
return;
|
|
}
|
|
}
|
|
|
|
rlox2_interpreter::run_repl(&mut runtime);
|
|
}
|
|
}
|