main: deal with file read errors more appropriately, unify error interface
- ~read_file~ now returns an error code and takes the ~sv_t~ (which contains the file contents) by pointer. We can now deal with the error in ~main~ directly. - Make the return code of ~main~ a variable which error branches can set. Unify the error branch and normal branch code. Pattern for error handling is now unified.
This commit is contained in:
45
src/main.c
45
src/main.c
@@ -18,28 +18,37 @@
|
|||||||
#include <arl/parser/ast.h>
|
#include <arl/parser/ast.h>
|
||||||
#include <arl/parser/parser.h>
|
#include <arl/parser/parser.h>
|
||||||
|
|
||||||
sv_t read_file(const char *filename)
|
int read_file(const char *filename, sv_t *ret)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(filename, "rb");
|
FILE *fp = fopen(filename, "rb");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
FAIL("File `%s` does not exist\n", filename);
|
return 1;
|
||||||
|
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
long size = ftell(fp);
|
ret->size = ftell(fp);
|
||||||
fseek(fp, 0, SEEK_SET);
|
fseek(fp, 0, SEEK_SET);
|
||||||
char *buffer = calloc(1, size + 1);
|
ret->data = calloc(1, ret->size + 1);
|
||||||
fread(buffer, size, 1, fp);
|
fread(ret->data, ret->size, 1, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
buffer[size] = '\0';
|
ret->data[ret->size] = '\0';
|
||||||
return SV(buffer, size);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
const char *filename = "./examples/hello-world.arl";
|
const char *filename = "./examples/hello-world.arl";
|
||||||
sv_t contents = read_file(filename);
|
sv_t contents = {0};
|
||||||
printf("%s\n=> `" PR_SV "`\n", filename, SV_FMT(contents));
|
if (read_file(filename, &contents))
|
||||||
|
{
|
||||||
|
LOG_ERR("ERROR: Reading `%s`: ", filename);
|
||||||
|
perror("");
|
||||||
|
ret = 1;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG("%s => `" PR_SV "`\n", filename, SV_FMT(contents));
|
||||||
|
|
||||||
parse_stream_t stream = {.byte = 0, .contents = contents};
|
parse_stream_t stream = {.byte = 0, .contents = contents};
|
||||||
ast_t ast = {0};
|
ast_t ast = {0};
|
||||||
@@ -49,23 +58,23 @@ int main(void)
|
|||||||
u64 line = 1, col = 0;
|
u64 line = 1, col = 0;
|
||||||
parse_stream_get_line_col(&stream, &line, &col);
|
parse_stream_get_line_col(&stream, &line, &col);
|
||||||
|
|
||||||
fprintf(stderr, "%s:%lu:%lu: %s\n", filename, line, col,
|
LOG_ERR("%s:%lu:%lu: %s\n", filename, line, col, parse_err_to_string(perr));
|
||||||
parse_err_to_string(perr));
|
ret = 1;
|
||||||
goto fail;
|
goto end;
|
||||||
}
|
}
|
||||||
printf("=> Parsed %lu nodes\n", ast.nodes.size / sizeof(ast_node_t));
|
|
||||||
|
LOG("Parsed %lu nodes\n", ast.nodes.size / sizeof(ast_node_t));
|
||||||
|
#if VERBOSE_LOGS
|
||||||
ast_print(stdout, &ast);
|
ast_print(stdout, &ast);
|
||||||
|
#endif
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
free(contents.data);
|
end:
|
||||||
ast_free(&ast);
|
|
||||||
return 0;
|
|
||||||
fail:
|
|
||||||
if (contents.data)
|
if (contents.data)
|
||||||
free(contents.data);
|
free(contents.data);
|
||||||
if (ast.nodes.capacity > 0)
|
if (ast.nodes.capacity > 0)
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
return 1;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copyright (C) 2026 Aryadev Chavali
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|||||||
Reference in New Issue
Block a user