aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--lib.c57
-rw-r--r--lib.h23
-rw-r--r--main.c131
4 files changed, 119 insertions, 94 deletions
diff --git a/Makefile b/Makefile
index cfd0ecb..9e7800c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall -Wextra -Wpedantic -ggdb -fsanitize=address -std=c11
LIBS=
-OBJECTS=main.o
+OBJECTS=lib.o main.o
OUT=obf.out
ARGS=
diff --git a/lib.c b/lib.c
new file mode 100644
index 0000000..83f3516
--- /dev/null
+++ b/lib.c
@@ -0,0 +1,57 @@
+/* lib.c
+ * Created: 2023-09-02
+ * Author: Aryadev Chavali
+ * Description: General functions used throughout
+ */
+
+#include <string.h>
+
+#include "./lib.h"
+
+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);
+}
+
+char *fread_all(FILE *fp)
+{
+ const size_t CHUNK_SIZE = 1024, MULT = 2;
+ struct
+ {
+ char *data;
+ size_t used, available;
+ } buffer = {calloc(CHUNK_SIZE, sizeof(*buffer.data)), 0, CHUNK_SIZE};
+
+ size_t acc = 0, bytes_read = 0;
+ while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data),
+ CHUNK_SIZE, fp)) != 0)
+ {
+ buffer.used += bytes_read;
+ acc += bytes_read;
+ if (buffer.used + CHUNK_SIZE >= buffer.available)
+ {
+ buffer.available = MAX(buffer.available * MULT, buffer.used + CHUNK_SIZE);
+ buffer.data = realloc(buffer.data, buffer.available);
+ }
+ }
+ buffer.data = realloc(buffer.data, buffer.used + 1);
+ buffer.data[buffer.used] = '\0';
+ return buffer.data;
+}
+
+bool usable_character(char c)
+{
+ return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' ||
+ c == ',' || c == '[' || c == ']';
+}
diff --git a/lib.h b/lib.h
new file mode 100644
index 0000000..f0fa170
--- /dev/null
+++ b/lib.h
@@ -0,0 +1,23 @@
+#ifndef LIB_H
+#define LIB_H
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX(a, b) (a > b ? a : b)
+
+bool usable_character(char c);
+char *fread_all(FILE *fp);
+void print_error(const char *handle, size_t row, size_t column,
+ const char *reason);
+
+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);
+#endif
diff --git a/main.c b/main.c
index e2a0ec9..dc7269a 100644
--- a/main.c
+++ b/main.c
@@ -10,6 +10,8 @@
#include <stdlib.h>
#include <string.h>
+#include "./lib.h"
+
#define MEMORY_DEFAULT 30000
typedef struct
@@ -18,12 +20,6 @@ typedef struct
uint8_t memory[MEMORY_DEFAULT];
} machine_t;
-bool usable_character(char c)
-{
- return c == '>' || c == '<' || c == '+' || c == '-' || c == '-' || c == '.' ||
- c == ',' || c == '[' || c == ']';
-}
-
typedef struct AST
{
size_t col, row;
@@ -41,34 +37,48 @@ 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)
+char *ast_to_str(node_t *ast, size_t size)
{
- fprintf(stderr, "%s:%lu:%lu:%s\n", handle, row, column, reason);
+ char *out = calloc(size + 1, 1);
+ for (size_t i = 0; i < size; ++i)
+ {
+ switch (ast[i].type)
+ {
+ case NEXT:
+ out[i] = '>';
+ break;
+ case PREV:
+ out[i] = '<';
+ break;
+ case INC:
+ out[i] = '+';
+ break;
+ case DEC:
+ out[i] = '-';
+ break;
+ case OUT:
+ out[i] = '.';
+ break;
+ case READ:
+ out[i] = ',';
+ break;
+ case LIN:
+ out[i] = '[';
+ break;
+ case LOUT:
+ out[i] = ']';
+ break;
+ }
+ }
+ out[size] = '\0';
+ return out;
}
struct PResult
{
node_t *nodes;
size_t size;
-} parse_input(buffer_t *buffer)
+} parse_buffer(buffer_t *buffer)
{
node_t *nodes = NULL;
size_t usable = 0, loops = 0;
@@ -166,71 +176,6 @@ error:
return (struct PResult){0};
}
-char *ast_to_str(node_t *ast, size_t size)
-{
- char *out = calloc(size + 1, 1);
- for (size_t i = 0; i < size; ++i)
- {
- switch (ast[i].type)
- {
- case NEXT:
- out[i] = '>';
- break;
- case PREV:
- out[i] = '<';
- break;
- case INC:
- out[i] = '+';
- break;
- case DEC:
- out[i] = '-';
- break;
- case OUT:
- out[i] = '.';
- break;
- case READ:
- out[i] = ',';
- break;
- case LIN:
- out[i] = '[';
- break;
- case LOUT:
- out[i] = ']';
- break;
- }
- }
- out[size] = '\0';
- return out;
-}
-
-#define MAX(a, b) (a > b ? a : b)
-
-char *fread_all(FILE *fp)
-{
- const size_t CHUNK_SIZE = 1024, MULT = 2;
- struct
- {
- char *data;
- size_t used, available;
- } buffer = {calloc(CHUNK_SIZE, sizeof(*buffer.data)), 0, CHUNK_SIZE};
-
- size_t acc = 0, bytes_read = 0;
- while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data),
- CHUNK_SIZE, fp)) != 0)
- {
- buffer.used += bytes_read;
- acc += bytes_read;
- if (buffer.used + CHUNK_SIZE >= buffer.available)
- {
- buffer.available = MAX(buffer.available * MULT, buffer.used + CHUNK_SIZE);
- buffer.data = realloc(buffer.data, buffer.available);
- }
- }
- buffer.data = realloc(buffer.data, buffer.used + 1);
- buffer.data[buffer.used] = '\0';
- return buffer.data;
-}
-
int main(int argc, char *argv[])
{
if (argc == 1)
@@ -260,7 +205,7 @@ int main(int argc, char *argv[])
fclose(handle);
buffer = buffer_init_str(filepath, file_data, strlen(file_data));
- res = parse_input(buffer);
+ res = parse_buffer(buffer);
if (res.nodes == NULL)
{
fputs("Exiting early...\n", stderr);