Add program name parameter to noom_format_error

This commit is contained in:
2026-06-09 11:18:41 +03:00
parent 5ff0594791
commit 0e0d417259
3 changed files with 23 additions and 20 deletions

View File

@@ -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 += (
if (err.near) space +=
(err.near == 1 ? sizeof(" near") - 1 : 0) +
sizeof(" '") - 1 +
parser->last_token_length +
sizeof("'\n") - 1
);
}
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, ":");

View File

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

View File

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