Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- #Use like the following:
- # this-program.py bf-program.bf spl-program.spl
- #
- #Alternatively, if you don't provide the output name, or either name, the program will prompt you.
- try:
- BFprogramName = sys.argv[1]
- except:
- BFprogramName = input("Enter name of brainfuck program: ")
- with open(BFprogramName,"r") as f:
- BFprogram = f.read()
- def roman(a):
- return "".join(reversed(["".join(["IVXLCDM"[int(d)+i*2]for d in["","0","00","000","01","1","10","100","1000","02"][int(c)]])for i,c in enumerate(reversed(str(a)))]))
- header = """Brainfuck.
- Lennox, the number of entries in the stack.
- Page, the memory pointer.
- Shallow, a temporary variable that tells how deep in the stack to go.
- Julius Caesar, another temporary variable.
- King Henry, the "real" stack.
- King John, who holds a part of the stack as necessary.
- Act I: Setup of the stack and variables.
- Scene I: The stack will be 24 entries long in this example; change what Julius Caesar says to extend it.
- [Enter Julius Caesar and Lennox]
- Julius Caesar:
- Thou art the factorial of a big huge stone wall!
- Lennox:
- You are as huge as me.
- [Exit Lennox]
- [Enter King Henry]
- Scene II: Producing the stack with a loop.
- Julius Caesar:
- Remember nothing!
- King Henry:
- Thou art as stupid as the sum of thyself and thy death!
- Julius Caesar:
- Am I as cunning as a pig? If not, we must return to Scene II.
- Act II: The rest of the program.
- Scene I: This scene must be here if your program doesn't start with a bracket.
- """
- getStack = """[Exeunt]
- [Enter Lennox and Shallow]
- Lennox:
- You are the difference between myself and the sum of Page and the Devil!
- Scene @: Retrieve the correct number in the stack with a loop.
- [Exeunt]
- [Enter King Henry and King John]
- King John:
- Recall!
- King Henry:
- Remember me!
- [Exit King John]
- [Enter Shallow]
- King Henry:
- Thou art the sum of thyself and the Devil!
- Shallow:
- Am I as mighty as nothing? If not, we must return to Scene @.
- """
- fixStack = """[Exeunt]
- [Enter Lennox and Shallow]
- Lennox:
- You are the difference between myself and Page!
- [Exeunt]
- [Enter King Henry and Julius Caesar]
- King Henry:
- Thou art as noble as me.
- [Exit Julius Caesar]
- [Enter King John]
- King Henry:
- Recall!
- King John:
- Remember thyself!
- Scene #: Put the stack back together again with a loop.
- [Exeunt]
- [Enter King Henry and King John]
- King Henry:
- Recall!
- King John:
- Remember me!
- [Exit King John]
- [Enter Shallow]
- King Henry:
- Thou art the sum of thyself and the Devil!
- Shallow:
- Am I as mighty as nothing? If not, we must return to Scene #.
- """
- BAK = """[Exeunt]
- [Enter King Henry and Page]
- King Henry:
- Thou art the sum of thyself and the Devil! Art thou worse than nothing? If so, thou art as good as the sum of Lennox and the Devil.
- """
- FWD = """[Exeunt]
- [Enter King Henry and Page]
- King Henry:
- Thou art the sum of thyself and the Lord! Art thou better than the sum of Lennox and the Devil? If so, thou art nothing.
- """
- INC = getStack + """Shallow:
- You are the sum of yourself and a king! Are you better than the sum of a brave brave brave brave brave brave brave brave hero and a coward? If so, you are nothing.
- """ + fixStack
- DEC = getStack + """Shallow:
- You are the sum of yourself and a plague! Are you worse than nothing? If so, you are the sum of a brave brave brave brave brave brave brave brave hero and a coward.
- """ + fixStack
- IN = getStack + """Shallow:
- Open your mind!
- """ + fixStack
- OUT = getStack + """Shallow:
- Speak your mind!
- """ + fixStack
- IF = """Scene $: If the current value is 0, skip to the matching right bracket.
- """ + getStack + fixStack + """[Exeunt]
- [Enter Julius Caesar]
- Julius Caesar:
- Am I as cunning as nothing? If so, we must proceed to scene %.
- """
- EIF = """Scene $: If the current value is not 0, jump back to the matching left bracket.
- """ + getStack + fixStack + """[Exeunt]
- [Enter Julius Caesar]
- Julius Caesar:
- Am I as cunning as nothing? If not, we must return to scene %.
- """
- SPLprogram = header
- if BFprogram[0] == "[":
- SPLprogram += """[Exeunt]
- """
- ifPositions = []
- eifPositions = []
- sceneCount = 1
- for i in range(len(BFprogram)):
- if BFprogram[i] in "+-,.":
- sceneCount += 2
- elif BFprogram[i] == "[":
- sceneCount += 1
- ifPositions.append(sceneCount)
- sceneCount += 2
- ifCount = 1
- newSceneCount = sceneCount
- j = i + 1
- while not (j >= len(BFprogram) or ifCount == 0):
- if BFprogram[j] in "+-,.":
- newSceneCount += 2
- elif BFprogram[j] == "[":
- newSceneCount += 3
- ifCount += 1
- elif BFprogram[j] == "]":
- newSceneCount += 3
- ifCount -= 1
- j += 1
- eifPositions.append(newSceneCount-2)
- elif BFprogram[i] == "]":
- sceneCount += 3
- sceneCount = 1
- for i in BFprogram:
- if i == "<":
- SPLprogram += BAK
- elif i == ">":
- SPLprogram += FWD
- elif i == "+":
- SPLprogram += INC.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
- sceneCount += 2
- elif i == "-":
- SPLprogram += DEC.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
- sceneCount += 2
- elif i == ",":
- SPLprogram += IN.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
- sceneCount += 2
- elif i == ".":
- SPLprogram += OUT.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
- sceneCount += 2
- elif i == "[":
- command = IF.replace("$",roman(sceneCount+1))
- command = command.replace("@",roman(sceneCount+2))
- command = command.replace("#",roman(sceneCount+3))
- command = command.replace("%",roman(eifPositions[ifPositions.index(sceneCount+1)]))
- SPLprogram += command
- sceneCount += 3
- elif i == "]":
- command = EIF.replace("$",roman(sceneCount+1))
- command = command.replace("@",roman(sceneCount+2))
- command = command.replace("#",roman(sceneCount+3))
- command = command.replace("%",roman(ifPositions[eifPositions.index(sceneCount+1)]))
- SPLprogram += command
- sceneCount += 3
- try:
- programName = sys.argv[2]
- except:
- programName = input("Enter name of output program: ")
- print("Writing program...")
- with open(programName,"w") as f:
- f.write(SPLprogram)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement