Interpreter - Handle return value in functions
This commit is contained in:
parent
0de6b927b5
commit
d6ea1c3099
1 changed files with 5 additions and 3 deletions
|
@ -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(¶m_env);
|
let child = Environment::create_child(¶m_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)
|
||||||
|
|
Loading…
Reference in a new issue