Finished up to and including chapter 16

This commit is contained in:
Moritz Gmeiner 2023-01-30 17:41:48 +01:00
commit b86985deaf
24 changed files with 1042 additions and 189 deletions

View file

@ -1,5 +1,4 @@
use clap::{ArgAction, Parser};
use rlox2_interpreter::{run_file, run_repl, Runtime};
#[derive(Parser, Debug)]
struct CliArgs {
@ -8,24 +7,73 @@ struct CliArgs {
#[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() {
interpreter_main();
}
pub fn interpreter_main() {
let cli_args = CliArgs::parse();
let mut runtime = Runtime::default();
if cli_args.vm {
use rlox2_vm::InterpretError;
if let Some(file_name) = cli_args.file_name {
run_file(&mut runtime, &file_name);
let mut vm = rlox2_vm::VM::default();
if cli_args.interactive {
run_repl(&mut runtime);
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 {
InterpretError::LexerError { .. } | InterpretError::CompileError { .. } => std::process::exit(65),
InterpretError::RuntimeError { .. } => std::process::exit(70),
InterpretError::Exit { exit_code } => std::process::exit(exit_code),
}
}
if !cli_args.interactive {
return;
}
}
rlox2_vm::run_repl(&mut vm);
} else {
run_repl(&mut runtime);
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);
}
}