fixed a bug

This commit is contained in:
2022-08-10 18:22:02 +01:00
parent 67f7b3e436
commit 380b7a3699
7 changed files with 31 additions and 10 deletions

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;
}