Ajout de tests unitaires et réorganisation des fichiers

This commit is contained in:
2024-04-22 08:31:25 +02:00
parent f6091f3d2a
commit 5d5928d4ef
9 changed files with 219 additions and 66 deletions

56
src/rlox_interpreter.rs Normal file
View File

@@ -0,0 +1,56 @@
use std::{fs, io};
use crate::EX_OK;
use std::io::Write;
use crate::scanner::Scanner;
pub struct RLoxInterpreter;
impl RLoxInterpreter {
pub fn new() -> Self {
RLoxInterpreter
}
pub fn run_file(&self, file_path: &str ) -> i32 {
let contents = fs::read_to_string(file_path)
.expect(&format!("Should have been able to read the file {file_path}"));
self.run(contents)
}
pub fn run_prompt(&self) -> i32 {
let mut exit_code = EX_OK;
loop {
print!("> ");
io::stdout().flush().expect("Unable to flush stdout");
let mut line = String::new();
io::stdin()
.read_line(&mut line)
.expect("Failed to read line");
if line.trim().is_empty() {
break;
}
exit_code = self.run(line);
}
exit_code
}
fn run(&self, src: String) -> i32 {
let mut scanner = Scanner::new( src );
scanner.scan_tokens();
let mut current_line = 0;
for t in scanner.tokens {
if t.line!=current_line {
current_line = t.line;
println!("-- line {} --------------------", current_line);
}
println!("{}\t{}\t{}", t.token_type, t.lexeme, t.literal);
}
EX_OK
}
}