diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen/codegen.c | 3 | ||||
| -rw-r--r-- | src/codegen/codegen_stmt.c | 7 | ||||
| -rw-r--r-- | src/parser/parser.h | 5 | ||||
| -rw-r--r-- | src/parser/parser_expr.c | 4 | ||||
| -rw-r--r-- | src/parser/parser_stmt.c | 14 |
5 files changed, 20 insertions, 13 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index 8f2b6c1..b375bbb 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -56,7 +56,7 @@ static void codegen_var_expr(ParserContext *ctx, ASTNode *node, FILE *out) if (node->resolved_type && strcmp(node->resolved_type, "unknown") == 0) { - if (node->var_ref.suggestion) + if (node->var_ref.suggestion && !ctx->silent_warnings) { char msg[256]; sprintf(msg, "Undefined variable '%s'", node->var_ref.name); @@ -289,7 +289,6 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) else { fprintf(out, "("); - // Left side: Only move if NOT an assignment target int is_assignment = (node->binary.op[strlen(node->binary.op) - 1] == '=' && strcmp(node->binary.op, "==") != 0 && strcmp(node->binary.op, "!=") != 0 && diff --git a/src/codegen/codegen_stmt.c b/src/codegen/codegen_stmt.c index 003ce42..ff8ea46 100644 --- a/src/codegen/codegen_stmt.c +++ b/src/codegen/codegen_stmt.c @@ -498,9 +498,10 @@ void codegen_match_internal(ParserContext *ctx, ASTNode *node, FILE *out, int us { if (is_string_literal) { - fprintf(out, "({ printf(\"%%s\", "); - codegen_expression(ctx, body, out); - fprintf(out, "); printf(\"\\n\"); 0; })"); + char *inner = body->literal.string_val; + char *code = process_printf_sugar(ctx, inner, 1, "stdout", NULL, NULL, 0); + fprintf(out, "%s;", code); + free(code); } else { diff --git a/src/parser/parser.h b/src/parser/parser.h index 8857641..e1d6bb9 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -272,7 +272,8 @@ struct ParserContext // Type Validation struct TypeUsage *pending_type_validations; - int is_speculative; // Flag to suppress side effects during speculative parsing + int is_speculative; // Flag to suppress side effects during speculative parsing + int silent_warnings; // Suppress warnings (for example, during codegen interpolation) }; typedef struct TypeUsage @@ -433,7 +434,7 @@ ASTNode *parse_match(ParserContext *ctx, Lexer *l); ASTNode *parse_return(ParserContext *ctx, Lexer *l); char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, const char *target, - char ***used_syms, int *count); + char ***used_syms, int *count, int check_symbols); ASTNode *parse_assert(ParserContext *ctx, Lexer *l); ASTNode *parse_defer(ParserContext *ctx, Lexer *l); ASTNode *parse_asm(ParserContext *ctx, Lexer *l); diff --git a/src/parser/parser_expr.c b/src/parser/parser_expr.c index 0951475..07f133e 100644 --- a/src/parser/parser_expr.c +++ b/src/parser/parser_expr.c @@ -3470,7 +3470,7 @@ ASTNode *parse_expr_prec(ParserContext *ctx, Lexer *l, Precedence min_prec) } // Reuse printf sugar to generate the prompt print - char *print_code = process_printf_sugar(ctx, inner, 0, "stdout", NULL, NULL); + char *print_code = process_printf_sugar(ctx, inner, 0, "stdout", NULL, NULL, 1); free(inner); // Checks for (args...) suffix for SCAN mode @@ -3638,7 +3638,7 @@ ASTNode *parse_expr_prec(ParserContext *ctx, Lexer *l, Precedence min_prec) newline = 0; } - char *code = process_printf_sugar(ctx, inner, newline, "stderr", NULL, NULL); + char *code = process_printf_sugar(ctx, inner, newline, "stderr", NULL, NULL, 1); free(inner); ASTNode *n = ast_create(NODE_RAW_STMT); diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c index 1b3a5d9..4b09c83 100644 --- a/src/parser/parser_stmt.c +++ b/src/parser/parser_stmt.c @@ -1345,8 +1345,10 @@ ASTNode *parse_for(ParserContext *ctx, Lexer *l) } char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, const char *target, - char ***used_syms, int *count) + char ***used_syms, int *count, int check_symbols) { + int saved_silent = ctx->silent_warnings; + ctx->silent_warnings = !check_symbols; char *gen = xmalloc(8192); strcpy(gen, "({ "); @@ -1447,7 +1449,7 @@ char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, // Analyze usage & Type Check for to_string() char *final_expr = xstrdup(clean_expr); - // Use final_expr in usage analysis if needed, but mainly for symbol tracking + if (check_symbols) { Lexer lex; lexer_init(&lex, clean_expr); // Scan original for symbols @@ -1483,6 +1485,7 @@ char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, // Parse expression fully Lexer lex; lexer_init(&lex, clean_expr); + ASTNode *expr_node = parse_expression(ctx, &lex); char *rw_expr = NULL; @@ -1696,6 +1699,7 @@ char *process_printf_sugar(ParserContext *ctx, const char *content, int newline, strcat(gen, "0; })"); free(s); + ctx->silent_warnings = saved_silent; return gen; } @@ -1871,7 +1875,8 @@ ASTNode *parse_statement(ParserContext *ctx, Lexer *l) int is_ln = (next_type == TOK_SEMICOLON); char **used_syms = NULL; int used_count = 0; - char *code = process_printf_sugar(ctx, inner, is_ln, "stdout", &used_syms, &used_count); + char *code = + process_printf_sugar(ctx, inner, is_ln, "stdout", &used_syms, &used_count, 1); if (next_type == TOK_SEMICOLON) { @@ -2428,7 +2433,8 @@ ASTNode *parse_statement(ParserContext *ctx, Lexer *l) char **used_syms = NULL; int used_count = 0; - char *code = process_printf_sugar(ctx, inner, is_ln, target, &used_syms, &used_count); + char *code = + process_printf_sugar(ctx, inner, is_ln, target, &used_syms, &used_count, 1); free(inner); if (lexer_peek(l).type == TOK_SEMICOLON) |
