OBFI

Brainfuck Interpreter
git clone http://git.omkov.net/OBFI
Log | Tree | Refs | README | LICENCE | Download

AuthorJakob Wakeling <[email protected]>
Date2021-08-18 04:27:31
Commit27e3d631fb56d1a0473cd3ae44408290bca7299c
Parent35b4ead3ce7badaba4c81b493930f45a51eec9a7

Stop converting instructions to opcodes

Diffstat

M src/obfi.c | 27 +++++++++++----------------

1 files changed, 11 insertions, 16 deletions

diff --git a/src/obfi.c b/src/obfi.c
index 6cbaa1f..b6eb207 100644
--- a/src/obfi.c
+++ b/src/obfi.c
@@ -135,18 +135,13 @@ int main(int ac, char *av[]) { A0 = av[0];
 		fb[j] = fb[i]; fc[j] = fc[i]; ++j;
 	} fb[j] = 0;
 
-	// Convert instructions to opcodes
-	for (i = 0; fb[i]; ++i) {
-		for (j = 0; fb[i] != ">+.,[]ZTM"[j]; ++j) {} fb[i] = j + 1;
-	}
-	
 	size_t l = 1024, t = 0, *S = malloc(l * sizeof (*S));
 	if (!S) { error(1, "%s", serr()); }
 
 	// Find and store bracket pairs
 	for (size_t i = 0; fb[i]; ++i) switch (fb[i]) {
-	case 5: { S[t++] = i; break; }
-	case 6: { --t; fc[S[t]] = i - S[t]; fc[i] = S[t] - i; break; }
+	case '[': { S[t++] = i; break; }
+	case ']': { --t; fc[S[t]] = i - S[t]; fc[i] = S[t] - i; break; }
 	}
 
 	if (run(fb, fc)) { error(1, "%s", serr()); }
@@ -166,15 +161,15 @@ static inline int run(const char *fb, const long *fc) {
 
 	// Execute each optimised Brainfuck instruction
 	for (size_t i = 0; fb[i]; ++i) switch (fb[i]) {
-	case 1: { p  += fc[i]; break; }
-	case 2: { *p += fc[i]; break; }
-	case 3: { fputc(*p, stdout); break; }
-	case 4: { int c = fgetc(stdin); *p = c != EOF ? c : 0; break; }
-	case 5: { i += !*p ? fc[i] : 0; break; }
-	case 6: { i +=  *p ? fc[i] : 0; break; }
-	case 7: { *p = 0; break; }
-	case 8: { for (; *p; p += fc[i]) {} break; }
-	case 9: { *(p + fc[i]) += *p; *p = 0; break; }
+	case '>': { p  += fc[i]; break; }
+	case '+': { *p += fc[i]; break; }
+	case '.': { fputc(*p, stdout); break; }
+	case ',': { int c = fgetc(stdin); *p = c != EOF ? c : 0; break; }
+	case '[': { i += !*p ? fc[i] : 0; break; }
+	case ']': { i +=  *p ? fc[i] : 0; break; }
+	case 'Z': { *p = 0; break; }
+	case 'T': { for (; *p; p += fc[i]) {} break; }
+	case 'M': { *(p + fc[i]) += *p; *p = 0; break; }
 	}
 
 	free(M); return 0;