Files
obf/main.c
Aryadev Chavali 9fcd22a03b Simplify build system
RELEASE and DEBUG builds have differing build flags, triggered by
setting RELEASE variable.  No longer doing object based compilation
because:
+ gcc is fast enough without it
+ stale code compilation bugs are annoying
+ having one output binary to clean-up is just easier when switching
build-types
2024-12-03 03:17:44 +00:00

159 lines
2.9 KiB
C

/* main.c
* Created: 2023-09-02
* Author: Aryadev Chavali
* Description: Entrypoint of compiler
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./lib.h"
#include "./parser.h"
#define MEMORY_DEFAULT 30000
#define MIN_MEM_DUMP 64
typedef struct
{
size_t dp;
uint8_t memory[MEMORY_DEFAULT];
#ifdef DEBUG
size_t dp_max;
#endif
} machine_t;
void interpret(machine_t *cpu, node_t *ast, size_t num)
{
for (size_t i = 0; i < num; ++i)
{
node_t node = ast[i];
switch (node.type)
{
case NEXT:
++cpu->dp;
break;
case PREV:
--cpu->dp;
break;
case INC:
++cpu->memory[cpu->dp];
break;
case DEC:
--cpu->memory[cpu->dp];
break;
case OUT:
putchar(cpu->memory[cpu->dp]);
break;
case READ:
cpu->memory[cpu->dp] = getchar();
break;
case LIN:
if (cpu->memory[cpu->dp] == 0)
i = node.loop_ref;
break;
case LOUT:
if (cpu->memory[cpu->dp] != 0)
i = node.loop_ref;
break;
}
#ifdef DEBUG
if (cpu->dp > cpu->dp_max)
cpu->dp_max = cpu->dp + 1;
#endif
}
#ifdef DEBUG
if (cpu->dp_max < MIN_MEM_DUMP)
cpu->dp_max = MIN_MEM_DUMP;
#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)
{
usage(argv[0], stderr);
return 1;
}
char *filepath = NULL, *file_data = NULL;
buffer_t *buffer = NULL;
struct PResult res = {0};
machine_t machine = {0};
for (int i = 1; i < argc; ++i)
{
filepath = argv[i];
FILE *handle = fopen(filepath, "r");
#ifdef DEBUG
printf("[DEBUG]: Attempting to open file handle to %s\n", filepath);
#endif
if (!handle)
{
fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath);
goto error;
}
file_data = fread_all(handle);
fclose(handle);
#ifdef DEBUG
printf("[DEBUG]: Read data from file %s\n", filepath);
#endif
buffer = buffer_init_str(filepath, file_data, strlen(file_data));
#ifdef DEBUG
puts("[DEBUG]: Initialised buffer");
#endif
res = parse_buffer(buffer);
if (res.nodes == NULL)
{
fputs("Exiting early...\n", stderr);
goto error;
}
#ifdef DEBUG
char *str = ast_to_str(res.nodes, res.size);
printf("[DEBUG]: Parsed buffer (%lu nodes parsed)\n\t[DEBUG]: Out=%s\n",
res.size, str);
free(str);
#endif
interpret(&machine, res.nodes, res.size);
#ifdef DEBUG
printf("[DEBUG]: Finished interpreting, memory:");
for (size_t i = 0; i < machine.dp_max; ++i)
{
if (i % 8 == 0)
printf("\n\t");
printf("%d ", machine.memory[i]);
}
#endif
free(res.nodes);
free(buffer);
free(file_data);
}
return 0;
error:
if (buffer)
free(buffer);
if (res.nodes)
free(res.nodes);
return 1;
}