Stopgap limit on return count

This commit is contained in:
2022-08-26 02:14:05 +01:00
parent a4f1e048e9
commit 0c67ce6476
4 changed files with 43 additions and 8 deletions

View File

@@ -21,7 +21,7 @@ TODO: functions take a number of parameters
TODO: functions can return any number of values TODO: functions can return any number of values
TODO: function arguments can have specified types TODO: function arguments can have specified types
TODO: function returns can have specified types TODO: function returns can have specified types
TODO: functions are invoked by calling thier names TODO: functions are invoked by calling their names
TODO: functions are first-class citizens TODO: functions are first-class citizens
TODO: functions last argument can be a rest parameter TODO: functions last argument can be a rest parameter

View File

@@ -1,5 +1,5 @@
fn name(param1, param2) { fn name(param1: string, param2: string): string {
print "foobar"; print "foobar";
print param1; print param1;
return param2; return param2;

View File

@@ -89,7 +89,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) {
break; break;
default: default:
fprintf(stderr, ERROR "[internal] Unrecognized key node type in writeNodeCompoundToCache()" RESET); fprintf(stderr, ERROR "[internal] Unrecognized key node type in writeNodeCompoundToCache()\n" RESET);
return -1; return -1;
} }
@@ -114,7 +114,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) {
break; break;
default: default:
fprintf(stderr, ERROR "[internal] Unrecognized value node type in writeNodeCompoundToCache()" RESET); fprintf(stderr, ERROR "[internal] Unrecognized value node type in writeNodeCompoundToCache()\n" RESET);
return -1; return -1;
} }
} }
@@ -176,8 +176,16 @@ static int writeNodeCollectionToCache(Compiler* compiler, Node* node) {
} }
break; break;
case NODE_LITERAL: {
//write each piece of the declaration to the cache
int typeIndex = writeLiteralTypeToCacheOpt(&compiler->literalCache, node->fnCollection.nodes[i].atomic.literal, false);
pushLiteralArray(store, TO_INTEGER_LITERAL(typeIndex));
}
break;
default: default:
fprintf(stderr, ERROR "[internal] Unrecognized node type in writeNodeCollectionToCache()" RESET); fprintf(stderr, ERROR "[internal] Unrecognized node type in writeNodeCollectionToCache()\n" RESET);
return -1; return -1;
} }
} }

View File

@@ -783,13 +783,13 @@ static bool execFnCall(Interpreter* interpreter) {
for (int i = 0; i < paramArray->count; i += 2) { //contents is the indexes of identifier & type for (int i = 0; i < paramArray->count; i += 2) { //contents is the indexes of identifier & type
//declare and define each entry in the scope //declare and define each entry in the scope
if (!declareScopeVariable(inner.scope, paramArray->literals[i], paramArray->literals[i + 1])) { if (!declareScopeVariable(inner.scope, paramArray->literals[i], paramArray->literals[i + 1])) {
printf(ERROR "[internal] Could not redeclare parameter\n" RESET); printf(ERROR "[internal] Could not re-declare parameter\n" RESET);
freeInterpreter(&inner); freeInterpreter(&inner);
return false; return false;
} }
if (!setScopeVariable(inner.scope, paramArray->literals[i], popLiteralArray(&arguments), false)) { if (!setScopeVariable(inner.scope, paramArray->literals[i], popLiteralArray(&arguments), false)) {
printf(ERROR "[internal] Could not redefine parameter\n" RESET); printf(ERROR "[internal] Could not define parameter (bad type?)\n" RESET);
freeInterpreter(&inner); freeInterpreter(&inner);
return false; return false;
} }
@@ -807,8 +807,35 @@ static bool execFnCall(Interpreter* interpreter) {
pushLiteralArray(&returns, popLiteralArray(&inner.stack)); //NOTE: also reverses the order pushLiteralArray(&returns, popLiteralArray(&inner.stack)); //NOTE: also reverses the order
} }
//TODO: remove this when multiple assignment is enabled
if (returns.count > 1) {
printf(ERROR "ERROR: Too many values returned (multiple returns not yet implemented)\n" RESET);
//free, and skip out
freeLiteralArray(&returns);
freeLiteralArray(&arguments);
freeInterpreter(&inner);
return false;
}
for (int i = 0; i < returns.count; i++) { for (int i = 0; i < returns.count; i++) {
pushLiteralArray(&interpreter->stack, popLiteralArray(&returns)); //NOTE: reverses again Literal ret = popLiteralArray(&returns);
//check the return types
if (AS_TYPE(returnArray->literals[i]).typeOf != ret.type) {
printf(ERROR "ERROR: bad type found in return value\n" RESET);
//free, and skip out
freeLiteral(ret);
freeLiteralArray(&returns);
freeLiteralArray(&arguments);
freeInterpreter(&inner);
return false;
}
pushLiteralArray(&interpreter->stack, ret); //NOTE: reverses again
} }
//free //free