From 0c67ce647632e25e902e6f5308e83dc9b63e52f5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 26 Aug 2022 02:14:05 +0100 Subject: [PATCH] Stopgap limit on return count --- docs/TODO.txt | 2 +- scripts/function.toy | 2 +- source/compiler.c | 14 +++++++++++--- source/interpreter.c | 33 ++++++++++++++++++++++++++++++--- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/docs/TODO.txt b/docs/TODO.txt index 3efb00c..0a266d8 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -21,7 +21,7 @@ TODO: functions take a number of parameters TODO: functions can return any number of values TODO: function arguments 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 last argument can be a rest parameter diff --git a/scripts/function.toy b/scripts/function.toy index 2af96c6..4ffba62 100644 --- a/scripts/function.toy +++ b/scripts/function.toy @@ -1,5 +1,5 @@ -fn name(param1, param2) { +fn name(param1: string, param2: string): string { print "foobar"; print param1; return param2; diff --git a/source/compiler.c b/source/compiler.c index c9824f3..ce925b2 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -89,7 +89,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { break; 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; } @@ -114,7 +114,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { break; 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; } } @@ -176,8 +176,16 @@ static int writeNodeCollectionToCache(Compiler* compiler, Node* node) { } 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: - fprintf(stderr, ERROR "[internal] Unrecognized node type in writeNodeCollectionToCache()" RESET); + fprintf(stderr, ERROR "[internal] Unrecognized node type in writeNodeCollectionToCache()\n" RESET); return -1; } } diff --git a/source/interpreter.c b/source/interpreter.c index 4948b8c..2117e14 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -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 //declare and define each entry in the scope 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); return 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); return false; } @@ -807,8 +807,35 @@ static bool execFnCall(Interpreter* interpreter) { 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++) { - 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