Convert Brainfuck to MarioLANG

28

3

MarioLANG is a two-dimensional programming language where the source code resembles a Super Mario Bros. level. Furthermore, its instruction set is very similar to Brainfuck's. This means that MarioLANG is essentially a 2-D Brainfuck where the instruction pointer moves like Mario. So when I wrote my MarioLANG submission for the Programming Language Quiz, I started by converting a Brainfuck "Hello, World!" program to MarioLANG. I noticed that this is possible with a very systematic process, so let's write a Brainfuck-to-MarioLANG compiler!

Note: The MarioLANG spec isn't entirely unambiguous, so I'm assuming the interpretation of the Ruby implementation.

I'll explain the process with the following Brainfuck program:

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

It prints Hello! and a trailing newline.

  1. Convert < and > to ( and ), respectively:

    ++[)+++++[)+++++++)++++++++++)+++)+((((-](-]))++.)+.+++++++..+++.)+++.).
    
  2. Add a floor for Mario to walk on:

    ++[)+++++[)+++++++)++++++++++)+++)+((((-](-]))++.)+.+++++++..+++.)+++.).
    ========================================================================
    
  3. Now the issue is that MarioLANG doesn't have loops like [ and ]. Instead, we have to use elevators and directional instructions to make Mario actually walk in a loop. First, we replace [ with > and change the floor to ". And we also replace ] with [! and change the floor to =#:

    ++>)+++++>)+++++++)++++++++++)+++)+((((-[!(-[!))++.)+.+++++++..+++.)+++.).
    =="======"===============================#===#============================
    
  4. The " and # can form elevators (starting from #, ending at "), so now all we need are auxiliary floors for Mario to walk back on. The start with ! and end with <:

    ++>)+++++>)+++++++)++++++++++)+++)+((((-[!(-[!))++.)+.+++++++..+++.)+++.).
    =="======"===============================#===#============================
             !                               <    
             #==============================="    
      !                                          <
      #=========================================="
    

    Note that all lines must be at least as long as the largest enclosing loop, because the interpreter isn't able to connect elevator ends across shorter lines. Therefore, we pad the middle lines with spaces.

And that's it. We've got a fully functional, equivalent MarioLANG program.

The Challenge

Given a valid Brainfuck program, implement the above procedure to compile it into a MarioLANG program.

You may assume that there are only command characters in the input, i.e. no characters except ,.+-<>[].

All auxiliary floor must be as close as possible to the main program floor. You may choose to pad the lines of intermediate floors either as little as possible (up to the width of the largest enclosing loop) or to the end of the main program.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter. If you don't print the result to STDOUT, it should still be a single newline-separated string.

This is code golf, so the shortest answer (in bytes) wins.

Test Cases

The test cases are formatted as follows: the first line is the Brainfuck program (your input), then there's an empty line, and everything until the next empty line is the expected output in MarioLANG. These examples use the minimal amount of padded spaces. Alternatively, you may pad each line with spaces to the width of the first line of the output.

>,++-.<

),++-.(
=======

,[.,]

,>.,[!
="===#
 !   <
 #==="

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

))>-[!((>-))+(([!
=="==#=="=======#
  !  <  !       <
  #=="  #======="

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

++>)+++++>)+++++++)++++++++++)+++)+((((-[!(-[!))++.)+.+++++++..+++.)+++.).
=="======"===============================#===#============================
         !                               <    
         #==============================="    
  !                                          <
  #=========================================="

[][[[][[]]][]][[]]

>[!>>>[!>>[![![!>[![!>>[![!
"=#"""=#""=#=#=#"=#=#""=#=#
! <  ! < ! <    ! <   ! <  
#="  #=" #="    #="   #="  
        !    <       !    <
        #===="       #===="
    !          <     
    #=========="     
   !                <
   #================"


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

))+++++++))++))++++))+++++++))+))++++))+))+++))+))+++++))+))++))+))++++++))++))++++))+++++++))+))+++++))++))+))+))++++))+++++++))+))+++++))+))+))+))++++))+++++++))+))+++++))++++++++++++++))+))+))++++))+++++++))+))+++++))++))+))+))++++))+++++++))+))+++++))+++++++++++++++++++++++++++++))+))+))++++))+++++++))+))+++++))++))+))+))+++++))+))++++++))+))++))+))++++++))+))++))+))++++++))+))++))+))++++++))+))++))+))++++++))+))++))+))++++++))+))++))+))++++++))++))++++))+++++++))+))+++++))+++++++))+))+++++))+))+))+))++++))+))++))+))++++++))+))+++++))+++++++))+))++++))+))+))++))+++++))+))+++))+))++++))+))++))+))++++++))+))+++++))+++++++++++++++++++))++))++))+++))++))+))++))++++))+++++++))++))+++++))++++++++++))+))++))++++))+))++))+))++++++))++++++))+))+))+++++))+))++++++))++))+++++))+++++++))++))++++))+))++++++>(([!))>)++++++>-((++++++++++))[!((++..-------------------)>-(.))+([!)>-(+)[!)[!((>->->->->->->)++)[!(+++++++++++++++++++++++++++++)[!(++)[!(++++++++++++++)[!(+)[!(++)[!((>-).([!(([!
========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================="===#=="======="================#=========================="========#="=====#==#=="="="="="="="=====#================================#=====#=================#====#=====#=="=====#===#
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         !   <          !                <                          !        < !     <                 !     <                                                                      !     <    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         #==="          #================"                          #========" #====="                 #====="                                                                      #====="    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                !                                                                       <            !                                        <                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                #======================================================================="            #========================================"                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   !                                                <                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   #================================================"                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 !                                                                    <                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 #===================================================================="                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               !                                                                           <                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               #==========================================================================="                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             !                                                                                   <             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             #==================================================================================="             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           !                                                                                                  <
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           #=================================================================================================="

Martin Ender

Posted 2015-08-27T10:18:39.137

Reputation: 184 808

Answers

5

CJam, 148 144 bytes

q"<>[""()>"er']/"[!"*_">!"1$|"\"#=":Cer:FF,[{SF,*L{'=t}/FI>(C#"['=-{_'##}{_CW</s}w],2*(:M'#tM('!tLM+ ;LW=34ctLW=('<tLW< ;L"S/=~:L;}fI]z~]{S-},N*

Try it online in the CJam interpreter.

Dennis

Posted 2015-08-27T10:18:39.137

Reputation: 196 637

1

Python, 707 chars

def b(c):
 c,l,o,x,j,m=list(c.replace(">",")").replace("<","(")),[list("="*len(c))],[],[],0,0
 for i,z in enumerate(c):
  i+=j
  if"["==z:
   c[i],l[0][i]=">",'"'
   x.append(i)
  if"]"==z:
   c[i],l[0][i]="[!","=#"
   c,l[0],m,d,b=list("".join(c)),list("".join(l[0])),max(m,len(x)),0,x.pop()
   for a in o:d+=b<a[0]and-~i>a[0]or b<a[1]and-~i>a[1]
   o.append((b,i+1,d))
   j+=1
 for q in o:
  d=q[2]*2+1
  while len(l)<=d+1:l.append(list(" "*len(c)))
  l[d][q[0]],l[d+1][q[0]],l[d][q[1]],l[d+1][q[1]]="!","#","<",'"'
  for i in range(q[0]+1,q[1]):l[d+1][i]="="
 g="\n".join(l.rstrip()for l in("".join(c)+"\n"+"\n".join("".join(f)for f in l)).split("\n"))
 while"\n\n"in g:g=g.replace("\n\n","\n")
 return g

There's probably a lot of space to be golfed. Doesn't work for the last test case.

Example usage:

>>> print b("++[>+++<-]>[<+++>-]<.")
=="=======#="=======#==
  !       < !       <
  #=======" #======="

No lines in the output contains trailing spaces.

Loovjo

Posted 2015-08-27T10:18:39.137

Reputation: 7 357

6If it doesn't work, it's not a solution (imo). – mınxomaτ – 2015-11-08T17:39:30.910