Advertisement
Guest User

Brainfuck to Shakespeare

a guest
Sep 6th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.08 KB | None | 0 0
  1. import sys
  2.  
  3. #Use like the following:
  4. #   this-program.py bf-program.bf spl-program.spl
  5. #
  6. #Alternatively, if you don't provide the output name, or either name, the program will prompt you.
  7.  
  8. try:
  9.     BFprogramName = sys.argv[1]
  10. except:
  11.     BFprogramName = input("Enter name of brainfuck program: ")
  12.  
  13. with open(BFprogramName,"r") as f:
  14.     BFprogram = f.read()
  15.  
  16.  
  17. def roman(a):
  18.     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)))]))
  19.  
  20. header = """Brainfuck.
  21.  
  22. Lennox, the number of entries in the stack.
  23. Page, the memory pointer.
  24. Shallow, a temporary variable that tells how deep in the stack to go.
  25. Julius Caesar, another temporary variable.
  26. King Henry, the "real" stack.
  27. King John, who holds a part of the stack as necessary.
  28.  
  29. Act I: Setup of the stack and variables.
  30.  
  31. Scene I: The stack will be 24 entries long in this example; change what Julius Caesar says to extend it.
  32.  
  33. [Enter Julius Caesar and Lennox]
  34.  
  35. Julius Caesar:
  36. Thou art the factorial of a big huge stone wall!
  37.  
  38. Lennox:
  39. You are as huge as me.
  40.  
  41. [Exit Lennox]
  42. [Enter King Henry]
  43.  
  44. Scene II: Producing the stack with a loop.
  45.  
  46. Julius Caesar:
  47. Remember nothing!
  48.  
  49. King Henry:
  50. Thou art as stupid as the sum of thyself and thy death!
  51.  
  52. Julius Caesar:
  53. Am I as cunning as a pig? If not, we must return to Scene II.
  54.  
  55. Act II: The rest of the program.
  56.  
  57. Scene I: This scene must be here if your program doesn't start with a bracket.
  58.  
  59. """
  60. getStack = """[Exeunt]
  61. [Enter Lennox and Shallow]
  62.  
  63. Lennox:
  64. You are the difference between myself and the sum of Page and the Devil!
  65.  
  66. Scene @: Retrieve the correct number in the stack with a loop.
  67.  
  68. [Exeunt]
  69. [Enter King Henry and King John]
  70.  
  71. King John:
  72. Recall!
  73.  
  74. King Henry:
  75. Remember me!
  76.  
  77. [Exit King John]
  78. [Enter Shallow]
  79.  
  80. King Henry:
  81. Thou art the sum of thyself and the Devil!
  82.  
  83. Shallow:
  84. Am I as mighty as nothing? If not, we must return to Scene @.
  85.  
  86. """
  87. fixStack = """[Exeunt]
  88. [Enter Lennox and Shallow]
  89.  
  90. Lennox:
  91. You are the difference between myself and Page!
  92.  
  93. [Exeunt]
  94. [Enter King Henry and Julius Caesar]
  95.  
  96. King Henry:
  97. Thou art as noble as me.
  98.  
  99. [Exit Julius Caesar]
  100. [Enter King John]
  101.  
  102. King Henry:
  103. Recall!
  104.  
  105. King John:
  106. Remember thyself!
  107.  
  108. Scene #: Put the stack back together again with a loop.
  109.  
  110. [Exeunt]
  111. [Enter King Henry and King John]
  112.  
  113. King Henry:
  114. Recall!
  115.  
  116. King John:
  117. Remember me!
  118.  
  119. [Exit King John]
  120. [Enter Shallow]
  121.  
  122. King Henry:
  123. Thou art the sum of thyself and the Devil!
  124.  
  125. Shallow:
  126. Am I as mighty as nothing? If not, we must return to Scene #.
  127.  
  128. """
  129.  
  130. BAK = """[Exeunt]
  131. [Enter King Henry and Page]
  132.  
  133. King Henry:
  134. 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.
  135.  
  136. """
  137. FWD = """[Exeunt]
  138. [Enter King Henry and Page]
  139.  
  140. King Henry:
  141. 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.
  142.  
  143. """
  144. INC = getStack + """Shallow:
  145. 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.
  146.  
  147. """ + fixStack
  148. DEC = getStack + """Shallow:
  149. 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.
  150.  
  151. """ + fixStack
  152. IN = getStack + """Shallow:
  153. Open your mind!
  154.  
  155. """ + fixStack
  156. OUT = getStack + """Shallow:
  157. Speak your mind!
  158.  
  159. """ + fixStack
  160. IF = """Scene $: If the current value is 0, skip to the matching right bracket.
  161.  
  162. """ + getStack + fixStack + """[Exeunt]
  163. [Enter Julius Caesar]
  164.  
  165. Julius Caesar:
  166. Am I as cunning as nothing? If so, we must proceed to scene %.
  167.  
  168. """
  169. EIF = """Scene $: If the current value is not 0, jump back to the matching left bracket.
  170.  
  171. """ + getStack + fixStack + """[Exeunt]
  172. [Enter Julius Caesar]
  173.  
  174. Julius Caesar:
  175. Am I as cunning as nothing? If not, we must return to scene %.
  176.  
  177. """
  178.  
  179. SPLprogram = header
  180. if BFprogram[0] == "[":
  181.     SPLprogram += """[Exeunt]
  182.  
  183. """
  184.  
  185. ifPositions = []
  186. eifPositions = []
  187. sceneCount = 1
  188.  
  189. for i in range(len(BFprogram)):
  190.     if BFprogram[i] in "+-,.":
  191.         sceneCount += 2
  192.     elif BFprogram[i] == "[":
  193.         sceneCount += 1
  194.         ifPositions.append(sceneCount)
  195.         sceneCount += 2
  196.         ifCount = 1
  197.         newSceneCount = sceneCount
  198.         j = i + 1
  199.         while not (j >= len(BFprogram) or ifCount == 0):
  200.             if BFprogram[j] in "+-,.":
  201.                 newSceneCount += 2
  202.             elif BFprogram[j] == "[":
  203.                 newSceneCount += 3
  204.                 ifCount += 1
  205.             elif BFprogram[j] == "]":
  206.                 newSceneCount += 3
  207.                 ifCount -= 1
  208.             j += 1
  209.         eifPositions.append(newSceneCount-2)
  210.     elif BFprogram[i] == "]":
  211.         sceneCount += 3
  212.  
  213. sceneCount = 1
  214. for i in BFprogram:
  215.     if i == "<":
  216.         SPLprogram += BAK
  217.     elif i == ">":
  218.         SPLprogram += FWD
  219.     elif i == "+":
  220.         SPLprogram += INC.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
  221.         sceneCount += 2
  222.     elif i == "-":
  223.         SPLprogram += DEC.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
  224.         sceneCount += 2
  225.     elif i == ",":
  226.         SPLprogram += IN.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
  227.         sceneCount += 2
  228.     elif i == ".":
  229.         SPLprogram += OUT.replace("@",roman(sceneCount+1)).replace("#",roman(sceneCount+2))
  230.         sceneCount += 2
  231.     elif i == "[":
  232.         command = IF.replace("$",roman(sceneCount+1))
  233.         command = command.replace("@",roman(sceneCount+2))
  234.         command = command.replace("#",roman(sceneCount+3))
  235.         command = command.replace("%",roman(eifPositions[ifPositions.index(sceneCount+1)]))
  236.         SPLprogram += command
  237.         sceneCount += 3
  238.     elif i == "]":
  239.         command = EIF.replace("$",roman(sceneCount+1))
  240.         command = command.replace("@",roman(sceneCount+2))
  241.         command = command.replace("#",roman(sceneCount+3))
  242.         command = command.replace("%",roman(ifPositions[eifPositions.index(sceneCount+1)]))
  243.         SPLprogram += command
  244.         sceneCount += 3
  245.  
  246. try:
  247.     programName = sys.argv[2]
  248. except:
  249.     programName = input("Enter name of output program: ")
  250.  
  251. print("Writing program...")
  252. with open(programName,"w") as f:
  253.     f.write(SPLprogram)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement