aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-09-02 15:39:51 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-09-02 15:39:51 +0100
commit7d369f6fadfe7a2ff589fb11c2ba3eaa98675373 (patch)
tree4bc07595b29f4673c143692e272d7ed63d9343de /main.c
parent2d33640c037c9f3bcbddc200eec39bb713ca0c9f (diff)
downloadobf-7d369f6fadfe7a2ff589fb11c2ba3eaa98675373.tar.gz
obf-7d369f6fadfe7a2ff589fb11c2ba3eaa98675373.tar.bz2
obf-7d369f6fadfe7a2ff589fb11c2ba3eaa98675373.zip
Better error reporting
Using a buffer structure allows me to pass more information to callers and a bit more discretion over how data is passed. Better error reporting via having a file handle and reporting it Emacs/GCC style.
Diffstat (limited to 'main.c')
-rw-r--r--main.c55
1 files changed, 43 insertions, 12 deletions
diff --git a/main.c b/main.c
index 5174e3d..28dbb77 100644
--- a/main.c
+++ b/main.c
@@ -41,37 +41,60 @@ typedef struct AST
int loop_ref;
} node_t;
+typedef struct Buffer
+{
+ const char *name;
+ size_t size;
+ char data[];
+} buffer_t;
+
+buffer_t *buffer_init_str(const char *name, const char *str, size_t str_size)
+{
+ buffer_t *buf = malloc(sizeof(*buf) + str_size + 1);
+ buf->name = name;
+ buf->size = str_size;
+ memcpy(buf->data, str, str_size);
+ buf->data[str_size] = '\0';
+ return buf;
+}
+
+void print_error(const char *handle, size_t row, size_t column,
+ const char *reason)
+{
+ fprintf(stderr, "%s:%lu:%lu:%s\n", handle, row, column, reason);
+}
+
struct PResult
{
node_t *nodes;
size_t size;
-} parse_input(const char *input, size_t input_size)
+} parse_input(buffer_t *buffer)
{
node_t *nodes = NULL;
size_t usable = 0, loops = 0;
- for (size_t i = 0; i < input_size; ++i)
- if (usable_character(input[i]))
+ for (size_t i = 0; i < buffer->size; ++i)
+ if (usable_character(buffer->data[i]))
{
++usable;
- if (input[i] == '[')
+ if (buffer->data[i] == '[')
++loops;
}
nodes = calloc(usable, sizeof(*nodes));
// First pass: Get my info
- for (size_t i = 0, col = 0, row = 1, nptr = 0; i < input_size; ++i)
+ for (size_t i = 0, col = 0, row = 1, nptr = 0; i < buffer->size; ++i)
{
++col;
- if (input[i] == '\n')
+ if (buffer->data[i] == '\n')
{
++row;
col = 0;
}
- else if (usable_character(input[i]))
+ else if (usable_character(buffer->data[i]))
{
nodes[nptr].col = col;
nodes[nptr].row = row;
- switch (input[i])
+ switch (buffer->data[i])
{
case '>':
nodes[nptr].type = NEXT;
@@ -117,7 +140,8 @@ struct PResult
{
if (stackptr == 0)
{
- fputs("ERROR: Unbalanced square brackets!\n", stderr);
+ print_error(buffer->name, node->row, node->col,
+ "ERROR: Unbalanced square brackets!");
goto error;
}
// Access last IN loop
@@ -129,7 +153,9 @@ struct PResult
if (stackptr > 0)
{
- fputs("ERROR: Unbalanced square brackets!\n", stderr);
+ node_t *node = nodes + usable - 1;
+ print_error(buffer->name, node->row, node->col,
+ "ERROR: Unbalanced square brackets!");
goto error;
}
@@ -179,15 +205,20 @@ char *ast_to_str(node_t *ast, size_t size)
int main(void)
{
- const char input[] = "[[->+<]";
- struct PResult res = parse_input(input, sizeof(input) / sizeof(input[0]));
+ const char input[] = "[->+<]]";
+ buffer_t *buffer =
+ buffer_init_str("*internal*", input, sizeof(input) / sizeof(input[0]));
+ struct PResult res = parse_input(buffer);
if (res.nodes == NULL)
{
fputs("Exiting early...\n", stderr);
+ free(buffer);
return 1;
}
char *str = ast_to_str(res.nodes, res.size);
+
free(str);
free(res.nodes);
+ free(buffer);
return 0;
}