Glubs9 / python-interpreter-for-brainfuck-using-numbers

This is just a small little python interpreter for brainfuck, it doesn't support characters being outputted because i find it more fun to work with numbers, though i'm sure i could implement a sorta like print both things, numbers and characters when it prints. also sorry if the code is a bit shit i wrote it in an hour at 10 pm.
0 stars 0 forks source link

Looping bug in interpreter #1

Open rdebath opened 5 years ago

rdebath commented 5 years ago

This little BF program should print 79 75 10 or OK in ASCII

++++++++++[>++++++++>+<<-]>-.<----[>->>[[]<<++>>][+]<<<+]>.>.

It hangs after the first output.

$ bfi -fintio ok-test3.b
79
75
10
$ time python brainfuck.py ok-test3.b
79
^CTraceback (most recent call last):
  File "brainfuck.py", line 68, in <module>
    main() #I have it all in a main function just because it looks nicer and I can re-use useful keywords like tokens.
  File "brainfuck.py", line 66, in main
    interp(tokens)
  File "brainfuck.py", line 37, in interp
    elif tokens[n] == ",":
KeyboardInterrupt
Command exited with non-zero status 1
12.36user 0.00system 0:12.37elapsed 99%CPU (0avgtext+0avgdata 5608maxresident)k
0inputs+0outputs (0major+566minor)pagefaults 0swaps
$
Glubs9 commented 5 years ago

I was looking at your program and I was a bit confused about a large portion of it, it seemed to be completely unnecessary to me so I deleted it, (the part I'm talking about is the [[]<<++>>][+] part), after deleting this I got ++++++++++[>++++++++>+<<-]>-.<----[>-<+]>.>. which does work with my interpreter. Though the original should work. this is probably a problem with the way my interpreter is skipping the brackets. I will fix this and get back to you.

Glubs9 commented 5 years ago

lol sorry did not mean to close the issue

Glubs9 commented 5 years ago

the issue was that when I was skipping past the empty brackets I went one token further than I should've, So in the interp function I added n -= 1 at the bottom of the if statement for "[" tokens which made it work!, Thank you so much for the feedback it really helped!

rdebath commented 5 years ago

... seemed to be completely unnecessary ...

You're not wrong, from the "printing OK" point of view, the extra code is for testing interpreters. Still, this sounds like a very good reason to stick with my "Hello World" program instead, it has much less obviously "unnecessary" code.

+[>[<-[]>+[>+++>[+++++++++++>][>]-[<]>-]]++++++++++<]>
>>>>>----.<<+++.<-..+++.<-.>>>.<<.+++.------.>-.<<+.<.
Glubs9 commented 5 years ago

Thanks for the test!