Fix function args

This commit is contained in:
Daniel M 2022-02-10 12:13:30 +01:00
parent ab059ce18c
commit c4d2f89d35

View File

@ -221,22 +221,26 @@ impl Interpreter {
} }
Expression::FunCall(fun_name, fun_stackpos, args) => { Expression::FunCall(fun_name, fun_stackpos, args) => {
let args_len = args.len();
// All of the arg expressions must be resolved before pushing the vars on the stack,
// otherwise the stack positions are incorrect while resolving
let args = args.iter().map(|arg| self.resolve_expr(arg)).collect::<Vec<_>>();
for arg in args { for arg in args {
let arg = self.resolve_expr(arg)?; self.vartable.push(arg?);
self.vartable.push(arg);
} }
// Function existance has been verified in the parser, so unwrap here shouldn't fail // Function existance has been verified in the parser, so unwrap here shouldn't fail
let num_args = self.funtable.get(*fun_stackpos).unwrap().argnames.len(); let expected_num_args = self.funtable.get(*fun_stackpos).unwrap().argnames.len();
if num_args != args.len() { if expected_num_args != args_len {
let fun_name = self.stringstore.lookup(*fun_name).cloned().unwrap_or("<unknown>".to_string()); let fun_name = self.stringstore.lookup(*fun_name).cloned().unwrap_or("<unknown>".to_string());
return Err(RuntimeError::InvalidNumberOfArgs(fun_name, num_args, args.len())); return Err(RuntimeError::InvalidNumberOfArgs(fun_name, expected_num_args, args_len));
} }
match self.run_block_fp_offset( match self.run_block_fp_offset(
&Rc::clone(&self.funtable.get(*fun_stackpos).unwrap().body), &Rc::clone(&self.funtable.get(*fun_stackpos).unwrap().body),
num_args, expected_num_args,
)? { )? {
BlockExit::Normal => Value::Void, BlockExit::Normal => Value::Void,
BlockExit::Return(val) => val, BlockExit::Return(val) => val,