diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-28 10:52:36 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-28 10:52:36 +0000 |
| commit | 985072a63b72332b1a012a5d686771da93342349 (patch) | |
| tree | aedec24a350d0cb4056742b44367ef33609e2a42 /src/codegen/codegen.c | |
| parent | 279bc13582cb681867bc4ebadb4287449a2c7383 (diff) | |
fix: prompt segfault and strict string eq
Diffstat (limited to 'src/codegen/codegen.c')
| -rw-r--r-- | src/codegen/codegen.c | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index b090546..a66f179 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -240,6 +240,7 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) strcmp(t1, "const char*") == 0)) { // Check if comparing to NULL - don't use strcmp for NULL comparisons + char *t2 = infer_type(ctx, node->binary.right); int is_null_compare = 0; if (node->binary.right->type == NODE_EXPR_VAR && strcmp(node->binary.right->var_ref.name, "NULL") == 0) @@ -252,16 +253,8 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) is_null_compare = 1; } - if (is_null_compare) - { - // Direct pointer comparison for NULL - fprintf(out, "("); - codegen_expression(ctx, node->binary.left, out); - fprintf(out, " %s ", node->binary.op); - codegen_expression(ctx, node->binary.right, out); - fprintf(out, ")"); - } - else + if (!is_null_compare && strcmp(t1, "string") == 0 && t2 && + strcmp(t2, "string") == 0) { fprintf(out, "(strcmp("); codegen_expression(ctx, node->binary.left, out); @@ -276,6 +269,19 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) fprintf(out, ") != 0)"); } } + else + { + // Direct pointer comparison + fprintf(out, "("); + codegen_expression(ctx, node->binary.left, out); + fprintf(out, " %s ", node->binary.op); + codegen_expression(ctx, node->binary.right, out); + fprintf(out, ")"); + } + if (t2) + { + free(t2); + } } else { |
