error: program name parameter for noom_format_error

Reviewed-on: NeoFlock/noom#6
Co-authored-by: tema5002 <tema5002@tuta.io>
Co-committed-by: tema5002 <tema5002@tuta.io>
This commit is contained in:
2026-06-09 13:04:05 +02:00
committed by Blendi
parent 5ff0594791
commit 8709efda7d
3 changed files with 23 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
#include "error.h" #include "error.h"
#include "helper.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 { struct noom_error {
const char* s; const char* s;
int near; // 0:none 1: near '%s'\n 2: '%s'\n 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; noom_uint_t space = 0;
space = if (program_name) space +=
sizeof("noom: ") - 1 + noom_strlen(program_name) +
sizeof(": ") - 1;
space +=
noom_strlen(parser->filename) + noom_strlen(parser->filename) +
sizeof(":") - 1 + sizeof(":") - 1 +
linedig + linedig +
@@ -105,19 +108,19 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin
noom_strlen(err.s) + noom_strlen(err.s) +
+ 1; // \0; + 1; // \0;
if (err.near) { if (err.near) space +=
space += (
(err.near == 1 ? sizeof(" near") - 1 : 0) + (err.near == 1 ? sizeof(" near") - 1 : 0) +
sizeof(" '") - 1 + sizeof(" '") - 1 +
parser->last_token_length + parser->last_token_length +
sizeof("'\n") - 1 sizeof("'\n") - 1;
);
}
return space; 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, parser->filename);
noom_safe_strcpy(buffer, &pos, buffer_size, ":"); noom_safe_strcpy(buffer, &pos, buffer_size, ":");

View File

@@ -3,4 +3,4 @@
#include "types.h" #include "types.h"
#include "parser.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);

View File

@@ -34,7 +34,7 @@ void print_node(const noomP_Node *node, noom_uint_t depth) {
printf("}\n"); 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_Parser parser;
noomP_Node *program; noomP_Node *program;
@@ -84,9 +84,9 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(
print_node(program, 0); print_node(program, 0);
} }
else { 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); char* buf = noom_alloc(bleh);
noom_format_error(&parser, buf, bleh); noom_format_error(&parser, program_name, buf, bleh);
fputs(buf, stdout); fputs(buf, stdout);
noom_free(buf); noom_free(buf);
} }
@@ -237,23 +237,23 @@ int main(int argc, char **argv) {
} }
if (params.script_exec || params.script_path) { if (params.script_exec || params.script_path) {
if (params.script_exec) { 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); char* code = read_file(params.script_path);
if (code == 0) return 1; 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) { if (params.use_stdin) {
char* code = read_stdin(); char* code = read_stdin();
if (code == 0) return 1; 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) { if (params.enter_repl) {
puts(NOOM_VERSION_TEXT); puts(NOOM_VERSION_TEXT);
for (;;) { for (;;) {
char code[4096]; char code[4096];
if (read_prompt(code, sizeof(code), "> ", 1)) return 0; 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: die: