Advertisement
none-None1

Brainfuck extended interpreter

Apr 7th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | Source Code | 0 0
  1. """
  2. Brainfuck extended (a.k.a Bx) - An extension to BF
  3. See https://esolangs.org/wiki/Brainfuck_extended for details of this esoteric programming language
  4. """
  5. import sys
  6. try:
  7.     from secrets import randbelow
  8.     rndgen=lambda x:randbelow(x+1)
  9. except:
  10.     from random import randint
  11.     rndgen=lambda x:randint(0,x)
  12. def bx(code):
  13.     s=[]
  14.     matches={}
  15.     ifs=[]
  16.     ifm={}
  17.     tape=[0]*1000000
  18.     r=0
  19.     for i,j in enumerate(code):
  20.         if j=='[':
  21.             s.append(i)
  22.         if j==']':
  23.             m=s.pop()
  24.             matches[m]=i
  25.             matches[i]=m
  26.         if j=='?':
  27.             ifs.append(i)
  28.         if j==':':
  29.             ifm[ifs[-1]]=i
  30.         if j=='\'':
  31.             k=ifs.pop()
  32.             ifm[ifm[k]]=k
  33.             ifm[k]=(ifm[k],i)
  34.     cp=0
  35.     p=0
  36.     while cp<len(code):
  37.         cmd=code[cp]
  38.         if cmd=='/':
  39.             tape[p]=(tape[p]+1)%256
  40.         if cmd=='\\':
  41.             tape[p]=(tape[p]-1)%256
  42.         if cmd==',':
  43.             c=sys.stdin.read(1)
  44.             tape[p]=(ord(c) if c else 0)%256
  45.         if cmd=='.':
  46.             print(chr(tape[p]),end='')
  47.         if cmd=='<':
  48.             p-=1
  49.         if cmd=='>':
  50.             p+=1
  51.         if cmd=='[':
  52.             if not tape[p]:
  53.                 cp=matches[cp]
  54.         if cmd==']':
  55.             if tape[p]:
  56.                 cp=matches[cp]
  57.         if cmd=='@':
  58.             r=tape[p]
  59.         if cmd=='%':
  60.             tape[p]=r
  61.         if cmd=='~':
  62.             r,tape[p]=tape[p],r
  63.         if cmd=='+':
  64.             r=(r+tape[p])%256
  65.         if cmd=='-':
  66.             r=(r-tape[p])%256
  67.         if cmd=='*':
  68.             r=(r*tape[p])%256
  69.         if cmd=='|':
  70.             r=int(r>tape[p])
  71.         if cmd=='&':
  72.             r&=tape[p]
  73.         if cmd=='^':
  74.             r|=tape[p]
  75.         if cmd=='!':
  76.             r=(~r)&255
  77.         if cmd==';':
  78.             r=rndgen(r)
  79.         if cmd=='(':
  80.             tape[p]=int(input().strip())%256
  81.         if cmd==')':
  82.             print(tape[p])
  83.         if cmd=='{':
  84.             tape[p] = int(input().strip(),16) % 256
  85.         if cmd=='}':
  86.             print('{:2F}'.format(tape[p]))
  87.         if cmd=='_':
  88.             tape[p]=int(code[cp+1]+code[cp+2],16)
  89.             cp+=2
  90.         if cmd=='$':
  91.             cp+=1
  92.             tp=p
  93.             while code[cp]!='$':
  94.                 tape[tp]=ord(code[cp])&255
  95.                 tp+=1
  96.                 cp+=1
  97.             tape[tp]=0
  98.         if cmd=='#':
  99.             cp+=1
  100.             while code[cp]!='#':
  101.                 cp+=1
  102.         if cmd=='?':
  103.             x,y=ifm[cp]
  104.             if not tape[p]:
  105.                 cp=x
  106.         if cmd==':':
  107.             x,y=ifm[ifm[cp]]
  108.             cp=y
  109.         cp+=1
  110. bx(input())
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement