Fixed a string comparison bug

This commit is contained in:
2022-08-05 17:11:21 +01:00
parent 8f2ba5cd50
commit bfaf4e83bb
5 changed files with 15 additions and 12 deletions

View File

@@ -6,3 +6,6 @@ print 42;
print 3.14;
print -69;
print -4.20;
print "hello world";
print "hello world";
print "hello world";

View File

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

View File

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

View File

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

View File

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