summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-04-15 21:30:07 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-04-15 21:30:07 +0200
commitfb317a3cafd7db4b3c60768eee8549dea9b54ce1 (patch)
treef4057e6003aac8eec916ca9bafd399c1bb556d65
parent7f53337609dda8f22497fe35850b68a554434008 (diff)
Add bfinter_simple, which is slow but "obviously correct"HEADmaster
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--bfinter_simple.c113
3 files changed, 116 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 4b8ea84..4af4306 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
bfinter
bfinter2
+bfinter_simple
*.o
*.dSYM
*.bf
+compile_commands.json
diff --git a/Makefile b/Makefile
index 0081677..4638c48 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c11 -fwrapv -D_POSIX_C_SOURCE
-TARGETS = bfinter bfinter2
+TARGETS = bfinter bfinter2 bfinter_simple
.PHONY: all clean remake
diff --git a/bfinter_simple.c b/bfinter_simple.c
new file mode 100644
index 0000000..58174fb
--- /dev/null
+++ b/bfinter_simple.c
@@ -0,0 +1,113 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <assert.h>
+
+
+static char* readfile(const char *fname, size_t *length){
+ int fd = open(fname, O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ exit(1);
+ }
+
+ struct stat statbuf;
+ if (fstat(fd, &statbuf) < 0) {
+ perror("stat");
+ exit(1);
+ }
+
+ *length = statbuf.st_size;
+
+ FILE *f = fdopen(fd, "r");
+ if (!f) {
+ fprintf(stderr, "Cannot open file '%s'\n", fname);
+ exit(1);
+ }
+ char *mem = malloc(*length + 1);
+ assert(fread(mem, 1, *length, f) == *length);
+ fclose(f);
+
+ return mem;
+}
+
+static void dump(const uint8_t *mem, size_t memsize, size_t mp) {
+ size_t low = 0, high = memsize - 1;
+ while (low < mp && mem[low] == 0) low++;
+ while (high > mp && mem[high] == 0) high--;
+ for (size_t i = low; i <= high; i++) {
+ if (i == mp) fprintf(stderr, "\x1B[4m");
+ fprintf(stderr, "%3u ", mem[i]);
+ if (i == mp) fprintf(stderr, "\x1B[0m");
+ }
+ fprintf(stderr, "\n");
+}
+
+
+int main(int argc, char **argv) {
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file.bf>\n", argv[0]);
+ return 1;
+ }
+
+ size_t srclen;
+ char *source = readfile(argv[1], &srclen);
+
+ size_t *jm = malloc(srclen * sizeof(size_t));
+ size_t *stk = malloc(srclen * sizeof(size_t));
+ size_t stklen = 0;
+ for (size_t i = 0; i < srclen; i++) {
+ if (source[i] == '[') {
+ stk[stklen++] = i;
+ } else if (source[i] == ']') {
+ if (stklen == 0) {
+ fprintf(stderr, "Too many ']'\n");
+ return 1;
+ }
+ jm[stk[stklen-1]] = i;
+ jm[i] = stk[stklen-1];
+ stklen--;
+ }
+ }
+ if (stklen > 0) {
+ fprintf(stderr, "Too many '['\n");
+ return 1;
+ }
+
+ free(stk);
+
+ const size_t memsize = 60000;
+ uint8_t *mem = calloc(memsize, 1);
+ size_t mp = memsize / 2;
+ bool stop = false;
+ for (size_t i = 0; i < srclen && !stop; i++) {
+ switch (source[i]) {
+ case '+': mem[mp]++; break;
+ case '-': mem[mp]--; break;
+ case '>': mp++; break;
+ case '<': mp--; break;
+ case ',': mem[mp] = getchar(); break;
+ case '.': putchar(mem[mp]); break;
+ case '[': if (!mem[mp]) i = jm[i]; break;
+ case ']': if (mem[mp]) i = jm[i]; break;
+ case '`': stop = true; break;
+ case '$': fprintf(stderr, "CRASH\n"); stop = true; break;
+ // case '*': usleep(5000000); break;
+ case '*': fprintf(stderr, "CHANGE\n"); source[i] = '`'; break;
+ default: continue;
+ }
+
+ // dump(mem, memsize, mp);
+ }
+
+ dump(mem, memsize, mp);
+
+ free(mem);
+ free(source);
+ free(jm);
+}