From 0e0d4172597add04e9e7f0b8c18b1c3afbd4b60b Mon Sep 17 00:00:00 2001 From: tema5002 Date: Tue, 9 Jun 2026 11:18:41 +0300 Subject: [PATCH] Add program name parameter to noom_format_error --- src/error.c | 27 +++++++++++++++------------ src/error.h | 2 +- src/main.c | 14 +++++++------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/error.c b/src/error.c index 7b02d3b..2e0eca5 100644 --- a/src/error.c +++ b/src/error.c @@ -1,7 +1,7 @@ #include "error.h" #include "helper.h" -noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uint_t buffer_size) { +noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_name, char* buffer, noom_uint_t buffer_size) { struct noom_error { const char* s; int near; // 0:none 1: near '%s'\n 2: '%s'\n @@ -96,8 +96,11 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin noom_uint_t space = 0; - space = - sizeof("noom: ") - 1 + + if (program_name) space += + noom_strlen(program_name) + + sizeof(": ") - 1; + + space += noom_strlen(parser->filename) + sizeof(":") - 1 + linedig + @@ -105,19 +108,19 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin noom_strlen(err.s) + + 1; // \0; - if (err.near) { - space += ( - (err.near == 1 ? sizeof(" near") - 1 : 0) + - sizeof(" '") - 1 + - parser->last_token_length + - sizeof("'\n") - 1 - ); - } + if (err.near) space += + (err.near == 1 ? sizeof(" near") - 1 : 0) + + sizeof(" '") - 1 + + parser->last_token_length + + sizeof("'\n") - 1; return space; } - noom_safe_strcpy(buffer, &pos, buffer_size, "noom: "); + if (program_name) { + noom_safe_strcpy(buffer, &pos, buffer_size, program_name); + noom_safe_strcpy(buffer, &pos, buffer_size, ": "); + } noom_safe_strcpy(buffer, &pos, buffer_size, parser->filename); noom_safe_strcpy(buffer, &pos, buffer_size, ":"); diff --git a/src/error.h b/src/error.h index 5f0d3a8..bac0f61 100644 --- a/src/error.h +++ b/src/error.h @@ -3,4 +3,4 @@ #include "types.h" #include "parser.h" -noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uint_t buffer_size); +noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_name, char* buffer, noom_uint_t buffer_size); diff --git a/src/main.c b/src/main.c index d390f29..56cca8e 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ void print_node(const noomP_Node *node, noom_uint_t depth) { printf("}\n"); } -int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(const char *code, const char *filename) { +int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(const char *code, const char *program_name, const char *filename) { noomP_Parser parser; noomP_Node *program; @@ -84,9 +84,9 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later( print_node(program, 0); } else { - noom_uint_t bleh = noom_format_error(&parser, NULL, 0); + noom_uint_t bleh = noom_format_error(&parser, program_name, NULL, 0); char* buf = noom_alloc(bleh); - noom_format_error(&parser, buf, bleh); + noom_format_error(&parser, program_name, buf, bleh); fputs(buf, stdout); noom_free(buf); } @@ -237,23 +237,23 @@ int main(int argc, char **argv) { } if (params.script_exec || params.script_path) { if (params.script_exec) { - return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(params.script_exec, "(command line)"); + return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(params.script_exec, argv[0], "(command line)"); } char* code = read_file(params.script_path); if (code == 0) return 1; - return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, params.script_path); + return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, argv[0], params.script_path); } if (params.use_stdin) { char* code = read_stdin(); if (code == 0) return 1; - return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, "stdin"); + return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, argv[0], "stdin"); } if (params.enter_repl) { puts(NOOM_VERSION_TEXT); for (;;) { char code[4096]; if (read_prompt(code, sizeof(code), "> ", 1)) return 0; - the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, "(noom input)"); + the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, 0, "(noom input)"); } } die: