Interpreter - Fixed silly english/french confusion
This commit is contained in:
parent
3750b5226b
commit
6a227c96ee
1 changed files with 25 additions and 25 deletions
|
@ -10,7 +10,7 @@ use crate::{
|
||||||
/// Evaluate an interpretable, returning its value.
|
/// Evaluate an interpretable, returning its value.
|
||||||
pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option<Value> {
|
pub fn evaluate(err_hdl: &mut ErrorHandler, ast: &dyn Interpretable) -> Option<Value> {
|
||||||
let env = Rc::new(RefCell::new(Environment::default()));
|
let env = Rc::new(RefCell::new(Environment::default()));
|
||||||
match ast.interprete(&env) {
|
match ast.interpret(&env) {
|
||||||
Ok(v) => Some(v.result()),
|
Ok(v) => Some(v.result()),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
e.report(err_hdl);
|
e.report(err_hdl);
|
||||||
|
@ -66,7 +66,7 @@ pub type InterpreterResult = Result<InterpreterFlowControl, InterpreterError>;
|
||||||
|
|
||||||
/// An Interpretable can be evaluated and will return a value.
|
/// An Interpretable can be evaluated and will return a value.
|
||||||
pub trait Interpretable {
|
pub trait Interpretable {
|
||||||
fn interprete(&self, environment: &EnvironmentRef) -> InterpreterResult;
|
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------- *
|
/* ----------------------------- *
|
||||||
|
@ -74,9 +74,9 @@ pub trait Interpretable {
|
||||||
* ----------------------------- */
|
* ----------------------------- */
|
||||||
|
|
||||||
impl Interpretable for ast::ProgramNode {
|
impl Interpretable for ast::ProgramNode {
|
||||||
fn interprete(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
||||||
for stmt in self.0.iter() {
|
for stmt in self.0.iter() {
|
||||||
stmt.interprete(environment)?;
|
stmt.interpret(environment)?;
|
||||||
}
|
}
|
||||||
Ok(InterpreterFlowControl::default())
|
Ok(InterpreterFlowControl::default())
|
||||||
}
|
}
|
||||||
|
@ -87,9 +87,9 @@ impl Interpretable for ast::ProgramNode {
|
||||||
* ------------------------------- */
|
* ------------------------------- */
|
||||||
|
|
||||||
impl Interpretable for ast::StmtNode {
|
impl Interpretable for ast::StmtNode {
|
||||||
fn interprete(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
||||||
match self {
|
match self {
|
||||||
ast::StmtNode::Expression(expr) => expr.interprete(environment),
|
ast::StmtNode::Expression(expr) => expr.interpret(environment),
|
||||||
ast::StmtNode::Print(expr) => self.on_print(environment, expr),
|
ast::StmtNode::Print(expr) => self.on_print(environment, expr),
|
||||||
ast::StmtNode::VarDecl(name, expr) => self.on_var_decl(environment, name, expr),
|
ast::StmtNode::VarDecl(name, expr) => self.on_var_decl(environment, name, expr),
|
||||||
ast::StmtNode::Block(statements) => self.on_block(environment, statements),
|
ast::StmtNode::Block(statements) => self.on_block(environment, statements),
|
||||||
|
@ -115,7 +115,7 @@ impl Interpretable for ast::StmtNode {
|
||||||
impl ast::StmtNode {
|
impl ast::StmtNode {
|
||||||
/// Handle the `print` statement.
|
/// Handle the `print` statement.
|
||||||
fn on_print(&self, environment: &EnvironmentRef, expr: &ast::ExprNode) -> InterpreterResult {
|
fn on_print(&self, environment: &EnvironmentRef, expr: &ast::ExprNode) -> InterpreterResult {
|
||||||
let value = expr.interprete(environment)?.result();
|
let value = expr.interpret(environment)?.result();
|
||||||
let output = match value {
|
let output = match value {
|
||||||
Value::Nil => String::from("nil"),
|
Value::Nil => String::from("nil"),
|
||||||
Value::Boolean(true) => String::from("true"),
|
Value::Boolean(true) => String::from("true"),
|
||||||
|
@ -136,7 +136,7 @@ impl ast::StmtNode {
|
||||||
initializer: &Option<ast::ExprNode>,
|
initializer: &Option<ast::ExprNode>,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let variable = match initializer {
|
let variable = match initializer {
|
||||||
Some(expr) => Some(expr.interprete(environment)?.result()),
|
Some(expr) => Some(expr.interpret(environment)?.result()),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
environment.borrow_mut().define(name, variable)?;
|
environment.borrow_mut().define(name, variable)?;
|
||||||
|
@ -147,7 +147,7 @@ impl ast::StmtNode {
|
||||||
fn on_block(&self, environment: &EnvironmentRef, stmts: &[ast::StmtNode]) -> InterpreterResult {
|
fn on_block(&self, environment: &EnvironmentRef, stmts: &[ast::StmtNode]) -> InterpreterResult {
|
||||||
let child = Environment::create_child(environment);
|
let child = Environment::create_child(environment);
|
||||||
for stmt in stmts.iter() {
|
for stmt in stmts.iter() {
|
||||||
let result = stmt.interprete(&child)?;
|
let result = stmt.interpret(&child)?;
|
||||||
if result.is_flow_control() {
|
if result.is_flow_control() {
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
@ -163,10 +163,10 @@ impl ast::StmtNode {
|
||||||
then_branch: &ast::StmtNode,
|
then_branch: &ast::StmtNode,
|
||||||
else_branch: &Option<Box<ast::StmtNode>>,
|
else_branch: &Option<Box<ast::StmtNode>>,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
if condition.interprete(environment)?.result().is_truthy() {
|
if condition.interpret(environment)?.result().is_truthy() {
|
||||||
then_branch.interprete(environment)
|
then_branch.interpret(environment)
|
||||||
} else if let Some(else_stmt) = else_branch {
|
} else if let Some(else_stmt) = else_branch {
|
||||||
else_stmt.interprete(environment)
|
else_stmt.interpret(environment)
|
||||||
} else {
|
} else {
|
||||||
Ok(InterpreterFlowControl::default())
|
Ok(InterpreterFlowControl::default())
|
||||||
}
|
}
|
||||||
|
@ -182,8 +182,8 @@ impl ast::StmtNode {
|
||||||
after_body: &Option<Box<ast::StmtNode>>,
|
after_body: &Option<Box<ast::StmtNode>>,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let ln = label.as_ref().map(|token| token.lexeme.clone());
|
let ln = label.as_ref().map(|token| token.lexeme.clone());
|
||||||
while condition.interprete(environment)?.result().is_truthy() {
|
while condition.interpret(environment)?.result().is_truthy() {
|
||||||
let result = body.interprete(environment)?;
|
let result = body.interpret(environment)?;
|
||||||
match &result {
|
match &result {
|
||||||
InterpreterFlowControl::Result(_) => (),
|
InterpreterFlowControl::Result(_) => (),
|
||||||
InterpreterFlowControl::Continue(lv) if lv == &ln => (),
|
InterpreterFlowControl::Continue(lv) if lv == &ln => (),
|
||||||
|
@ -191,7 +191,7 @@ impl ast::StmtNode {
|
||||||
_ => return Ok(result),
|
_ => return Ok(result),
|
||||||
}
|
}
|
||||||
if let Some(stmt) = after_body {
|
if let Some(stmt) = after_body {
|
||||||
let result = stmt.interprete(environment)?;
|
let result = stmt.interpret(environment)?;
|
||||||
match &result {
|
match &result {
|
||||||
InterpreterFlowControl::Result(_) => (),
|
InterpreterFlowControl::Result(_) => (),
|
||||||
InterpreterFlowControl::Continue(lv) if lv == &ln => (),
|
InterpreterFlowControl::Continue(lv) if lv == &ln => (),
|
||||||
|
@ -223,10 +223,10 @@ impl ast::StmtNode {
|
||||||
* -------------------------------- */
|
* -------------------------------- */
|
||||||
|
|
||||||
impl Interpretable for ast::ExprNode {
|
impl Interpretable for ast::ExprNode {
|
||||||
fn interprete(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
fn interpret(&self, environment: &EnvironmentRef) -> InterpreterResult {
|
||||||
match self {
|
match self {
|
||||||
ast::ExprNode::Assignment { name, value } => {
|
ast::ExprNode::Assignment { name, value } => {
|
||||||
let value = value.interprete(environment)?.result();
|
let value = value.interpret(environment)?.result();
|
||||||
environment.borrow_mut().assign(name, value)?;
|
environment.borrow_mut().assign(name, value)?;
|
||||||
Ok(InterpreterFlowControl::default())
|
Ok(InterpreterFlowControl::default())
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ impl Interpretable for ast::ExprNode {
|
||||||
right,
|
right,
|
||||||
} => self.on_binary(environment, left, operator, right),
|
} => self.on_binary(environment, left, operator, right),
|
||||||
ast::ExprNode::Unary { operator, right } => self.on_unary(environment, operator, right),
|
ast::ExprNode::Unary { operator, right } => self.on_unary(environment, operator, right),
|
||||||
ast::ExprNode::Grouping { expression } => expression.interprete(environment),
|
ast::ExprNode::Grouping { expression } => expression.interpret(environment),
|
||||||
ast::ExprNode::Litteral { value } => self.on_litteral(value),
|
ast::ExprNode::Litteral { value } => self.on_litteral(value),
|
||||||
ast::ExprNode::Variable { name } => Ok(environment.borrow().get(name)?.into()),
|
ast::ExprNode::Variable { name } => Ok(environment.borrow().get(name)?.into()),
|
||||||
ast::ExprNode::Call {
|
ast::ExprNode::Call {
|
||||||
|
@ -262,13 +262,13 @@ impl ast::ExprNode {
|
||||||
operator: &Token,
|
operator: &Token,
|
||||||
right: &ast::ExprNode,
|
right: &ast::ExprNode,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let left_value = left.interprete(environment)?.result();
|
let left_value = left.interpret(environment)?.result();
|
||||||
if operator.token_type == TokenType::Or && left_value.is_truthy()
|
if operator.token_type == TokenType::Or && left_value.is_truthy()
|
||||||
|| operator.token_type == TokenType::And && !left_value.is_truthy()
|
|| operator.token_type == TokenType::And && !left_value.is_truthy()
|
||||||
{
|
{
|
||||||
Ok(left_value.into())
|
Ok(left_value.into())
|
||||||
} else {
|
} else {
|
||||||
right.interprete(environment)
|
right.interpret(environment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,8 +280,8 @@ impl ast::ExprNode {
|
||||||
operator: &Token,
|
operator: &Token,
|
||||||
right: &ast::ExprNode,
|
right: &ast::ExprNode,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let left_value = left.interprete(environment)?.result();
|
let left_value = left.interpret(environment)?.result();
|
||||||
let right_value = right.interprete(environment)?.result();
|
let right_value = right.interpret(environment)?.result();
|
||||||
match operator.token_type {
|
match operator.token_type {
|
||||||
TokenType::Plus => match (left_value, right_value) {
|
TokenType::Plus => match (left_value, right_value) {
|
||||||
(Value::Number(a), Value::Number(b)) => Ok(Value::Number(a + b).into()),
|
(Value::Number(a), Value::Number(b)) => Ok(Value::Number(a + b).into()),
|
||||||
|
@ -350,7 +350,7 @@ impl ast::ExprNode {
|
||||||
operator: &Token,
|
operator: &Token,
|
||||||
right: &ast::ExprNode,
|
right: &ast::ExprNode,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let right_value = right.interprete(environment)?.result();
|
let right_value = right.interpret(environment)?.result();
|
||||||
match operator.token_type {
|
match operator.token_type {
|
||||||
TokenType::Minus => {
|
TokenType::Minus => {
|
||||||
if let Value::Number(n) = right_value {
|
if let Value::Number(n) = right_value {
|
||||||
|
@ -390,11 +390,11 @@ impl ast::ExprNode {
|
||||||
right_paren: &Token,
|
right_paren: &Token,
|
||||||
arguments: &Vec<ast::ExprNode>,
|
arguments: &Vec<ast::ExprNode>,
|
||||||
) -> InterpreterResult {
|
) -> InterpreterResult {
|
||||||
let callee = callee.interprete(environment)?.result();
|
let callee = callee.interpret(environment)?.result();
|
||||||
let arg_values = {
|
let arg_values = {
|
||||||
let mut v = Vec::with_capacity(arguments.len());
|
let mut v = Vec::with_capacity(arguments.len());
|
||||||
for argument in arguments.iter() {
|
for argument in arguments.iter() {
|
||||||
v.push(argument.interprete(environment)?.result());
|
v.push(argument.interpret(environment)?.result());
|
||||||
}
|
}
|
||||||
v
|
v
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue