Advertisement
tripl3dogdare

Roadrunner Interpreter v1.0

Sep 12th, 2015
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. # Brainf*ck Clone Interpreter v1.0
  2. # Created by Connor Scialdone
  3. #
  4. # Language Implemented:
  5. #   Name: Roadrunner
  6. #   Version: 1.0
  7. #   Author: Connor Scialdone
  8. #
  9. #   More Info:
  10. #       Roadrunner was a stupid idea that ended up turning into
  11. #       a full-on Brainf*ck clone interpreter in which I
  12. #       implemented the language. So yeah!
  13. #
  14. #       It's based off the popular Looney Tunes character by the
  15. #       same name; all the commands are just "meep" with different
  16. #       capitalization.
  17. #
  18. #   More documentation is maintained here: https://esolangs.org/wiki/Roadrunner
  19.  
  20. # Syntax list
  21. # Each entry is stored by the Brainf*ck equivalent and contains
  22. #   the translated token.
  23. # 'sep' is the token separator; note that it can be empty (if it is, tokens are assumed to be single characters).
  24. syntax = {
  25.     '>': 'meeP',
  26.     '<': 'Meep',
  27.     '+': 'mEEp',
  28.     '-': 'MeeP',
  29.     '.': 'MEEP',
  30.     ',': 'meep',
  31.     '[': 'mEEP',
  32.     ']': 'MEEp',
  33.     'sep': ' ',
  34. }
  35.  
  36. # Interprets a program in the clone language
  37. # prog: String containing program to interpret
  38. def terp(prog):
  39.     tape,p,t = [0],0,-1
  40.  
  41.     try: prog = prog.split(syntax['sep'])
  42.     except ValueError: pass
  43.  
  44.     # Simple function to skip tokens until reaching a specific one
  45.     # t: Current token pointer
  46.     # end: Token to end if found (i.e. ']')
  47.     # inc: Amount to increment (use negative to go backwards)
  48.     # Returns updated token pointer (int)
  49.     def skip(t,end,inc):
  50.         while prog[t] != end: t += inc
  51.         return t
  52.  
  53.     while t+1 < len(prog):
  54.         try:
  55.             t,token = t+1,prog[t+1]
  56.  
  57.             if token == syntax['>']: p += 1
  58.             if token == syntax['<']: p -= 1
  59.             if token == syntax['+']: tape[p] += 1
  60.             if token == syntax['-']: tape[p] -= 1
  61.             if token == syntax['.']: print(chr(tape[p]),end='')
  62.             if token == syntax[',']: tape[p] = ord(input('')[0])
  63.             if token == syntax['['] and tape[p] == 0: t = skip(t,syntax[']'],1)
  64.             if token == syntax[']'] and tape[p] != 0: t = skip(t,syntax['['],-1)
  65.            
  66.             if len(tape) <= p: tape.append(0)
  67.         except IndexError:
  68.             if p < 0: print('Cannot decrement pointer beyond 0')
  69.             else:
  70.                 print('Unknown error...')
  71.             raise
  72.  
  73. # Compiles a Brainf*ck program into the clone language
  74. # prog: Brainf*ck program to compile
  75. # Returns compiled program (string)
  76. def comp(prog):
  77.     prog,new = list(prog),''
  78.     for c in prog:
  79.         if c in syntax: new += syntax[c]+syntax['sep']
  80.     return new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement