r/dailyprogrammer • u/oskar_s • May 11 '12
[5/11/2012] Challenge #51 [intermediate]
Brainfuck is an extremely minimalistic programming language. The memory consists of a large array of bytes, the "tape", which is manipulated by moving around a single tape pointer. The 8 commands are:
| Character | Action |
|---|---|
| < | move the pointer to the left |
| > | move the pointer to the right |
| + | increment the byte the pointer is pointing at (mod 256) |
| - | decrement the byte the pointer is pointing at (mod 256) |
| [ | if the data which the tape pointer is pointing at is 0, jump forward to the command after the matching square bracket. Otherwise, just continue to the next command |
| ] | if the data which the tape pointer is pointing at is not 0, jump backwards to the command after the matching square bracket. Otherwise, just continue to the next command |
| , | read a character from the input and store it into the current pointer byte |
| . | output the current pointer byte as an ascii character |
Any other character is ignored and treated as a comment
[ ... ] thus make a kind of while loop, equivalent to something like "while(data[pointer] != 0) { ... }". The brackets match like parentheses usually do, each starting one has a matching ending one. These loops can be nested inside other loops.
Write a program that reads a brainfuck program and its input, interprets the code, and returns the output.
More information, including a "Hello World" program, can be found on wikipedia.
If you've written your program successfully, try running this and see what pops out:
++++++++++[>>++++++>+++++++++++>++++++++++>+++++++++>+++>+++++>++++>++++++++>+[<
]<-]>>+++++++.>+.-.>+++.<++++.>>+++++++.<<++.+.>+++++.>.<<-.>---.<-----.-.+++++.
>>>+++.-.<<-.<+..----.>>>>++++++++.>+++++++..<<<<+.>>>>-.<<<<.++++.------.<+++++
.---.>>>>>.<<<++.<<---.>++++++.>>>>+.<<<-.--------.<<+.>>>>>>+++.---.<-.<<<<---.
<.>---.>>>>>>. - Thanks to nooodl for submitting this problem in /r/dailyprogrammer_ideas!
7
u/Ttl May 11 '12
Brainfuck compiler using gcc. Probably doesn't work in Windows, but it should be easy to fix.
4
u/prophile May 11 '12
Implemented in x86-64 assembly for Darwin: https://gist.github.com/5ecaafa3db79fa7e19c5
It's got some clever threading tricks so it should be fairly fast.
1
u/[deleted] May 13 '12 edited May 13 '12
I tried GolfScript, too. It works with Wikipedia's "Hello World!", but not the example program in the OP:
Looks cool, though.
3
u/theresidents13 May 11 '12
Python:
Implementation:
output:
(edit: formattin')
2
u/luxgladius 0 0 May 11 '12 edited May 11 '12
Perl
Output
Congratulations! http://i.imgur.com/bZpSP.jpg
2
u/luxgladius 0 0 May 11 '12
Inspired by Ttl's solution, I tried a translator as well in addition to my previous intepreter solution. One difference from his is that I combine identical adjacent operands so rather than
you get
Makes it a little more human-readable, though not much.
Perl
Output
Output's output:
perl dp51i2.pl | perl
Congratulations! http://i.imgur.com/bZpSP.jpg
2
u/wicked-canid 0 0 May 11 '12
A bit of Common Lisp, with a separate parser (in case I ever want to write a compiler :).
2
u/drb226 0 0 May 12 '12
I got stuck trying to do weird things with continuations xD
But here's what I've got: https://github.com/DanBurton/bf-interp
It works for anything that doesn't have loops, which isn't saying much, but yeah. There are simpler ways to encode a Program, but I really wanted to explore the generalized definition of Program that I made.
I threw a question on SO, maybe one of the wise Haskell sages will know how to make it work.
2
u/loonybean 0 0 May 12 '12 edited May 12 '12
Pretty verbose, but does the job in Javascript:
Tried the Hello World program from Wikipedia and the example on this post. Output for this post:
2
u/fractals_ May 12 '12 edited May 12 '12
A brainfuck interpreter and shell, in C: http://pastebin.com/FTqzFFtT
2
u/bh3 May 12 '12 edited May 12 '12
Brainfuck translator in C (brainfuck -> x86_64 assembly): https://gist.github.com/2669562
Brainfuck interpreter in C: https://gist.github.com/2669580
EDIT: new interpreter, now instead of a jump-table I just created a special purpose byte-code based language and my first-pass processing translates brainfuck to that instead of generating the jump-table and then that is run (combines benefits of the jump table and add/sub compression): https://gist.github.com/2670193
1
u/[deleted] May 12 '12
Quick Python solution, using a
dicttape and a jump table:1
u/emcoffey3 0 0 May 13 '12
Here's my solution in C#:
Output: Congratulations! http://i.imgur.com/bZpSP.jpg
1
u/[deleted] May 14 '12
Bit late to the party, but here's mine in Scala https://github.com/UberMouse/Dailyprogrammer-Answers/blob/master/src/nz/ubermouse/dailyprogrammer/challenge51/BrainfuckParser_Intermediate.scala
1
u/TweenageDream May 15 '12
That nested loop had me going for a bit, i saw the solution for a jump table but i wanted something more real time / on the fly i suppose.
Here is my solution in ruby:
1
u/BobTreehugger Sep 28 '12
solution in Go: