Advertisement
none-None1

+-) interpreter

Jan 25th, 2024 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | Source Code | 0 0
  1. '''
  2. +-) interpreter in Python
  3.  
  4. +-) is an esolang. Since it has no I/O, the 'debug' argument is recommended to view the IP and the tape.
  5.  
  6. Commands:
  7. +: Increment current cell unless the previous instruction executed is its matching ), then jump to the matching ) if the current cell is zero or the next character is ).
  8. -: Decrement current cell unless the previous instruction executed is its matching ), then move the pointer to the right, then jump to the matching ) if the current cell is zero or the next character is ).
  9. ): Jump to the matching + (or -) if current cell is nonzero and the previous character is ).
  10.  
  11. More information see esolang wiki: https://esolangs.org/wiki/%2B-)
  12.  
  13. You can run any program by changing the last line, the default program is translated from the brainf program ++++++++[>++++++++<-]>+, which computes 65.
  14. '''
  15. def interpret_idb(code,debug=True):
  16.     stack=[]
  17.     matches={}
  18.     for i,j in enumerate(code):
  19.         if j!=')':
  20.             stack.append(i)
  21.         else:
  22.             if not stack:
  23.                 raise SyntaxError('Unmatched symbol')
  24.             a=stack.pop()
  25.             matches[i]=a
  26.             matches[a]=i
  27.     if stack:
  28.         raise SyntaxError('Unmatched symbol')
  29.     ip=0
  30.     p=0
  31.     prev_ip=-1
  32.     tape=[0,0,0]
  33.     while ip<len(code):
  34.         opcode=code[ip]
  35.         if debug:
  36.             print('IP: '+str(ip)+'\tCurrent character: '+code[ip]+'\tTape (before command): '+' '.join(map(lambda x:('>' if x[0]==p else ' ')+str(x[1]),enumerate(tape))))
  37.         if opcode=='+':
  38.             if prev_ip!=matches[ip]:
  39.                 tape[p]+=1
  40.             if tape[p]==0 or code[ip+1]==')':
  41.                 prev_ip=ip
  42.                 ip=matches[ip]
  43.                 continue
  44.             prev_ip=ip
  45.             ip+=1
  46.         elif opcode=='-':
  47.             if prev_ip!=matches[ip]:
  48.                 tape[p]-=1
  49.             p=(p+1)%3
  50.             if tape[p]==0 or code[ip+1]==')':
  51.                 prev_ip=ip
  52.                 ip=matches[ip]
  53.                 continue
  54.             prev_ip=ip
  55.             ip+=1
  56.         else:
  57.             if tape[p]!=0 and code[ip-1]==')':
  58.                 prev_ip=ip
  59.                 ip=matches[ip]
  60.                 continue
  61.             prev_ip=ip
  62.             ip+=1
  63.     if debug:
  64.         print('Result: '+' '.join(map(lambda x:('>' if x[0]==p else ' ')+str(x[1]),enumerate(tape))))
  65.     return (p,tape)
  66. interpret_idb('+)+)+)+)+)+)+)+)-)+)-)+)-)++)-)+)-)+)-)+)-)+)+)+)+)+)+)+)+)+)-)+)-)-)+)-)+)-))+)-)+)')
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement