-
-
Save pingiun/f72850bf2dcb0456d0cbb72046c52c5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
// initialize the tape with 30,000 zeroes | |
unsigned char tape[30000] = {0}; | |
// set the pointer to point at the left-most cell of the tape | |
unsigned char* ptr = tape; | |
int main(int argc, char* argv[]) { | |
int current_char; | |
size_t loop; | |
FILE* inp = fopen(argv[1], "r"); | |
if (inp == NULL) { | |
perror(argv[0]); | |
return 1; | |
} | |
setvbuf(stdout, NULL, _IONBF, 0); | |
for (;;) { | |
current_char = fgetc(inp); | |
if (current_char == EOF) { | |
if (feof(inp) == 0) | |
perror(argv[0]); | |
return 0; | |
} | |
if (current_char == '>') { | |
++ptr; | |
} else if (current_char == '<') { | |
--ptr; | |
} else if (current_char == '+') { | |
if (*ptr == 255) | |
*ptr = 0; | |
else | |
++*ptr | |
} else if (current_char == '-') { | |
if (*ptr == 0) | |
*ptr = 255; | |
else | |
--*ptr | |
} else if (current_char == '.' ) { | |
if (putchar(*ptr) == EOF) { | |
if (feof(stdout) == 0) | |
perror(argv[0]); | |
return 0; | |
} | |
} else if (current_char == ',') { | |
int x = getchar(); | |
if (x == EOF) { | |
if (feof(stdin) == 0) | |
perror(argv[0]); | |
return 0; | |
} | |
*ptr = x; | |
} else if (current_char == '[' && *ptr == 0) { | |
loop = 1; | |
while (loop > 0) { | |
current_char = fgetc(inp); | |
if (current_char == EOF) { | |
if (feof(inp) == 0) | |
perror(argv[0]); | |
fprintf(stderr, "Unbalanced parens!\n"); | |
return 1; | |
} | |
if (current_char == '[') { | |
loop++; | |
} else if (current_char == ']') { | |
loop--; | |
} | |
} | |
} else if (current_char == ']' && *ptr) { | |
loop = 1; | |
while (loop > 0) { | |
if (fseek(inp, -2L, SEEK_CUR) != 0) { | |
perror(argv[0]); | |
return 0; | |
} | |
current_char = fgetc(inp); | |
if (current_char == EOF) { | |
if (feof(inp) == 0) | |
perror(argv[0]); | |
fprintf(stderr, "Unbalanced parens!\n"); | |
return 1; | |
} | |
if (current_char == '[') { | |
loop--; | |
} else if (current_char == ']') { | |
loop++; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def execute(code, inp = "", steps=10000): | |
tape = defaultdict(lambda: 0) | |
cur = 0 | |
loc = 0 | |
read = 0 | |
ret = "" | |
while steps > 0: | |
try: | |
c = code[loc] | |
except IndexError: | |
return ret | |
if c == ">": | |
cur += 1 | |
elif c == "<": | |
cur -= 1 | |
elif c == "+": | |
if tape[cur] == 255: | |
tape[cur] = 0 | |
else: | |
tape[cur] += 1 | |
elif c == "-": | |
if tape[cur] == 0: | |
tape[cur] = 255 | |
else: | |
tape[cur] -= 1 | |
elif c == ".": | |
ret += chr(tape[cur]) | |
elif c == ",": | |
try: | |
tape[cur] = ord(inp[read]) | |
read += 1 | |
except IndexError: | |
raise Exception("No data left to read from input") | |
elif c == "[" and tape[cur] == 0: | |
loop = 1 | |
loc += 1 | |
while loop > 0: | |
try: | |
c = code[loc] | |
except IndexError: | |
raise Exception("Unbalanced parens") | |
if c == "[": | |
loop += 1 | |
elif c == "]": | |
loop -= 1 | |
loc += 1 | |
elif c == "]" and tape[cur] != 0: | |
loop = 1 | |
loc -= 1 | |
while loop > 0: | |
try: | |
c = code[loc] | |
except IndexError: | |
raise Exception("Unbalanced parens") | |
if c == "[": | |
loop -= 1 | |
elif c == "]": | |
loop += 1 | |
loc -= 1 | |
loc += 1 | |
steps -= 1 | |
return "You used too many steps!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment