Advertisement
PixelatedStarfish

THE interpreter

Jul 14th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.71 KB | None | 0 0
  1.  
  2. import random
  3. import sys
  4. import traceback
  5. import os
  6. # Translator Handles Everything interpreter in Python 3
  7.  
  8. helpDoc ='''
  9.  
  10. The (Translator Handles Everything) is a language by User:PixelatedStarfish in 2022.
  11. It is a bf derivative designed to handle arbitrary input and convert it into runnable Turing complete code.
  12. bf [sic] instructions are modified accordingly:
  13. *For easy conversion from arbitrary input.
  14. *For the elimination of errors.
  15. *For achieving the above while maintaining Turing completeness.
  16.  
  17. The purpose of this language is to interpret any data at all, and run it without error.
  18. Files are converted into a set of integers, one character at a time.
  19. Then, each integer is set to itself mod 10, and mapped to a command.
  20.  
  21. ==Computation==
  22. ===Memory===
  23. Commands operate on a tape of bytes in the range of 0 to 255.
  24. A byte value will wrap around so as not to exceed range.
  25. Bytes are modified by a pointer that can operate on one byte at a time.
  26. There are 65536 bytes on the tape.
  27. The tape is circular so byte 65535 is adjacent to byte 0.
  28. There is also a stack for storing branches, and a program counter to track the current command in execution.
  29.  
  30. ===Commands===
  31. 0 - Set the tape pointer to 0.
  32.  
  33. 1 - Increment tape pointer.
  34.  
  35. 2 - Decrement tape pointer.
  36.  
  37. 3 - Increment byte.
  38.  
  39. 4 - Decrement byte.
  40.  
  41. 5 - Take input as a string.
  42.     Write each character to the next byte and increment the pointer.
  43.  
  44. 6 - Output byte as ASCII character.
  45.  
  46. 7 - If the value of the current byte is zero, push the program counter on to the stack.
  47.     Otherwise, look for the next 8. If an eight is found, jump to it. If not, halt
  48.  
  49. 8 - If the stack is not empty and the value of the current byte is not 0,
  50.     set the program counter to the value at the top of the stack, and pop.
  51.     Otherwise, do nothing.
  52.  
  53. 9 - Set byte to a random value.
  54.  
  55. A string of numbers is not interpreted directly. That would not be any fun.
  56. However, bf commands are interpreted directly via a character substitution (> becomes e, < becomes f etc.)
  57.  
  58. ===Error Message===
  59.  
  60. If there is an error, this message will print:
  61.  
  62. NothingError: Oops! Something went wrong!
  63.  
  64. Following the message, print two new lines and any text the implementing language prints.
  65.  
  66. ==Using the Interpeter==
  67. ===Running===
  68. The official interpreter is written in Python3 and can take two inline arguments on the command line:
  69. *file name
  70. *debugger flag
  71.  
  72. If the file name is left out, it should ask for a file like so:
  73. Please give a source file:
  74. >
  75.  
  76. The debugger flag is -d and is used to run the debugger.
  77.  
  78. At the start of a program, print ''Running...'' on its own line:
  79. Running...
  80.  
  81. Print this message at halt:
  82. Program Terminated.
  83.  
  84. You can use the '-d' argument after the file argument for a debugger.
  85. You can also type 'help' in the file argument for a help document.
  86. Thank you for using the THE interpreter.
  87.  
  88. Also note that when a program takes input a right angle bracket and space should be printed on its own line:
  89. >
  90.  
  91. ===The Debugger===
  92. The debugger single steps through a program and prints information about the current step, including a section of the tape extending from five cells left of the current cell, to five cells right of the current cell.
  93. Output is printed before the debug information.
  94.  
  95.  
  96. Example with the current cell at 102:
  97. Current Instruct: 9
  98. Current Cell: 0
  99. Stack Top: 0
  100. Stack Length: 0
  101.  -72-  -0-  -0-  -0-  -0-  >102<  -0-  -0-  -0-  -0-  -0-
  102. It will then skip a line and print this prompt:
  103. -Push Enter or Return to Continue-
  104.  
  105. ===Help===
  106. If the interpreter is given the argument help instead of a filename, it will print a plain text compatible version of this document, followed by this prompt:
  107.  
  108. -Push Enter or Return to Exit-
  109. ==End of Help Doc==
  110.  
  111.  
  112. '''
  113.  
  114. def main():
  115.     try:
  116.  
  117.         if (len(sys.argv) > 1):
  118.             x = sys.argv[1]
  119.         else:
  120.             x = input("Please give a source file:\n")
  121.  
  122.         if (x == "help"):
  123.             print(helpDoc)
  124.             input("-Push Enter or Return to Exit-")
  125.             return
  126.  
  127.         execute(compile((x)), (len(sys.argv) == 3 and sys.argv[2] == "-d"))
  128.  
  129.     except(Exception):
  130.         print("\nNothingError: Oops! Something went wrong!\n\n")
  131.         print(traceback.format_exc())
  132.  
  133.     except(KeyboardInterrupt):
  134.         print("\nNothingError: Oops! Something went wrong!\n\n")
  135.         print(traceback.format_exc())
  136.  
  137.     finally:
  138.         print("\nProgram Terminated.\n\nYou can use the '-d' argument after the file argument for a debugger.\nYou can also type 'help' in the file argument for a help document.\nThank you for using the THE (Translator Handles Everything) interpreter.\n")
  139.  
  140.     return
  141.  
  142.  
  143.  
  144. #I think i can use string replacements to make this run bf code
  145. def compile(f):
  146.     #get the file path
  147.     f = os.path.abspath(f)
  148.  
  149.     s = ""
  150.  
  151.     with open(f) as StringCheese:
  152.         s = StringCheese.read()
  153.  
  154.     #bf substitutions
  155.     s = s.replace(">", "e")
  156.     s = s.replace("<", "f")
  157.     s = s.replace("+", "g")
  158.     s = s.replace("-", "h")
  159.     s = s.replace(",", "i")
  160.     s = s.replace(".", "j")
  161.     s = s.replace("[", "k")
  162.     s = s.replace("]", "l")
  163.  
  164.     #translating
  165.     out = ""
  166.     for c in s:
  167.         out += str((ord(c) % 10))
  168.     print("\nTranslated Code:\n" + out + "\n")
  169.     return out
  170.  
  171. def execute(innie, Debug):
  172.     #init
  173.     tape = []
  174.     stack = []
  175.     pointer = 0
  176.     pc = -1
  177.  
  178.  
  179.     for i in range(65356):
  180.         tape.append(0)
  181.        
  182.  
  183.     print("\nRunning...\n")
  184.     while (pc < len(innie) -1):
  185.  
  186.         pc = pc + 1
  187.         if (Debug):
  188.             if (len(stack) > 0):
  189.                 debug(innie[pc], pointer, stack[len(stack) - 1], len(stack), tape)
  190.             else:
  191.                 debug(innie[pc], pointer, 0, 0, tape)
  192.        
  193.  
  194.         if (innie[pc] == '0'):
  195.             pointer = 0 #Good!
  196.             continue
  197.         if (innie[pc] == '1'):
  198.             pointer = (pointer + 1) % 65356
  199.             continue
  200.         if (innie[pc] == '2'):
  201.             pointer = (pointer - 1) % 65356
  202.             continue
  203.         if (innie[pc] == '3'):
  204.             tape[pointer] =  (tape[pointer] + 1) % 256 #Good!
  205.             continue
  206.         if (innie[pc] == '4'):
  207.             tape[pointer] =  (tape[pointer] - 1) % 256 #Good!
  208.             continue
  209.         if (innie[pc] == '5'):
  210.             s = input("\n> ")
  211.             w = pointer
  212.             for i in s:
  213.                 tape[pointer] = (ord(i) % 256) #Good!
  214.                 pointer = pointer + 1
  215.             pointer = w
  216.             continue
  217.         if (innie[pc] == '6'):
  218.             c = chr(tape[pointer]) #Good!
  219.             print(c, end = "")
  220.             continue
  221.         if (innie[pc] == '7'): #Good!
  222.            
  223.             stack.append(pc)
  224.                
  225.             if (tape[pointer] == 0):
  226.                 i = search(innie, pc, 8)
  227.                 if ( i == -1):
  228.                     return
  229.                 else:
  230.                     pc = i
  231.             continue
  232.         if (innie[pc] == '8'):
  233.            
  234.             if (len(stack) > 0 and tape[pointer] != 0):
  235.                 pc = stack[len(stack) -1] -1
  236.                 stack.pop(len(stack) -1)
  237.                 continue
  238.             continue
  239.         if (innie[pc] == '9'):
  240.             tape[pointer] = random.randint(0, 255) #Good!
  241.             continue
  242.  
  243.        
  244.     return
  245.  
  246.  
  247. def search(arr, curr_index, key):
  248.     while (curr_index < len(arr)):
  249.         if arr[curr_index] == key:
  250.             return curr_index
  251.         else:
  252.             curr_index = curr_index + 1
  253.     return -1
  254.    
  255.  
  256. def debug(CurrentInstruct, currentCell, stacktop, stackLen, tape):
  257.     print("\n\nCurrent Instruct: " + str(CurrentInstruct) + "")
  258.     print("Current Cell: " + str(currentCell) + "")
  259.     print("Stack Top: " + str(stacktop) + "")
  260.     print("Stack Length: " + str(stackLen) + "")
  261.  
  262.  
  263.     i = (currentCell - 6) % 65356
  264.     j = 0
  265.  
  266.     while (i != currentCell + 5 and j < 12):
  267.         i = (i + 1) % 65356
  268.         j = j + 1
  269.  
  270.         if (j > 11):
  271.             continue
  272.  
  273.         if ( i == currentCell):
  274.             print(" >" + str(tape[i]) + "< ", end = "")
  275.         else:
  276.             print(" -" + str(tape[i]) + "- ", end = "")
  277.     print("\n")
  278.     input("-Push Enter or Return to Continue-\n")  
  279.     return
  280.  
  281. main()
  282.  
  283. #Notes  
  284. #d = 0
  285. #e = 1
  286. #etc...
  287.  
  288. #a debug mode that shows whats going on, macrobeep style, would be nice
  289.  
  290. '''
  291. cat
  292. ,[.,]
  293.  
  294. ++++++++++>,<[>+.<.] looping counter
  295.  
  296. hw
  297. ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
  298.  
  299. nest
  300. [++[.-]]
  301.  
  302. '''
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement