Séparation dans un module indépendant
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -1,16 +1,9 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
|
||||||
use std::io;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
mod rlox;
|
mod rlox;
|
||||||
mod scanner;
|
use crate::rlox::rlox_interpreter::RLoxInterpreter;
|
||||||
mod token_type;
|
|
||||||
mod token;
|
|
||||||
|
|
||||||
use crate::rlox::RLox;
|
|
||||||
use crate::scanner::Scanner;
|
|
||||||
|
|
||||||
// Exit codes from #include <sysexits.h>
|
// Exit codes from #include <sysexits.h>
|
||||||
const EX_OK: i32 = 0;
|
const EX_OK: i32 = 0;
|
||||||
@@ -21,8 +14,8 @@ fn main() {
|
|||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
let exit_code = match args.len() {
|
let exit_code = match args.len() {
|
||||||
1 => run_prompt(),
|
1 => { RLoxInterpreter::new().run_prompt() } ,
|
||||||
2 => run_file(&args[1]),
|
2 => { RLoxInterpreter::new().run_file(&args[1]) },
|
||||||
_ => {
|
_ => {
|
||||||
println!("Usage : rlox [script]");
|
println!("Usage : rlox [script]");
|
||||||
EX_USAGE
|
EX_USAGE
|
||||||
@@ -32,37 +25,6 @@ fn main() {
|
|||||||
process::exit(exit_code);
|
process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_prompt() -> 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 = run(line);
|
|
||||||
}
|
|
||||||
exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run( script: String ) -> i32 {
|
|
||||||
let rlox_interpreter = RLox { had_error: false };
|
|
||||||
if rlox_interpreter.had_error { EX_DATAERR } else { EX_OK }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implémentations de référence :
|
// Implémentations de référence :
|
||||||
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
||||||
|
|||||||
21
src/rlox.rs
21
src/rlox.rs
@@ -1,21 +0,0 @@
|
|||||||
use crate::scanner::Scanner;
|
|
||||||
|
|
||||||
pub struct RLox {
|
|
||||||
pub had_error: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RLox {
|
|
||||||
fn error(&self, line: u32, message: String) {
|
|
||||||
self.report(line, String::from(""), message);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn report(&self, line: u32, place: String, message: String) {
|
|
||||||
println!("[line {line}] Error {place}: {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(&self, src: String) {
|
|
||||||
let mut scanner = Scanner::create_scanner( src );
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
4
src/rlox/mod.rs
Normal file
4
src/rlox/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod rlox_interpreter;
|
||||||
|
pub mod scanner;
|
||||||
|
pub mod token;
|
||||||
|
pub mod token_type;
|
||||||
57
src/rlox/rlox_interpreter.rs
Normal file
57
src/rlox/rlox_interpreter.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
use std::{fs, io};
|
||||||
|
use crate::{EX_DATAERR, EX_OK};
|
||||||
|
use crate::scanner::Scanner;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub struct RLoxInterpreter {
|
||||||
|
pub had_error: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RLoxInterpreter {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
RLoxInterpreter {
|
||||||
|
had_error: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error(&self, line: u32, message: String) {
|
||||||
|
self.report(line, String::from(""), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn report(&self, line: u32, place: String, message: String) {
|
||||||
|
println!("[line {line}] Error {place}: {message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
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::create_scanner( src );
|
||||||
|
let rlox_interpreter = RLoxInterpreter { had_error: false };
|
||||||
|
if rlox_interpreter.had_error { EX_DATAERR } else { EX_OK }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user