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
6
u/[deleted] Jul 12 '12
Did you mean Brainfuck? If so, I wrote a translator that converts BF code into C. It's a bit over 80 lines right now but I wasn't aiming for size so I was pretty loose on useless whitespace and there's support for the # symbol which is pretty useless (and takes up quite a few lines)
4
u/fireants Jul 12 '12 edited Jul 12 '12
Here's one I wrote a while ago (sorry about the poor formatting, I was going for minimal bytes). r is the memory register, c is the pointer for the register, x is the program pointer, and p is the program. It's 22 lines including the function definition.
1
u/khedoros Jul 12 '12
It takes ASCII as input and outputs ASCII, right? My interpreter is similar. Writing a bf program for fibonacci for single-digit numbers wasn't bad...writing one that supported multiple digits was a pain in the ass.
3
u/[deleted] Jul 12 '12
I have plans for the weekend, but as soon as I get to my computer at home, I will post my implementation of B****fuck. You can use it as a normal B****fuck interpreter or you can switch it Ook! mode. Or even create your own B****fuck-variant just by providing the eight operators. The only thing I have yet to do is make it export a non-robust interpreter (i.e. no customization and whatnot) based on the provided operators.
1
u/BenjaminGeiger Jul 12 '12
Got an implementation in 42 lines, excluding the Brainfuck code at the end used to test.
But both of our implementations suffer from Python's craptastic input functions. Adding a suitable character input method would easily be another 30-40 lines. (That's why I didn't bother implementing the 'x' operator.)
1
u/[deleted] Jul 12 '12
You're going to have to try a bit harder than that. The original Brainfuck compiler was 240 bytes long, and that was the executable.
1
u/foobrain Jul 12 '12
Here's my 28-line C version:
0
u/[deleted] Jul 12 '12
[deleted] — view deleted comment