7
Brainf**k is the most famous esoteric programming language and is the inspiration for hundreds of other esoteric languages. In fact, there are quite a few languages that are so heavily based off of Brainf**k that the only difference is the characters used. Your challenge is to interpret one of these languages.
Brainf**k
Brainf**k uses these commands:
> Move pointer to the right
< Move pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell under the pointer is 0
] Jump back to the matching [ if the cell under the pointer is nonzero
Trivial Brainf**k Substitution
There are lots of esolangs that are identical to Brainf**k in that they use the same commands, just with different characters/strings signaling the commands. For example, take Ook!:
Brainf**k | Ook | Command
-----------------------------------------------------
> | Ook. Ook? | Move the pointer to the right
< | Ook? Ook. | Move the pointer to the left
+ | Ook. Ook. | Increment the memory cell under the pointer
- | Ook! Ook! | Decrement the memory cell under the pointer
. | Ook! Ook. | Output the character signified by the cell at the pointer
, | Ook. Ook! | Input a character and store it in the cell at the pointer
[ | Ook! Ook? | Jump past the matching Ook? Ook! if the cell under the pointer is 0
] | Ook? Ook! | Jump back to the matching Ook! Ook? if the cell under the pointer is nonzero
Ook is exactly like Brainf**k, except for the syntax.
TrivialBrainfuckSubstitution
is a function defined as:
TrivialBrainfuckSubstitution(string1, string2, string3, string4, string5, string6, string7, string8)
Each string provided substitutes the corresponding Brainf**k character - so string1
will be a substitution for >
, string2
will be a substitution for <
, so on and so forth.
For example, Ook! is equivalent to TrivialBrainfuckSubstitution("Ook. Ook?", "Ook? Ook.", "Ook. Ook.", "Ook! Ook!", "Ook! Ook.", "Ook. Ook!", "Ook! Ook?", "Ook? Ook!")
.
Alphuck is equivalent to TrivialBrainfuckSubstitution("a", "c", "e", "i", "j", "o", "p", "s")
.
The challenge
Implement TrivialBrainfuckSubstitution
.
To elaborate: Given eight strings representing the eight substitutions and a ninth string representing a program, interpret the program as a trivial Brainf** substitution.
Rules
TrivialBrainfuckSubstitution
may take function arguments or command line arguments. These are the only ways it may take the first eight arguments.TrivialBrainfuckSubstitution
can take the ninth argument from standard input, a file, or a literal ninth argument.TrivialBrainfuckSubstitution
must be able to take any ASCII characters as substitutions. It does not have to handle Unicode, and you can assume there are no duplicate elements provided.- Note that the actual function in your code need not be named
TrivialBrainfuckSubstitution
. This is just the title of the function; the actual name in the code can bef
, orx
, or a lambda, or whatever you like. - Your interpreter should not require any spaces between command substitutions. However, it should not break if they are present. To rephrase, it should ignore unrecognized commands, just like normal Brainf**k.
- You may assume that all substitutions are the same length. I.e. you may assume that
AB
,A
, andB
are never in the same substitution set.
Challenge-specific Brainf**k semantics
In your newly-created Brainf**k language:
- You do not need to be able to go left from the start of the tape. (You can choose to allow this.)
- The tape should have a minimum length of 30000. (If you want it can be longer or allocated dynamically.)
- The maximum value of the tape elements must be the maximum value for an integer in your (host) language.
- Input can be read from standard input or a file.
- An attempt to read
EOF
(End Of File) should not cause any undefined behavior.EOF
must simply be stored as would any other character read.
Test cases
Winner
As with code-golf, the shortest submission wins. I won't ever accept an answer unless one is 50 bytes shorter than all others.
Also, as this challenge is somewhat more difficult than others (while certainly not the hardest), I kindly ask that you include an ungolfed version and explanation with your answer.
We need to (1) replace the 8 strings, (2) interpret BF. Nothing of substance has been added, what did I miss? – Jonathan Allan – 8 years ago
@JonathanAllan Well, it is a lot harder than it sounds to replace the strings, given the requirements. Did you look at
Challenge-specific Brainf**k semantics
? And if nothing of substance was added, then why are there hundreds of languages based off Brainf**k, that plenty of people use? – MD XF – 8 years ago3Because it's an easy starting point for people implementing esolangs since the spec of Brainfuck is very easy to understand. – a spaghetto – 8 years ago
3@MDXF you can't close an esolang as a duplicate – Stephen – 8 years ago
3The only other difference I see is in cell capacity of the interpreted BF program. If others think I've made a mistake it wont take long to get reopened. – Jonathan Allan – 8 years ago
I'm honestly annoyed about all the time I wasted on this. So many people helped me work on it in the sandbox, I asked three times in TNB and in the post itself if it was a dupe, and nobody bothered to let me know. – MD XF – 8 years ago
I'm not convinced it's a duplicate; the parsing step is considerably different from that of traditional BF (and needn't go via
+-<>[],.
at all). That said, I'm also not convinced it's not a duplicate, so holding off on my vote for now. (It's currently at 4 reopen + a failed reopen review.) – None – 8 years ago@MDXF cough
– caird coinheringaahing – 8 years ago@MDXF what happens if the provided substitution has conflicts? For example,
string1 = 'A', string2 = 'B', string3 = 'AB'
Also, I think the substitutions in Test 1 are missing trailing spaces. – musicman523 – 8 years ago@musicman523 Does "You may assume that all substitutions are the same length. I.e. you may assume that
AB
,A
, andB
are never in the same substitution set." cover your query? – Jonathan Allan – 8 years ago@JonathanAllan Yep, that'll do it, thank you! I think they should also be unique strings as well. – musicman523 – 8 years ago
"You may assume that all substitutions are the same length" What, don't you want a colonoscopy?
– KSmarts – 8 years agoCan we use a function with variable arguments to automatically take the 8 strings as a list (Like python's
f(*args)
)? – sagiksp – 8 years agoYour 3rd test case has nine substitution strings. – Adám – 8 years ago
What is
EOF+1
orEOF-1
? What happens if a program printsEOF
? – aschepler – 8 years agoCan the interpreter have more than 30000 tape cells if it shortens the byte count? Also, if there are more than 30k cells, what should happen when you attempt to move right from the 30000th tape cell? – Arnold Palmer – 8 years ago
@ArnoldPalmer Yes, I'll edit. If there are 30k cells, moving right from the 30000th cell can either wrap around or seg-fault, it's your choice. – MD XF – 8 years ago
I wonder ... can this be solved in brainf**k? :D – Titus – 8 years ago
@Titus Yes, if you want a +200 bounty :P – MD XF – 8 years ago
The third test case has 9 commands, not 8 (
???
appears twice), and is failing for me when the other 2 work fine. Can you make sure that it's correct? – Business Cat – 8 years agoShould the explanation for
Ook? Ook!
includeif the cell under the pointer is nonzero
? – undergroundmonorail – 8 years ago@BusinessCat Whoops. Fixed. – MD XF – 8 years ago
@undergroundmonorail Fixed, thanks. – MD XF – 8 years ago
@MDXF What in the world is going on with the third test case? None of the answers currently finish execution, let alone print "Hello, World!" like it's supposed to. The code starts off immediately moving to the left, presumably off the tape, but you wrote that moving left off the tape wasn't a requirement. I can get TIO's brainfuck to print it, but everyone's code here either errors out, or just spins until a timeout. – Arnold Palmer – 8 years ago
@ArnoldPalmer I think I broke it somehow. I'm looking into it, removing it for now. – MD XF – 8 years ago
@MDXF I converted it to brainfuck and tried it in TIO and it worked, but tried the exact same brainfuck on everyone's answers and no one got it. It looks like TIO uses signed bytes for its cells, so it wraps at 127, but our specification requires it be the maximum value in our language, so that example won't work for us. – Arnold Palmer – 8 years ago
@ArnoldPalmer Hm, it might be TIO having issues; I know there were server outages today... or it might be exactly what you described. Either way the third case was messed up and I fixed it hurriedly so probably didn't do it properly. – MD XF – 8 years ago
@MDXF Just run
,.>-[-<.>]
to see I'm right. It starts the second cell at -1, and decrements it, while printing the character from the first cell. On TIO it prints the character 256 times (255 plus the initial print) then stops. So if we should have the same output for that test case, we need to treat our cells as bytes. – Arnold Palmer – 8 years ago