Interpreter - Handle return value in functions

This commit is contained in:
Emmanuel BENOîT 2023-01-02 20:46:34 +01:00
parent 0de6b927b5
commit d6ea1c3099

View file

@ -5,7 +5,7 @@ use itertools::izip;
use crate::{ use crate::{
ast, ast,
errors::InterpreterError, errors::InterpreterError,
interpreter::{Environment, Interpretable}, interpreter::{Environment, Interpretable, InterpreterFlowControl},
tokens::Token, tokens::Token,
}; };
@ -54,8 +54,10 @@ impl Callable for Function {
let child = Environment::create_child(&param_env); let child = Environment::create_child(&param_env);
for stmt in self.body.iter() { for stmt in self.body.iter() {
let result = stmt.interpret(&child)?; let result = stmt.interpret(&child)?;
if result.is_flow_control() { match result {
panic!("unexpected flow control"); InterpreterFlowControl::Result(_) => (),
InterpreterFlowControl::Return(v) => return Ok(v),
_ => panic!("unexpected flow control {:?}", result),
} }
} }
Ok(Value::Nil) Ok(Value::Nil)