r/dailyprogrammer • u/nint22 1 2 • Nov 14 '12
[11/14/2012] Challenge #112 [Difficult]What a Brainf***
Description:
BrainFuck, is a Turing-complete (i.e. computationally-equivalent to modern programming languages), esoteric programming language. It mimics the concept of a Turing machine, a Turing-complete machine that can read, write, and move an infinite tape of data, through the use of the language's eight (that's right: 8!) operators.
An example is:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.Which prints "Hello World!"
Your goal is to write a BrainFuck interpreter from scratch, and have it support both single-character output to standard-out and single-character input from standard-in.
Formal Inputs & Outputs:
Input Description:
String BFProgram - A string of a valid BrainFuck program.
Output Description:
Your function must execute and print out any data from the above given BrainFuck program.
Sample Inputs & Outputs:
See above
Notes:
This is a trivial programming challenge if you understand the basis of a Turing-machine. I strongly urge you to read all related Wikipedia articles to prepare you for this. A more significan't challenge would be to write a BF interpreter through the BF language itself.
3
u/5outh 1 0 Nov 15 '12
I wrote one in Haskell not too long ago! Here it is: fhck.
This is a cool challenge. When I first got mine working it felt so good, like I had done something really computer-sciency and I was proud of it. It seems pretty simple now, but it's definitely an interesting and rewarding challenge.
2
u/[deleted] Nov 15 '12
Not my submission but worth noting: http://www.iwriteiam.nl/Ha_bf_inter.html
5
u/[deleted] Nov 15 '12
Is this not the same as Challenge #51? Because if so I've already done that one and my code is here https://github.com/UberMouse/Dailyprogrammer-Answers/tree/master/src/nz/ubermouse/dailyprogrammer/challenge51/intermediate
Though I don't think it works anymore because I think I broke the Parser trying to make it properly tail recursive.
5
u/Ledrug 0 2 Nov 15 '12 edited Nov 28 '12
Simpleminded BF to C translater:
If you have the indent program, piping the output through it may help some.
6
u/dreugeworst Nov 15 '12 edited Nov 23 '12
taking the easy way out by using c++, so I can implement everything quite simply. May take a shot at doing this in haskell at some point, but not sure how to do it nicely.
[edit]: nested_opens should have had type int, not size_t.
3
u/Lux01 0 0 Nov 15 '12
After doing some of the older challenges in Haskell, sleep deprived me has decided to man up and submit my solution this time. I'm nowhere near an expect at Haskell or functional programming in general so criticisms are highly welcome.
It uses the State monad to keep track of our stack position, our memory tape and the output characters. It currently handles IO by being pre-supplied all the required input and then producing the full output string.
I feel like there's a nicer solution available here but I can't see it right now.
3
u/skeeto -9 8 Nov 15 '12
Common Lisp. I
parseit into an s-expression, then interpret it withbf.Output:
3
u/IceDane 0 0 Nov 15 '12 edited Nov 15 '12
The entire program is at www.github.com/saevarb/Brainfunk As you can see by the commits, I haven't really tested this code for a while, heh. Wow.. 2 years ago. I'm getting old. Anyway, it uses a list zipper to counter the fact that we don't have trivial O(1) mutable random access arrays in Haskell. Works quite well.
Anyway, here's the real meat of it that does all the work: https://github.com/saevarb/Brainfunk/blob/master/Language/Brainfuck.hs
3
u/thestoicattack Nov 15 '12
I'd love some criticism of my Haskell solution. Compared to the other Haskell ones I've seen here, mine feels over-engineered.
3
u/cheeseburgerpizza Nov 19 '12
Here's my implementation in Haskell. It got a bit long, but I had fun with abstracting the "machine" out of program interpretation, and providing a few examples.
4
u/Valarauka_ Nov 15 '12 edited Nov 15 '12
Fun little challenge! In python:
I did loops recursively, instead of keeping a stack.
edit: Fixed a bug with looping, and changed it to take input as a second parameter, instead of from stdin. Added sample program for that.
Also: this page has a bunch of programs for people wanting more test input.
2
u/isopsephile Nov 15 '12
I recently started learning Clojure, so my solution is probably rife with unidiomatic constructs. Specifically, my approach goes out of its way to avoid the use of any explicit state, that being one of the core tenets of functional programming. Collections being callable just like normal functions is my favorite new thing that Clojure brings to the table, but I think I use them a little too much. I'm happy with the overall design, but would appreciate any feedback from somebody with more experience in functional programming.
2
u/bob1000bob Nov 15 '12 edited Nov 15 '12
C++
Output:
Working Link: http://ideone.com/hleNL8
2
u/ovaltineEuroFormula Nov 15 '12 edited Nov 15 '12
A few test cases:
and my perl implementation:
2
u/[deleted] Nov 15 '12
This will read it in char by char and put it into a simple dynamic array. Uses some static (semi-global) variables. It originally had a goto but I felt that I should at least have some standards.
2
u/Random832 Nov 15 '12
I got a bit fancy compared to the other python guy.
2
u/rahmu 0 0 Nov 15 '12
Here's my simple implementation in C:
2
u/robin-gvx 0 2 Nov 15 '12
Déjà Vu, like always (this time making use of the brand-new function
set-default):2
u/thehivemind5 Dec 24 '12
Switch based java interpreter. I used ints instead of bytes for the memory cells and added the commands " and : which correspond to ' and ., but do integer input and output. I used the following program for squaring an input integer as a test:
"[->+>+<<]>[->[->+>+<<]>>[-<<+>>]<<<]>>:
You could just as easily use normal input and then read the result by printing your memory outside the interpreter if you want to use this program as a test but don't want to implement the extra commands.
2
u/[deleted] Jan 06 '13
Lua [http://codepad.org/AZ8YAgoY]
2
u/exor674 Nov 15 '12
I wrote this ages ago, but feel like sharing it now anyway, not an infinite tape [ which breaks that part of the spec ], and is missing the ',' opcode, but is written in 6502 assembly: ( For the Ophis assembler )
https://gist.github.com/e16bc88d4ab7a97d95f8
1
u/OddOneOut Nov 15 '12 edited Nov 15 '12
C
1
u/niHiggim Nov 15 '12 edited Nov 15 '12
C++11:
1
u/javamonk Nov 17 '12
A rather verbose ruby solution. Haven't implemented any error checking :) https://github.com/javamonk/reddit-daily-programming/blob/master/112/hard/bf.rb
1
u/iMalevolence Nov 17 '12
Java. I don't know how to support a getChar() like function in java without using a gui, so I didn't implement the ','. Should be able to read nested loops (I wrote my own Hello World string using a nested loop (you can see it in the main method)).
1
u/GT100 Dec 25 '12 edited Dec 25 '12
C++, First attempt, need to do some error checking but prints out Hello World Correctly
*Edit failing at getting the spoiler to work right -- hold on.
1
u/lanerdofchristian 0 0 Dec 30 '12
1
u/muscleCarr Mar 27 '13
For my first post to this subreddit, here's a short Ruby solution:
1
u/Schmenge470 Apr 10 '13
Java:
1
u/nerdcorerising Nov 15 '12 edited Nov 15 '12
I wrote one in C# two years ago, here it is:
0
u/[deleted] Nov 19 '12
[deleted] — view deleted comment