mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
fixed a bug
This commit is contained in:
@@ -24,10 +24,16 @@ print 2 + (3 * 3);
|
|||||||
print "Back to the outer scope.";
|
print "Back to the outer scope.";
|
||||||
|
|
||||||
|
|
||||||
|
print [1, 2, 3];
|
||||||
|
print [4, 5];
|
||||||
|
print ["key":"value"];
|
||||||
print [1, 2, 3];
|
print [1, 2, 3];
|
||||||
print [4, 5];
|
print [4, 5];
|
||||||
print ["key":"value"];
|
print ["key":"value"];
|
||||||
|
|
||||||
|
//empties
|
||||||
|
print [];
|
||||||
|
print [:];
|
||||||
|
|
||||||
//var arr : [int] = [1, 2, 3, 42];
|
//var arr : [int] = [1, 2, 3, 42];
|
||||||
//var dict : [string, int] = ["hello": 1, "world":2];
|
//var dict : [string, int] = ["hello": 1, "world":2];
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ void writeCompiler(Compiler* compiler, Node* node) {
|
|||||||
initLiteralArray(store);
|
initLiteralArray(store);
|
||||||
|
|
||||||
//emit an array or a dictionary definition
|
//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
|
//ensure each literal key and value are in the cache, individually
|
||||||
for (int i = 0; i < node->compound.count; i++) {
|
for (int i = 0; i < node->compound.count; i++) {
|
||||||
//keys
|
//keys
|
||||||
@@ -124,7 +124,7 @@ void writeCompiler(Compiler* compiler, Node* node) {
|
|||||||
//push the store to the cache, with instructions about how pack it
|
//push the store to the cache, with instructions about how pack it
|
||||||
index = pushLiteralArray(&compiler->literalCache, TO_DICTIONARY_LITERAL(store));
|
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
|
//ensure each literal value is in the cache, individually
|
||||||
for (int i = 0; i < node->compound.count; i++) {
|
for (int i = 0; i < node->compound.count; i++) {
|
||||||
//values
|
//values
|
||||||
@@ -139,6 +139,9 @@ void writeCompiler(Compiler* compiler, Node* node) {
|
|||||||
//push the store to the cache, with instructions about how pack it
|
//push the store to the cache, with instructions about how pack it
|
||||||
index = pushLiteralArray(&compiler->literalCache, TO_ARRAY_LITERAL(store));
|
index = pushLiteralArray(&compiler->literalCache, TO_ARRAY_LITERAL(store));
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "[Internal] Unrecognized compound type in writeCompiler()");
|
||||||
|
}
|
||||||
|
|
||||||
//push the node opcode to the bytecode
|
//push the node opcode to the bytecode
|
||||||
if (index >= 256) {
|
if (index >= 256) {
|
||||||
@@ -325,7 +328,7 @@ unsigned char* collateCompiler(Compiler* compiler, int* size) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ Token scanLexer(Lexer* lexer) {
|
|||||||
case ')': return makeToken(lexer, TOKEN_PAREN_RIGHT);
|
case ')': return makeToken(lexer, TOKEN_PAREN_RIGHT);
|
||||||
case '{': return makeToken(lexer, TOKEN_BRACE_LEFT);
|
case '{': return makeToken(lexer, TOKEN_BRACE_LEFT);
|
||||||
case '}': return makeToken(lexer, TOKEN_BRACE_RIGHT);
|
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, TOKEN_BRACKET_RIGHT);
|
||||||
|
|
||||||
case '+': return makeToken(lexer, match(lexer, '=') ? TOKEN_PLUS_ASSIGN : match(lexer, '+') ? TOKEN_PLUS_PLUS: TOKEN_PLUS);
|
case '+': return makeToken(lexer, match(lexer, '=') ? TOKEN_PLUS_ASSIGN : match(lexer, '+') ? TOKEN_PLUS_PLUS: TOKEN_PLUS);
|
||||||
|
|||||||
@@ -129,6 +129,12 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) {
|
|||||||
printToBuffer(":");
|
printToBuffer(":");
|
||||||
printLiteralCustom(ptr->entries[i].value, printToBuffer);
|
printLiteralCustom(ptr->entries[i].value, printToBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//empty dicts MUST have a ":" printed
|
||||||
|
if (ptr->count == 0) {
|
||||||
|
printToBuffer(":");
|
||||||
|
}
|
||||||
|
|
||||||
printToBuffer("]");
|
printToBuffer("]");
|
||||||
|
|
||||||
//swap the parent-call buffer back into place
|
//swap the parent-call buffer back into place
|
||||||
|
|||||||
@@ -115,10 +115,11 @@ void emitNodeBlock(Node** nodeHandle) {
|
|||||||
*nodeHandle = tmp;
|
*nodeHandle = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emitNodeCompound(Node** nodeHandle) {
|
void emitNodeCompound(Node** nodeHandle, LiteralType literalType) {
|
||||||
Node* tmp = ALLOCATE(Node, 1);
|
Node* tmp = ALLOCATE(Node, 1);
|
||||||
|
|
||||||
tmp->type = NODE_COMPOUND;
|
tmp->type = NODE_COMPOUND;
|
||||||
|
tmp->compound.literalType = literalType;
|
||||||
tmp->compound.nodes = NULL;
|
tmp->compound.nodes = NULL;
|
||||||
tmp->compound.capacity = 0;
|
tmp->compound.capacity = 0;
|
||||||
tmp->compound.count = 0;
|
tmp->compound.count = 0;
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ typedef struct NodeBlock {
|
|||||||
|
|
||||||
typedef struct NodeCompound {
|
typedef struct NodeCompound {
|
||||||
NodeType type;
|
NodeType type;
|
||||||
|
LiteralType literalType;
|
||||||
Node* nodes;
|
Node* nodes;
|
||||||
int capacity;
|
int capacity;
|
||||||
int count;
|
int count;
|
||||||
@@ -97,7 +98,7 @@ void emitNodeUnary(Node** nodeHandle, Opcode opcode);
|
|||||||
void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode);
|
void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode);
|
||||||
void emitNodeGrouping(Node** nodeHandle);
|
void emitNodeGrouping(Node** nodeHandle);
|
||||||
void emitNodeBlock(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 emitNodePair(Node** nodeHandle, Node* left, Node* right);
|
||||||
void emitNodeVarTypes(Node** nodeHandle, unsigned char mask);
|
void emitNodeVarTypes(Node** nodeHandle, unsigned char mask);
|
||||||
void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Node* varType, Node* expression);
|
void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Node* varType, Node* expression);
|
||||||
|
|||||||
@@ -128,7 +128,9 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) {
|
|||||||
while (!match(parser, TOKEN_BRACKET_RIGHT)) {
|
while (!match(parser, TOKEN_BRACKET_RIGHT)) {
|
||||||
//if empty dictionary, there will be a colon between the brackets
|
//if empty dictionary, there will be a colon between the brackets
|
||||||
if (iterations == 0 && match(parser, TOKEN_COLON)) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +159,7 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) {
|
|||||||
|
|
||||||
//init the dictionary
|
//init the dictionary
|
||||||
if (!dictionary) {
|
if (!dictionary) {
|
||||||
emitNodeCompound(&dictionary);
|
emitNodeCompound(&dictionary, LITERAL_DICTIONARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
//grow the node if needed
|
//grow the node if needed
|
||||||
@@ -184,7 +186,7 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) {
|
|||||||
|
|
||||||
//init the array
|
//init the array
|
||||||
if (!array) {
|
if (!array) {
|
||||||
emitNodeCompound(&array);
|
emitNodeCompound(&array, LITERAL_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
//grow the node if needed
|
//grow the node if needed
|
||||||
@@ -208,7 +210,9 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) {
|
|||||||
(*nodeHandle) = dictionary;
|
(*nodeHandle) = dictionary;
|
||||||
}
|
}
|
||||||
else {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user