mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-17 15:54:07 +10:00
Functions take a set number of arguments
This commit is contained in:
@@ -426,6 +426,25 @@ static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAd
|
||||
}
|
||||
}
|
||||
|
||||
//push the argument COUNT to the top of the stack
|
||||
int argumentsCount = findLiteralIndex(&compiler->literalCache, TO_INTEGER_LITERAL(node->fnCall.arguments->fnCollection.count));
|
||||
if (argumentsCount < 0) {
|
||||
argumentsCount = pushLiteralArray(&compiler->literalCache, TO_INTEGER_LITERAL(node->fnCall.arguments->fnCollection.count));
|
||||
}
|
||||
|
||||
if (argumentsCount >= 256) {
|
||||
//push a "long" index
|
||||
compiler->bytecode[compiler->count++] = OP_LITERAL_LONG; //1 byte
|
||||
|
||||
*((unsigned short*)(compiler->bytecode + compiler->count)) = (unsigned short)argumentsCount; //2 bytes
|
||||
compiler->count += sizeof(unsigned short);
|
||||
}
|
||||
else {
|
||||
//push the index
|
||||
compiler->bytecode[compiler->count++] = OP_LITERAL; //1 byte
|
||||
compiler->bytecode[compiler->count++] = (unsigned char)argumentsCount; //1 byte
|
||||
}
|
||||
|
||||
//call the function
|
||||
//DO NOT call the collection, this is done in binary
|
||||
}
|
||||
|
||||
@@ -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])) {
|
||||
|
||||
Reference in New Issue
Block a user