From bfaf4e83bb2169acb242564ac749d12e9b8618c5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 5 Aug 2022 17:11:21 +0100 Subject: [PATCH] Fixed a string comparison bug --- scripts/example.toy | 5 ++++- source/compiler.c | 2 +- source/debug.c | 4 ++-- source/literal_array.c | 12 ++++++------ source/parser.c | 4 ++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/scripts/example.toy b/scripts/example.toy index 12e97c8..a606f7e 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -5,4 +5,7 @@ print false; print 42; print 3.14; print -69; -print -4.20; \ No newline at end of file +print -4.20; +print "hello world"; +print "hello world"; +print "hello world"; diff --git a/source/compiler.c b/source/compiler.c index f163060..b650714 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -20,7 +20,7 @@ void initCompiler(Compiler* compiler) { void writeCompiler(Compiler* compiler, Node* node) { //grow if the bytecode space is too small - if (compiler->capacity < compiler->count + 8) { //assume 8 is the maximum space needed by each instruction (can change later) + if (compiler->capacity < compiler->count + 1) { int oldCapacity = compiler->capacity; compiler->capacity = GROW_CAPACITY(oldCapacity); diff --git a/source/debug.c b/source/debug.c index cfcd91b..b315330 100644 --- a/source/debug.c +++ b/source/debug.c @@ -176,7 +176,7 @@ void dissectBytecode(const char* tb, int size) { break; case LITERAL_STRING: { - const s = printString(tb, &count); + const char* s = printString(tb, &count); printf("(string)"); } break; @@ -190,7 +190,7 @@ void dissectBytecode(const char* tb, int size) { //code printf("--bytecode--\n"); while(tb[count] != OP_EOF) { - const opcode = printByte(tb, &count); + const unsigned char opcode = printByte(tb, &count); switch (opcode) { case OP_PRINT: diff --git a/source/literal_array.c b/source/literal_array.c index 890b549..6181e31 100644 --- a/source/literal_array.c +++ b/source/literal_array.c @@ -56,29 +56,29 @@ int findLiteralIndex(LiteralArray* array, Literal literal) { if (AS_BOOLEAN(array->literals[i]) == AS_BOOLEAN(literal)) { return i; } - break; + continue; case LITERAL_INTEGER: if (AS_INTEGER(array->literals[i]) == AS_INTEGER(literal)) { return i; } - break; + continue; case LITERAL_FLOAT: if (AS_FLOAT(array->literals[i]) == AS_FLOAT(literal)) { return i; } - break; + continue; case LITERAL_STRING: - if (strcmp(AS_STRING(array->literals[i]), AS_STRING(literal)) == 0) { + if (strncmp(AS_STRING(array->literals[i]), AS_STRING(literal), STRLEN(literal)) == 0) { return i; } - break; + + continue; default: fprintf(stderr, "[Internal] Unexpected literal type in findLiteralIndex(): %d\n", literal.type); - break; } } diff --git a/source/parser.c b/source/parser.c index 7cd1ae0..4a3f417 100644 --- a/source/parser.c +++ b/source/parser.c @@ -131,7 +131,7 @@ static void binary(Parser* parser, Node** nodeHandle, bool canBeAssigned) { static void unary(Parser* parser, Node** nodeHandle, bool canBeAssigned) { switch(parser->previous.type) { - case TOKEN_MINUS: + case TOKEN_MINUS: { //temp handle to potentially negate values Node* tmpNode = NULL; parsePrecedence(parser, &tmpNode, PREC_TERNARY); @@ -157,7 +157,7 @@ static void unary(Parser* parser, Node** nodeHandle, bool canBeAssigned) { emitNodeUnary(nodeHandle, OP_NEGATE); parsePrecedence(parser, nodeHandle, PREC_TERNARY); } - + } break; default: