Last active
August 29, 2015 13:56
-
-
Save marcojetson/8840985 to your computer and use it in GitHub Desktop.
brainfuck interpreter
This file contains 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 <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
char *p = malloc(1024); | |
int l[10], j = 0, i = 0; | |
while (argv[1][i]) { | |
switch (argv[1][i++]) { | |
case '>': | |
++p; | |
break; | |
case '<': | |
--p; | |
break; | |
case '+': | |
++*p; | |
break; | |
case '-': | |
--*p; | |
break; | |
case '.': | |
putchar(*p); | |
break; | |
case ',': | |
*p = getchar(); | |
break; | |
case '[': | |
l[j++] = i - 1; | |
break; | |
case ']': | |
j--; | |
if (*p) { | |
i = l[j]; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment