fix: return function errors

This commit is contained in:
Artemy 2022-08-15 18:16:32 +03:00
parent 89f9b31c2b
commit b68150b304

View file

@ -307,7 +307,6 @@ impl Interpreter {
let real_len = args.len(); let real_len = args.len();
let func_args = function.get("args").unwrap().as_array().unwrap(); let func_args = function.get("args").unwrap().as_array().unwrap();
if real_len == func_args.len() { if real_len == func_args.len() {
self.enter_the_scope();
for i in 0..real_len { for i in 0..real_len {
let argname = &func_args[i]; let argname = &func_args[i];
match argname { match argname {
@ -317,12 +316,11 @@ impl Interpreter {
_ => self.error("Argument name must be a string"), _ => self.error("Argument name must be a string"),
} }
} }
let name = self.run_nodes(function.get("body").unwrap().as_array().unwrap()); let name = self.run_nodes(function.get("body").unwrap().as_array().unwrap());
match name { match name {
Value::Object(name) => { Value::Object(name) => {
let result = self.eval_node(&name.get("return").unwrap()); return name.get("return").unwrap().clone();
self.exit_from_scope();
return result;
} }
_ => return Value::Null, _ => return Value::Null,
} }
@ -336,7 +334,6 @@ impl Interpreter {
} }
} }
} }
self.exit_from_scope();
Value::Null Value::Null
} }
fn define_fn(&mut self, value: &Map<String, Value>) { fn define_fn(&mut self, value: &Map<String, Value>) {
@ -377,7 +374,11 @@ impl Interpreter {
} }
fn if_node(&mut self, value: &Map<String, Value>) -> Value { fn if_node(&mut self, value: &Map<String, Value>) -> Value {
let condition = self.eval_node(&value["condition"]); let condition = self.eval_node(
&value
.get("condition")
.expect("`if` must have a `condition` argument"),
);
let nodes = &value.get("body"); let nodes = &value.get("body");
let else_nodes = &value.get("else"); let else_nodes = &value.get("else");
@ -411,7 +412,7 @@ impl Interpreter {
_ => {} _ => {}
}, },
None => { None => {
self.error("if must have a body"); self.error("`if` must have a body");
} }
} }
Value::Null Value::Null
@ -443,13 +444,18 @@ impl Interpreter {
match to_do { match to_do {
Value::String(name) => { Value::String(name) => {
if name == "break" || name == "continue" { if name == "break" || name == "continue" {
self.exit_from_scope();
return Value::String(name); return Value::String(name);
} }
} }
Value::Object(name) => { Value::Object(name) => {
let check = name.get("return"); let check = name.get("return");
match check { match check {
Some(_) => return Value::Object(name), Some(check) => {
let result = json!({"return": self.eval_node(check)});
self.exit_from_scope();
return result;
}
None => {} None => {}
} }
} }