Séparation de RLox dans un sous-module indépendant.

This commit is contained in:
2024-03-29 07:56:40 +01:00
parent 873c1261d3
commit 0fcd4d09f1
2 changed files with 40 additions and 8 deletions

View File

@@ -2,27 +2,40 @@ use std::env;
use std::fs;
use std::io;
use std::io::Write;
use crate::rlox::RLox;
use std::process;
pub mod rlox;
// Exit codes from #include <sysexits.h>
const EX_OK: i32 = 0;
const EX_DATAERR: i32 = 65;
const EX_USAGE : i32 = 66;
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
let exit_code = match args.len() {
1 => run_prompt(),
2 => run_file(&args[1]),
_ => {
println!("Usage : rlox [script]");
EX_USAGE
}
}
};
process::exit(exit_code);
}
fn run_file( file_path: &str ) {
fn run_file( file_path: &str ) -> i32 {
let contents = fs::read_to_string(file_path)
.expect(&format!("Should have been able to read the file {file_path}"));
run(contents);
run(contents)
}
fn run_prompt() {
fn run_prompt() -> i32 {
let mut exit_code = EX_OK;
loop {
print!("> ");
io::stdout().flush().expect("Unable to flush stdout");
@@ -36,12 +49,15 @@ fn run_prompt() {
break;
}
run(line);
exit_code = run(line);
}
exit_code
}
fn run( _script: String ) {
fn run( script: String ) -> i32 {
let rlox_interpreter = RLox { had_error: false };
if rlox_interpreter.had_error { EX_DATAERR } else { EX_OK }
}
// http://www.craftinginterpreters.com/scanning.html#error-handling