Réorganisation avant l'ajout de l'interpreteur.
This commit is contained in:
2
src/interpreter.rs
Normal file
2
src/interpreter.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub struct Interpreter;
|
||||||
|
|
||||||
60
src/main.rs
60
src/main.rs
@@ -1,8 +1,11 @@
|
|||||||
use std::env;
|
use std::{env, fs, io};
|
||||||
|
use std::io::Write;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
use crate::astprinter::ASTPrinter;
|
||||||
|
use crate::expr::ExprVisitor;
|
||||||
|
use crate::parser::Parser;
|
||||||
|
|
||||||
mod rlox_interpreter;
|
mod interpreter;
|
||||||
|
|
||||||
mod scanner;
|
mod scanner;
|
||||||
mod token;
|
mod token;
|
||||||
mod token_type;
|
mod token_type;
|
||||||
@@ -10,7 +13,7 @@ mod expr;
|
|||||||
mod astprinter;
|
mod astprinter;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
use crate::rlox_interpreter::RLoxInterpreter;
|
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 +24,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 => { RLoxInterpreter::new().run_prompt() } ,
|
1 => { run_prompt() } ,
|
||||||
2 => { RLoxInterpreter::new().run_file(&args[1]) },
|
2 => { run_file(&args[1]) },
|
||||||
_ => {
|
_ => {
|
||||||
println!("Usage : rlox [script]");
|
println!("Usage : rlox [script]");
|
||||||
EX_USAGE
|
EX_USAGE
|
||||||
@@ -32,6 +35,51 @@ fn main() {
|
|||||||
process::exit(exit_code);
|
process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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(src: String) -> i32 {
|
||||||
|
let mut scanner = Scanner::new( src );
|
||||||
|
scanner.scan_tokens();
|
||||||
|
|
||||||
|
let mut parser = Parser::new( scanner.tokens );
|
||||||
|
match parser.parse() {
|
||||||
|
Some(expr) => {
|
||||||
|
let mut printer = ASTPrinter { depth: 0 };
|
||||||
|
printer.visit_expr(&expr);
|
||||||
|
println!();
|
||||||
|
},
|
||||||
|
None => println!("An error occurred while parsing expression.")
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
use std::{fs, io};
|
|
||||||
use crate::EX_OK;
|
|
||||||
use std::io::Write;
|
|
||||||
use crate::astprinter::ASTPrinter;
|
|
||||||
use crate::expr::ExprVisitor;
|
|
||||||
use crate::parser::Parser;
|
|
||||||
|
|
||||||
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 parser = Parser::new( scanner.tokens );
|
|
||||||
match parser.parse() {
|
|
||||||
Some(expr) => {
|
|
||||||
let mut printer = ASTPrinter { depth: 0 };
|
|
||||||
printer.visit_expr(&expr);
|
|
||||||
println!();
|
|
||||||
},
|
|
||||||
None => println!("An error occurred while parsing expression.")
|
|
||||||
}
|
|
||||||
|
|
||||||
EX_OK
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user