Finalisation de l'interpreteur d'expressions.
This commit is contained in:
25
src/main.rs
25
src/main.rs
@@ -2,7 +2,8 @@ use std::{env, fs, io};
|
||||
use std::io::Write;
|
||||
use std::process;
|
||||
use crate::astprinter::ASTPrinter;
|
||||
use crate::expr::ExprVisitor;
|
||||
use crate::expr::{ExprVisitor, Literal};
|
||||
use crate::interpreter::Interpreter;
|
||||
use crate::parser::Parser;
|
||||
|
||||
mod interpreter;
|
||||
@@ -17,8 +18,9 @@ use crate::scanner::Scanner;
|
||||
|
||||
// Exit codes from #include <sysexits.h>
|
||||
const EX_OK: i32 = 0;
|
||||
//const EX_DATAERR: i32 = 65;
|
||||
const EX_DATAERR: i32 = 65;
|
||||
const EX_USAGE : i32 = 66;
|
||||
const EX_EXECERR: i32 = 70;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
@@ -63,17 +65,32 @@ pub fn run_prompt() -> i32 {
|
||||
}
|
||||
|
||||
fn run(src: String) -> i32 {
|
||||
println!("Scanner");
|
||||
let mut scanner = Scanner::new( src );
|
||||
scanner.scan_tokens();
|
||||
|
||||
println!("Parser");
|
||||
let mut parser = Parser::new( scanner.tokens );
|
||||
match parser.parse() {
|
||||
Some(expr) => {
|
||||
println!("AST");
|
||||
let mut printer = ASTPrinter { depth: 0 };
|
||||
printer.visit_expr(&expr);
|
||||
println!("Interpretation");
|
||||
let mut interpreter = Interpreter;
|
||||
let result = interpreter.visit_expr(&expr);
|
||||
match result {
|
||||
Ok(r) => { match r {
|
||||
Literal::LiteralNumber(f) => { println!("Resultat numérique = {}", f); }
|
||||
Literal::LiteralString(s) => { println!("Résultat chaîne = {}", s); }
|
||||
Literal::LiteralBool(b) => { println!("Résultat booléen = {}", b); }
|
||||
Literal::LiteralNil => { println!("Résultat nul")}
|
||||
}}
|
||||
Err(e) => { println!("Erreur d'exécution : {}", e.message); return EX_EXECERR; }
|
||||
}
|
||||
println!();
|
||||
},
|
||||
None => println!("An error occurred while parsing expression.")
|
||||
None => { println!("An error occurred while parsing expression."); return EX_DATAERR; }
|
||||
}
|
||||
|
||||
EX_OK
|
||||
@@ -84,4 +101,4 @@ fn run(src: String) -> i32 {
|
||||
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
|
||||
|
||||
// Pause :
|
||||
// http://www.craftinginterpreters.com/parsing-expressions.html implémentation de comparison
|
||||
// http://www.craftinginterpreters.com/statements-and-state.html
|
||||
|
||||
Reference in New Issue
Block a user