πŸ‘€/compile from Minsky machine

From Esolang
Jump to navigation Jump to search

This python program compiles Minsky machine program into πŸ‘€ 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
'''
s=lambda x,y:f'πŸ“©/{x}\\/{y}\\'
g=lambda x:f'πŸ“¨/{x}\\'
loop=lambda c,b:'{'+f'{b}'+'}'+f'{c}'
seq=lambda*x:''.join(x)
add=lambda x,y:f'βž•/{x}\\/{y}\\'
sub=lambda x,y:f'βž–/{x}\\/{y}\\'
If=lambda flag,c,t:seq(s(flag,c),loop(g(flag),seq(t,s(flag,0))))
ifEq=lambda x,y,c:If('flag1',add(sub(1,x),y),If('flag2',add(sub(1,y),x),c))
n=lambda x:sub(1,x)
inc=lambda x,y:seq(s(x,add(g(x),1)),s('c',y))
dec=lambda x,y,z:seq(If('flag3',g('c'),seq(s(x,sub(g(x),1)),s('c',y))),If('flag3',n(g('c')),s('c',z)))
o=lambda x:f'πŸ‘€/{x}\\'
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!='']
    i=1;a,b,c='abc'
    res=seq(s(a,0),s(b,0),s(c,1),'{')
    for x in p:
        if(x[0],x[1])==('inc','a'):
            res+=ifEq(g(c),i,inc(a,x[2]))
        if(x[0],x[1])==('inc','b'):
            res+=ifEq(g(c),i,inc(b,x[2]))
        if(x[0],x[1])==('dec','a'):
            res+=ifEq(g(c),i,dec(b,x[2],x[3]))
        if(x[0],x[1])==('dec','b'):
            res+=ifEq(g(c),i,dec(b,x[2],x[3]))
        i+=1
    res+=seq('}',g(c),o('"a:"'),o(g(a)),o('";b:"'),o(g(b)))
    return res
print(compile(program))

For this example,the output will be:

πŸ“©/a\/0\πŸ“©/b\/0\πŸ“©/c\/1\{πŸ“©/flag1\/βž•/βž–/1\/πŸ“¨/c\\\/1\\{πŸ“©/flag2\/βž•/βž–/1\/1\\/πŸ“¨/c\\\{πŸ“©/a\/βž•/πŸ“¨/a\\/1\\πŸ“©/c\/2\πŸ“©/flag2\/0\}πŸ“¨/flag2\πŸ“©/flag1\/0\}πŸ“¨/flag1\πŸ“©/flag1\/βž•/βž–/1\/πŸ“¨/c\\\/2\\{πŸ“©/flag2\/βž•/βž–/1\/2\\/πŸ“¨/c\\\{πŸ“©/a\/βž•/πŸ“¨/a\\/1\\πŸ“©/c\/3\πŸ“©/flag2\/0\}πŸ“¨/flag2\πŸ“©/flag1\/0\}πŸ“¨/flag1\πŸ“©/flag1\/βž•/βž–/1\/πŸ“¨/c\\\/3\\{πŸ“©/flag2\/βž•/βž–/1\/3\\/πŸ“¨/c\\\{πŸ“©/a\/βž•/πŸ“¨/a\\/1\\πŸ“©/c\/4\πŸ“©/flag2\/0\}πŸ“¨/flag2\πŸ“©/flag1\/0\}πŸ“¨/flag1\πŸ“©/flag1\/βž•/βž–/1\/πŸ“¨/c\\\/4\\{πŸ“©/flag2\/βž•/βž–/1\/4\\/πŸ“¨/c\\\{πŸ“©/b\/βž•/πŸ“¨/b\\/1\\πŸ“©/c\/5\πŸ“©/flag2\/0\}πŸ“¨/flag2\πŸ“©/flag1\/0\}πŸ“¨/flag1\πŸ“©/flag1\/βž•/βž–/1\/πŸ“¨/c\\\/5\\{πŸ“©/flag2\/βž•/βž–/1\/5\\/πŸ“¨/c\\\{πŸ“©/flag3\/πŸ“¨/c\\{πŸ“©/b\/βž–/πŸ“¨/b\\/1\\πŸ“©/c\/4\πŸ“©/flag3\/0\}πŸ“¨/flag3\πŸ“©/flag3\/βž–/1\/πŸ“¨/c\\\{πŸ“©/c\/0\πŸ“©/flag3\/0\}πŸ“¨/flag3\πŸ“©/flag2\/0\}πŸ“¨/flag2\πŸ“©/flag1\/0\}πŸ“¨/flag1\}πŸ“¨/c\πŸ‘€/"a:"\πŸ‘€/πŸ“¨/a\\πŸ‘€/";b:"\πŸ‘€/πŸ“¨/b\\

by User:Jan jelo