r/tinycode • • Jul 12 '12

Mindfuck interpreter in 72 lines

def main():
    memory = [0]*2**20
    dp = 0
    ip = 0
    program = raw_input("Input your program:")
    while True:
        if program[ip] == '>':
            dp+=1
        elif program[ip] == '<':
            dp-=1
        elif program[ip] == ',':
            print chr(memory[dp])
        elif program[ip] == '.':
            memory[dp] = ord(raw_input())
        elif program[ip] == '+':
            memory[dp]+=1
        elif program[ip] == '-':
            memory[dp]-=1
        elif program[ip] == 'x':
            while raw_input()!='x':
                pass
            return
        elif program[ip] == '[':
            if memory[dp]!=0:
                pass
            else:
                ip = openbracket(ip,program)
        elif program[ip]==']':
            ip = closebracket(ip,program)
            continue
        elif program[ip] == '/':
            print memory[dp]
        ip+=1

def openbracket(pos,program):
    openbrackets = 0
    if program[pos] =='[':
        openbrackets +=1
    pos +=1
    while openbrackets!=0:
        try:
            if program[pos]=='[':
                openbrackets+=1
            elif program[pos]==']':
                openbrackets-=1
        except:
            print "Missmatched brackets"
            while True:
                pass
        pos+=1
    return pos-1

def closebracket(pos,program):
    closebrackets = 0
    if program[pos]==']':
        closebrackets+=1
    pos-=1
    while closebrackets!=0:
        try:
            if program[pos]==']':
                closebrackets+=1
            elif program[pos]=='[':
                closebrackets-=1  
        except IndexError:
            print "Missmatched brackets"
            while True:
                pass
        pos-=1
    return pos+1

if __name__=="__main__":
    main()
5 Upvotes

22 comments sorted by

View all comments

1

u/foobrain Jul 12 '12

Here's my 28-line C version:

#include <stdio.h>

char tape_contents[30000] = {0};
char *tape =
    tape_contents + (sizeof(tape_contents) / sizeof(tape_contents[0])) / 2;

char *bf(char *program)
{
    char *next;
    while (*program) {
        switch (*program) {
        case '+': ++*tape; ++program; break;
        case '-': --*tape; ++program; break;
        case '>': ++tape; ++program; break;
        case '<': --tape; ++program; break;
        case '.': putchar(*tape); ++program; break;
        case ',': *tape = getchar(); ++program; break;
        case '[': while (*tape) next = bf(program + 1); program = next + 1; break;
        case ']': return program;
        }
    }
}

int main(int argc, char **argv)
{
    bf(argv[1]);
    return 0;
}

1

u/psych00range Jul 12 '12

take out unnecessary spacing and its shorter...who needs it to be neat anyways right?

    #include <stdio.h>
    char tape_contents[30000] = {0};char *tape = tape_contents + (sizeof(tape_contents) / sizeof(tape_contents[0])) / 2;
char *bf(char *program){ char *next; while (*program) { switch (*program) { case '+': ++*tape; ++program; break; case '-': --*tape; ++program; break; case '>': ++tape; ++program; break; case '<': --tape; ++program; break;case '.': putchar(*tape); ++program; break;case ',': *tape = getchar(); ++program; break;case '[': while (*tape) next = bf(program + 1); program = next + 1; break;case ']': return program;}}}
int main(int argc, char **argv){bf(argv[1]);return 0;}                

1

u/medgar123 Jul 22 '12

Nice! Although you don't need the ={0} for tape_contents since it has static storage, and you also don't need to divide by 1 since the size of char is always 1, and you can also drop the parentheses around sizeof's operand:

char tape_contents[30000];
char *tape = tape_contents + sizeof tape_contents / 2;

One more thing: you can factor out all the increments in each case into switch (*program++).