TinyBF to Brainf*** converter

9

0

We have a Brainf*** to TinyBF converter, but not the other way around, so here's one.

Rules:

  • Your interpreter must take a valid TinyBF program, on one line, and it must output the corresponding BrainF*** program, on one line, with optional trailing whitespace/newline. No leading whitespace is allowed
  • The input may or may not contain characters that are not any of the four TinyBF characters. If so, you must print all of these characters in the same spots.
  • An answer will be accepted on April 1st (not joking), based on some obscure criteria I will use (jk ;) I will accept the shortest program that does not cheat by the following rules)
  • No 0-byte or 1-byte solutions, because it ruins the fun if you make (or there is) a programming language just for this purpose)

Test Cases


Input 1: +++++>+++++=>=|>>+=>>+|=>|>+=>+| (Computes 5 (byte 1) + 5 (byte 2) = 10 (byte 3))
Output 1: +++++>+++++<[>>+<<-]>[>+<-]

Input 2: +++>++++Hi+++:P+=>=|>|>+>+=>>&&+|=>>|=>>=+!!>>=+|>>>+| (Computes 3 (byte 1) * 8 (byte 2) = 24 (byte 3)) Output 2: +++>++++Hi+++:P+<[>[>+>+<<&&-]>>[<<+!!>>-]<<<-]

HyperNeutrino

Posted 2016-02-15T21:37:30.610

Reputation: 26 575

Will there ever be I/O commands? – lirtosiast – 2016-02-15T22:05:43.707

No, because TinyBF doesn't have IO commands. – HyperNeutrino – 2016-02-16T02:27:08.290

That's incorrect; == is brainfuck's .. – Dennis – 2018-02-13T23:16:41.133

Answers

1

Python 2, 106 bytes

a=0
r=''
for c in input():a=[a,~a][c=='='];r+={'+':'+-','>':'><','|':'[]'}.get(c,c)[a%-2]*(c!='=')
print r

Implements the TinyBF specification as found here. Try it online. Improvements were made using techniques from @Dica's answer. Requires quoted string input.

Mego

Posted 2016-02-15T21:37:30.610

Reputation: 32 998

This works for all of my test cases! Good job. – HyperNeutrino – 2016-02-16T14:43:53.767

You can save 5 bytes with r+=b.get(c,c+c)[a]*(c!='='). You can also make the for loop one line to save 3 more. You can inline the dict to save 4 more. Inlining input() is another 4. – Morgan Thrapp – 2016-02-16T14:44:18.393

Also, you need to either use raw_input or switch to Python 3 and take the byte penalty for print. Right now I get SyntaxError: invalid syntax. – Morgan Thrapp – 2016-02-16T14:50:06.753

@Mergo I based my answer on yours but I guess it's okay because the modifications are heavy, let me know if you want me to remove it – Dica – 2016-02-16T17:14:54.657

@MorganThrapp The input is quoted, so it works for 2. Taking quoted string input is allowed per a meta post that I can't find right now because I'm on mobile. – Mego – 2016-02-16T17:36:50.250

@Mego Oh, huh, did not know that. I would've assumed quoted input was disallowed. – Morgan Thrapp – 2016-02-16T17:37:46.517

@MorganThrapp Found it: http://meta.codegolf.stackexchange.com/questions/7633/string-input-with-or-without

– Mego – 2016-02-16T18:00:31.097

2

Python 3, 97 bytes

This script is based on @Mego's answer

a=0
for c in input():a-=c=='=';print({'+':'+-','>':'><','|':'[]'}.get(c,c)[a%-2]*(c!='='),end='')

Dica

Posted 2016-02-15T21:37:30.610

Reputation: 409

3I think this is too similar to Mego's answer and should have been suggested as an optimization in a comment. – mbomb007 – 2016-02-26T03:31:12.017

Although this is shorter, I accepted Mego's answer because your answer is too similar to Mego's an seems like an optimization rather than a distinct answer. – HyperNeutrino – 2017-01-19T21:05:04.573