Muriel/compile from minsky machine

From Esolang
Jump to navigation Jump to search

This python program compiles Minsky machine program into Muriel program.(Using state 0 means halt, and the program starts from state 1.)

program='''
inc a 2
inc a 3
inc a 4
inc b 5
dec a 4 0
'''
def r(s):
    return (s.replace("\\","\\\\")
             .replace("\"","\\\"")
             .replace('\n','\\n'))
def compile(program):
    p=[[x for x in x.split(' ')
        if x in{'inc','dec','a','b'}or x.isdigit()]
       for x in program.split('\n')if x!='']
    d='0';e='0';f='0';g='0';h='0'
    i=1
    for x in p:
        if(x[0],x[1])==('inc','a'):
            d+=f'+(c={i})'
            h+=f'+({x[2]}*(c={i}))'
        if(x[0],x[1])==('inc','b'):
            f+=f'+(c={i})'
            h+=f'+({x[2]}*(c={i}))'
        if(x[0],x[1])==('dec','a'):
            e+=f'+((c={i})*(a>0))'
            h+=f'+((({x[2]}*(a>0))+(({x[3]})*(a=0)))*(c={i}))'
        if(x[0],x[1])==('dec','b'):
            g+=f'+((c={i})*(b>0))'
            h+=f'+((({x[2]}*(b>0))+(({x[3]})*(b=0)))*(c={i}))'
        i+=1
    res=f'a:0;b:0;c:1;d:{d};e:{e};f:{f};g:{g};h:{h};\n'
    res+=f'D:"d:{d};";\nE:"e:{e};";\nF:"f:{f};";\nG:"g:{g};";\nH:"h:{h};";\n'
    res+=r'L:"a:"+$a+";b:"+$b+";.\"a:\"+$a+\"\\nb:\"+$b";'+'\n'
    R=r'"S:\""+|S+"\";\na:"+$(a+d-e)+";b:"+$(b+f-g)+";c:"+$h+";"+D+E+F+G+H+"\nD:\""+D+"\";\nE:\""+E+"\";\nF:\""+F+"\";\nG:\""+G+"\";\nH:\""+H+"\";\nL:"+"\"a:\"+$a+\";b:\"+$b+\";.\\\"a:\\\"+$a+\\\"\\\\nb:\\\"+$b\";\nR:"+S+";\n@(%L,0,&L*(c=0))+(%R,0,&R*(c>0))"'
    res+='R:'+R+';\n'
    res+='@(%L,0,&L*(c=0))+(%R,0,&R*(c>0))'
    S=f'S:"{r(R)}";\n'
    res=S+res
    return res
print(compile(program))

For this example, the output will be:

S:"\"S:\\\"\"+|S+\"\\\";\\na:\"+$(a+d-e)+\";b:\"+$(b+f-g)+\";c:\"+$h+\";\"+D+E+F+G+H+\"\\nD:\\\"\"+D+\"\\\";\\nE:\\\"\"+E+\"\\\";\\nF:\\\"\"+F+\"\\\";\\nG:\\\"\"+G+\"\\\";\\nH:\\\"\"+H+\"\\\";\\nL:\"+\"\\\"a:\\\"+$a+\\\";b:\\\"+$b+\\\";.\\\\\\\"a:\\\\\\\"+$a+\\\\\\\"\\\\\\\\nb:\\\\\\\"+$b\\\";\\nR:\"+S+\";\\n@(%L,0,&L*(c=0))+(%R,0,&R*(c>0))\"";
a:0;b:0;c:1;d:0+(c=1)+(c=2)+(c=3);e:0+((c=5)*(a>0));f:0+(c=4);g:0;h:0+(2*(c=1))+(3*(c=2))+(4*(c=3))+(5*(c=4))+(((4*(a>0))+((0)*(a=0)))*(c=5));
D:"d:0+(c=1)+(c=2)+(c=3);";
E:"e:0+((c=5)*(a>0));";
F:"f:0+(c=4);";
G:"g:0;";
H:"h:0+(2*(c=1))+(3*(c=2))+(4*(c=3))+(5*(c=4))+(((4*(a>0))+((0)*(a=0)))*(c=5));";
L:"a:"+$a+";b:"+$b+";.\"a:\"+$a+\"\\nb:\"+$b";
R:"S:\""+|S+"\";\na:"+$(a+d-e)+";b:"+$(b+f-g)+";c:"+$h+";"+D+E+F+G+H+"\nD:\""+D+"\";\nE:\""+E+"\";\nF:\""+F+"\";\nG:\""+G+"\";\nH:\""+H+"\";\nL:"+"\"a:\"+$a+\";b:\"+$b+\";.\\\"a:\\\"+$a+\\\"\\\\nb:\\\"+$b\";\nR:"+S+";\n@(%L,0,&L*(c=0))+(%R,0,&R*(c>0))";
@(%L,0,&L*(c=0))+(%R,0,&R*(c>0))

And the output of this Muriel program will be:

a:0
b:4

by User:Jan jelo