Functions take a set number of arguments

This commit is contained in:
2022-08-26 12:48:10 +01:00
parent f36289786e
commit 7bd67765aa
6 changed files with 72 additions and 58 deletions

View File

@@ -750,8 +750,10 @@ static bool execFnCall(Interpreter* interpreter) {
LiteralArray arguments;
initLiteralArray(&arguments);
//unpack the arguments
while (interpreter->stack.count > 1) {
Literal stackSize = popLiteralArray(&interpreter->stack);
//unpack the stack of arguments
for (int i = 0; i < AS_INTEGER(stackSize); i++) {
pushLiteralArray(&arguments, popLiteralArray(&interpreter->stack)); //NOTE: also reverses the order
}
@@ -780,6 +782,19 @@ static bool execFnCall(Interpreter* interpreter) {
LiteralArray* paramArray = AS_ARRAY(inner.literalCache.literals[ readShort(inner.bytecode, &inner.count) ]);
LiteralArray* returnArray = AS_ARRAY(inner.literalCache.literals[ readShort(inner.bytecode, &inner.count) ]);
//check the param total is correct
if (paramArray->count != arguments.count * 2) {
printf(ERROR "ERROR: Incorrect number of arguments passed to function \"\n");
printLiteral(identifier);
printf("\"\n" RESET);
//free, and skip out
freeLiteralArray(&arguments);
freeInterpreter(&inner);
return false;
}
for (int i = 0; i < paramArray->count; i += 2) { //contents is the indexes of identifier & type
//declare and define each entry in the scope
if (!declareScopeVariable(inner.scope, paramArray->literals[i], paramArray->literals[i + 1])) {