diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:10:59 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-09-02 16:14:43 +0100 |
commit | c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd (patch) | |
tree | b15cb3c1d407bee7b8dfdd330f46b8cb084328de /main.c | |
parent | f1891b0c2e85262a8f2dc0a0d13eea192fe00c26 (diff) | |
download | obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.tar.gz obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.tar.bz2 obf-c9b6b04d190c1f9d0c4dfd7c4e66ac0743be18fd.zip |
New module for functions I no longer need to work on
lib.(h|c) basically has structures and functions I no longer want to
stare at in main.c
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 131 |
1 files changed, 38 insertions, 93 deletions
@@ -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); |