diff --git a/scripts/example.toy b/scripts/example.toy index e1277a3..bbb9b9a 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -24,10 +24,16 @@ print 2 + (3 * 3); print "Back to the outer scope."; +print [1, 2, 3]; +print [4, 5]; +print ["key":"value"]; print [1, 2, 3]; print [4, 5]; print ["key":"value"]; +//empties +print []; +print [:]; //var arr : [int] = [1, 2, 3, 42]; //var dict : [string, int] = ["hello": 1, "world":2]; diff --git a/source/compiler.c b/source/compiler.c index efa3bf1..1cec0cb 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -102,7 +102,7 @@ void writeCompiler(Compiler* compiler, Node* node) { initLiteralArray(store); //emit an array or a dictionary definition - if (node->compound.nodes->type == NODE_PAIR) { + if (node->compound.literalType == LITERAL_DICTIONARY) { //ensure each literal key and value are in the cache, individually for (int i = 0; i < node->compound.count; i++) { //keys @@ -124,7 +124,7 @@ void writeCompiler(Compiler* compiler, Node* node) { //push the store to the cache, with instructions about how pack it index = pushLiteralArray(&compiler->literalCache, TO_DICTIONARY_LITERAL(store)); } - else { + else if (node->compound.literalType == LITERAL_ARRAY) { //ensure each literal value is in the cache, individually for (int i = 0; i < node->compound.count; i++) { //values @@ -139,6 +139,9 @@ void writeCompiler(Compiler* compiler, Node* node) { //push the store to the cache, with instructions about how pack it index = pushLiteralArray(&compiler->literalCache, TO_ARRAY_LITERAL(store)); } + else { + fprintf(stderr, "[Internal] Unrecognized compound type in writeCompiler()"); + } //push the node opcode to the bytecode if (index >= 256) { @@ -325,7 +328,7 @@ unsigned char* collateCompiler(Compiler* compiler, int* size) { break; default: - fprintf(stderr, "[Internal] Unknown literal type encountered within literal cache\n"); + fprintf(stderr, "[Internal] Unknown literal type encountered within literal cache: %d\n", compiler->literalCache.literals[i].type); return NULL; } } diff --git a/source/lexer.c b/source/lexer.c index 162dce5..a3a5ea5 100644 --- a/source/lexer.c +++ b/source/lexer.c @@ -256,7 +256,7 @@ Token scanLexer(Lexer* lexer) { case ')': return makeToken(lexer, TOKEN_PAREN_RIGHT); case '{': return makeToken(lexer, TOKEN_BRACE_LEFT); case '}': return makeToken(lexer, TOKEN_BRACE_RIGHT); - case '[': return makeToken(lexer, match(lexer, ']') ? TOKEN_ARRAY : TOKEN_BRACKET_LEFT); + case '[': return makeToken(lexer, TOKEN_BRACKET_LEFT); case ']': return makeToken(lexer, TOKEN_BRACKET_RIGHT); case '+': return makeToken(lexer, match(lexer, '=') ? TOKEN_PLUS_ASSIGN : match(lexer, '+') ? TOKEN_PLUS_PLUS: TOKEN_PLUS); diff --git a/source/literal.c b/source/literal.c index 0922a52..014bee7 100644 --- a/source/literal.c +++ b/source/literal.c @@ -129,6 +129,12 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) { printToBuffer(":"); printLiteralCustom(ptr->entries[i].value, printToBuffer); } + + //empty dicts MUST have a ":" printed + if (ptr->count == 0) { + printToBuffer(":"); + } + printToBuffer("]"); //swap the parent-call buffer back into place diff --git a/source/node.c b/source/node.c index 7cb4f28..f240244 100644 --- a/source/node.c +++ b/source/node.c @@ -115,10 +115,11 @@ void emitNodeBlock(Node** nodeHandle) { *nodeHandle = tmp; } -void emitNodeCompound(Node** nodeHandle) { +void emitNodeCompound(Node** nodeHandle, LiteralType literalType) { Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_COMPOUND; + tmp->compound.literalType = literalType; tmp->compound.nodes = NULL; tmp->compound.capacity = 0; tmp->compound.count = 0; diff --git a/source/node.h b/source/node.h index 4145db9..06bc80d 100644 --- a/source/node.h +++ b/source/node.h @@ -52,6 +52,7 @@ typedef struct NodeBlock { typedef struct NodeCompound { NodeType type; + LiteralType literalType; Node* nodes; int capacity; int count; @@ -97,7 +98,7 @@ void emitNodeUnary(Node** nodeHandle, Opcode opcode); void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode); void emitNodeGrouping(Node** nodeHandle); void emitNodeBlock(Node** nodeHandle); -void emitNodeCompound(Node** nodeHandle); +void emitNodeCompound(Node** nodeHandle, LiteralType literalType); void emitNodePair(Node** nodeHandle, Node* left, Node* right); void emitNodeVarTypes(Node** nodeHandle, unsigned char mask); void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Node* varType, Node* expression); diff --git a/source/parser.c b/source/parser.c index 733e19d..a0ec6be 100644 --- a/source/parser.c +++ b/source/parser.c @@ -128,7 +128,9 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) { while (!match(parser, TOKEN_BRACKET_RIGHT)) { //if empty dictionary, there will be a colon between the brackets if (iterations == 0 && match(parser, TOKEN_COLON)) { - consume(parser, TOKEN_BRACE_RIGHT, "Expected ']' at the end of empty dictionary definition"); + consume(parser, TOKEN_BRACKET_RIGHT, "Expected ']' at the end of empty dictionary definition"); + //emit an empty dictionary and finish + emitNodeCompound(&dictionary, LITERAL_DICTIONARY); break; } @@ -157,7 +159,7 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) { //init the dictionary if (!dictionary) { - emitNodeCompound(&dictionary); + emitNodeCompound(&dictionary, LITERAL_DICTIONARY); } //grow the node if needed @@ -184,7 +186,7 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) { //init the array if (!array) { - emitNodeCompound(&array); + emitNodeCompound(&array, LITERAL_ARRAY); } //grow the node if needed @@ -208,7 +210,9 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) { (*nodeHandle) = dictionary; } else { - error(parser, parser->current, "[internal] Couldn't determine if should save an array or dictionary"); + //both are null, must be an array (because reasons) + emitNodeCompound(&array, LITERAL_ARRAY); + (*nodeHandle) = array; }