aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib.c8
-rw-r--r--main.c13
-rw-r--r--parser.c20
-rw-r--r--parser.h2
4 files changed, 36 insertions, 7 deletions
diff --git a/lib.c b/lib.c
index 83f3516..4f79345 100644
--- a/lib.c
+++ b/lib.c
@@ -33,6 +33,14 @@ char *fread_all(FILE *fp)
size_t used, available;
} buffer = {calloc(CHUNK_SIZE, sizeof(*buffer.data)), 0, CHUNK_SIZE};
+ if (!buffer.data)
+ {
+ print_error(
+ "[internal]", 0, 0,
+ "ERROR: Out of Memory (could not allocate buffer in fread_all)");
+ return NULL;
+ }
+
size_t acc = 0, bytes_read = 0;
while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data),
CHUNK_SIZE, fp)) != 0)
diff --git a/main.c b/main.c
index bef8d3c..c8e16ea 100644
--- a/main.c
+++ b/main.c
@@ -74,14 +74,19 @@ void interpret(machine_t *cpu, node_t *ast, size_t num)
#endif
}
+void usage(const char *name, FILE *fp)
+{
+ fprintf(fp,
+ "Usage: %s [FILE]...\n\tExecutes FILES sequentially on the "
+ "same machine\n",
+ name);
+}
+
int main(int argc, char *argv[])
{
if (argc == 1)
{
- fprintf(
- stderr,
- "Usage: %s [FILE]...\nReads FILES sequentially on the same machine\n",
- argv[0]);
+ usage(argv[0], stderr);
return 1;
}
diff --git a/parser.c b/parser.c
index d1bc9c9..c3129f6 100644
--- a/parser.c
+++ b/parser.c
@@ -11,6 +11,13 @@
char *ast_to_str(node_t *ast, size_t size)
{
char *out = calloc(size + 1, 1);
+ if (!out)
+ {
+ print_error(
+ "[internal]", 0, 0,
+ "ERROR: Out of Memory (could not allocate buffer in ast_to_str)");
+ return NULL;
+ }
for (size_t i = 0; i < size; ++i)
{
switch (ast[i].type)
@@ -49,6 +56,8 @@ struct PResult parse_buffer(buffer_t *buffer)
{
node_t *nodes = NULL;
size_t usable = 0, loops = 0;
+
+ // First pass: Compute |nodes|
for (size_t i = 0; i < buffer->size; ++i)
if (usable_character(buffer->data[i]))
{
@@ -57,8 +66,15 @@ struct PResult parse_buffer(buffer_t *buffer)
++loops;
}
nodes = calloc(usable, sizeof(*nodes));
+ if (!nodes)
+ {
+ print_error(
+ "[internal]", 0, 0,
+ "ERROR: Out of Memory (could not allocate buffer in parse_buffer)");
+ return (struct PResult){0};
+ }
- // First pass: Get my info
+ // Second pass: parse nodes
for (size_t i = 0, col = 0, row = 1, nptr = 0; i < buffer->size; ++i)
{
++col;
@@ -104,7 +120,7 @@ struct PResult parse_buffer(buffer_t *buffer)
}
}
- // Second pass: setup any loop references
+ // Third pass: setup loop references
node_t *stack[loops];
memset(stack, 0, loops);
size_t stackptr = 0;
diff --git a/parser.h b/parser.h
index 70ed4ff..2b01850 100644
--- a/parser.h
+++ b/parser.h
@@ -3,7 +3,7 @@
#include "./lib.h"
-typedef struct AST
+typedef struct
{
size_t col, row;
enum