Underload/a interpreter in python

From Esolang
Jump to navigation Jump to search
def run(p):
    stack=[]
    program=p
    while program:
        x,program=program[0],program[1:]
        if x=='*':b,a=stack.pop(),stack.pop();stack.append(a+b)
        if x=='^':program=stack.pop()+program
        if x=='~':a,b=stack.pop(),stack.pop();stack.append(a);stack.append(b)
        if x==':':stack.append(stack[-1])
        if x=='!':stack.pop()
        if x=='a':stack.append(f'({stack.pop()})')
        if x=='S':print(stack.pop(),end='')
        if x=='(':
            i=1;tmp=''
            while True:
                x,program=program[0],program[1:]
                if x=='(':i+=1
                if x==')':i-=1
                if i==0:break
                tmp+=x
            stack.append(tmp)
program=r'''
(
x y pair = (x y)
pair = ~a~a*
(x y) left = x
left = ^!
(x y) right = y
right = ^~!
)!
(
x y true^ = x
true = (!)
x y false^ = y
false = (~!)
c t f if = t f c^^
if = a(a(a)~(a))~(a(a*~(^^)**^)****^)**^
true not = false
false not = true
x not = false true x ^
not = a((~!)(!))~(^)**^
)!
(
zero = pair true () = ((!)())
zero isZero = true
non_zero isZero = false
isZero = left = ^!
x succ = pair false x = ((~!) x)
succ = a((~!))~*
zero pred = zero
non_zero pred = non_zero right
pred = a:((((!)()))~(^~!))~(a(^!^)****^)**^
)!
(
init cond step loop = init (init cond^ is false)
init cond step loop = init step^cond step loop (init cond^ is true)
loop = a(a(~:a)~(a(^)*~:(a)))~(a(a(a(^~:^)**(!))**~(a*a(a(a)~(a))~(a(a*~(^^)**^)****^)**^)****^)****:^)**^

x toUnary = (1...1) (repeat x times)
toUnary 
= ()pair(left isZero not)(λx.x left right x right (1)* pair)loop right
= ()~a~a*(^!^!a((~!)(!))~(^)**^)(:a(^!^~!)~(^~!(1)*~a~a*)**^)a(a(~:a)~(a(^)*~:(a)))~(a(a(a(^~:^)**(!))**~(a*a(a(a)~(a))~(a(a*~(^^)**^)****^)**^)****^)****:^)**^^~!
)!

((!)()) a((~!))~* a((~!))~* a((~!))~*
()~a~a*(^!^!a((~!)(!))~(^)**^)(:a(^!^~!)~(^~!(1)*~a~a*)**^)a(a(~:a)~(a(^)*~:(a)))~(a(a(a(^~:^)**(!))**~(a*a(a(a)~(a))~(a(a*~(^^)**^)****^)**^)****^)****:^)**^^~!
aS
'''
run(program) #(111)

Or if you want to run it online.

https://tio.run/##zVVLbtswEN3zFNRKHNkxInTHQKdIu6lhFrQtJ0IciZAVVGpRXax36JXcmSHlj5QU6aaoDdvD9@b/aNt1zWNVfrhxneuOx22@k/VLqRxoIfFxaOzmKVuu@ODq6qG2z5nj09fHYp8PmPemRzs/ufnP5e1qgJapXp0ci51ssyxOYr2e24wLLVzlFMwv7DtvW@fycqvsbA3jeBProeBF3CxgY@8@1na@fme10XlaW8f6yoMPy5t0NfGMBk@uM2btKM8uVt8v3H9APAm5p7GLslGXo2BsFk99VXyWh@EsvWueHbpewV7Qj/VLfu3@V6pOSxezLH2LBqRvXqeLLLvV6zq3TxMWe59l7RV8tT7kQQz91jGOqUQrO@lsUctMKrSR94fe9jYRDMl9vmsQakUwTBSIunh4JKATg2X6SEAU8ja4MsOBZFGFCJjY2f2BmU6wSVSP3EY2ckdDZmxsjBF8sAqf0OOL3pRNemUMJAm9/LvxJcqKmuCcIbNHiMTKFzRDspVGeNAq6gAbxAo@IY/xLa8rZHkpHKGAmkU/BeDZ4vDZO3GRsiq/XMO@m9PxvMNWHl42myG776rl7NiJbEEE2rcGfeLruTrfIkr2uVoAT2fWQwTUaqVCyzQdJ8M1muhyfzxuURaN3FTlFi9O7uS@qhzGM6pOnMHZfLvwpwACzIgaJaGNgQhBJHKvrW8Nkl6T3F5vQnpNfZJACcpvk3fciZOVJNoPiTtvqk@lrTvac7pYLFKQqs5dbhvcfVM85wcQg4tAHyBxFIsWJMT7AurXz3bRei391W/Dp0ohYUGBx/I6UB7@QtHOo8lVA6VZDURZHsrB7kz@g72Eb60Il@R8416zxP8/i70X9OvGf9j@5w6Ox98

by User:Jan jelo