Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Brainf*ck Clone Interpreter v1.0
- # Created by Connor Scialdone
- #
- # Language Implemented:
- # Name: Roadrunner
- # Version: 1.0
- # Author: Connor Scialdone
- #
- # More Info:
- # Roadrunner was a stupid idea that ended up turning into
- # a full-on Brainf*ck clone interpreter in which I
- # implemented the language. So yeah!
- #
- # It's based off the popular Looney Tunes character by the
- # same name; all the commands are just "meep" with different
- # capitalization.
- #
- # More documentation is maintained here: https://esolangs.org/wiki/Roadrunner
- # Syntax list
- # Each entry is stored by the Brainf*ck equivalent and contains
- # the translated token.
- # 'sep' is the token separator; note that it can be empty (if it is, tokens are assumed to be single characters).
- syntax = {
- '>': 'meeP',
- '<': 'Meep',
- '+': 'mEEp',
- '-': 'MeeP',
- '.': 'MEEP',
- ',': 'meep',
- '[': 'mEEP',
- ']': 'MEEp',
- 'sep': ' ',
- }
- # Interprets a program in the clone language
- # prog: String containing program to interpret
- def terp(prog):
- tape,p,t = [0],0,-1
- try: prog = prog.split(syntax['sep'])
- except ValueError: pass
- # Simple function to skip tokens until reaching a specific one
- # t: Current token pointer
- # end: Token to end if found (i.e. ']')
- # inc: Amount to increment (use negative to go backwards)
- # Returns updated token pointer (int)
- def skip(t,end,inc):
- while prog[t] != end: t += inc
- return t
- while t+1 < len(prog):
- try:
- t,token = t+1,prog[t+1]
- if token == syntax['>']: p += 1
- if token == syntax['<']: p -= 1
- if token == syntax['+']: tape[p] += 1
- if token == syntax['-']: tape[p] -= 1
- if token == syntax['.']: print(chr(tape[p]),end='')
- if token == syntax[',']: tape[p] = ord(input('')[0])
- if token == syntax['['] and tape[p] == 0: t = skip(t,syntax[']'],1)
- if token == syntax[']'] and tape[p] != 0: t = skip(t,syntax['['],-1)
- if len(tape) <= p: tape.append(0)
- except IndexError:
- if p < 0: print('Cannot decrement pointer beyond 0')
- else:
- print('Unknown error...')
- raise
- # Compiles a Brainf*ck program into the clone language
- # prog: Brainf*ck program to compile
- # Returns compiled program (string)
- def comp(prog):
- prog,new = list(prog),''
- for c in prog:
- if c in syntax: new += syntax[c]+syntax['sep']
- return new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement