Hooked up the interpreter

This commit is contained in:
Emmanuel BENOîT 2022-12-31 13:49:01 +01:00
parent 2aa9fb278a
commit b89e330423
2 changed files with 17 additions and 2 deletions

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
ast, ast,
errors::InterpreterError, errors::{ErrorHandler, InterpreterError},
tokens::{Token, TokenType}, tokens::{Token, TokenType},
}; };
@ -31,6 +31,16 @@ pub trait Interpretable {
fn interprete(&self) -> Result<Value, InterpreterError>; fn interprete(&self) -> Result<Value, InterpreterError>;
} }
/// Evaluate an interpretable, returning its value.
pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option<Value> {
match ast.interprete() {
Ok(v) => Some(v),
Err(e) => {
e.report(err_hdl);
None
}
}
}
/* -------------------------------- * /* -------------------------------- *
* INTERPRETER FOR EXPRESSION NODES * * INTERPRETER FOR EXPRESSION NODES *

View file

@ -13,6 +13,7 @@ use std::{
use ast::AstDumper; use ast::AstDumper;
use errors::ErrorHandler; use errors::ErrorHandler;
use interpreter::evaluate;
use parser::Parser; use parser::Parser;
use scanner::Scanner; use scanner::Scanner;
@ -31,7 +32,11 @@ fn run(source: String) -> ErrorHandler {
match parser.parse(&mut error_handler) { match parser.parse(&mut error_handler) {
None => (), None => (),
Some(ast) => { Some(ast) => {
println!("AST generated ! {}", ast.dump()) println!("AST generated ! {}", ast.dump());
match evaluate(&mut error_handler, &ast) {
Some(v) => println!("Final evaluated value: {:?}", v),
None => (),
}
} }
} }