Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. import sys
  2. import time
  3. import os
  4.  
  5. class _Getch:
  6. """Gets a single character from standard input. Does not echo to the
  7. screen."""
  8. def __init__(self):
  9. try:
  10. self.impl = _GetchWindows()
  11. except ImportError:
  12. self.impl = _GetchUnix()
  13.  
  14. def __call__(self): return self.impl()
  15.  
  16.  
  17. class _GetchUnix:
  18. def __init__(self):
  19. import tty, sys
  20.  
  21. def __call__(self):
  22. import sys, tty, termios
  23. fd = sys.stdin.fileno()
  24. old_settings = termios.tcgetattr(fd)
  25. try:
  26. tty.setraw(sys.stdin.fileno())
  27. ch = sys.stdin.read(1)
  28. finally:
  29. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  30. return ch
  31.  
  32.  
  33. class _GetchWindows:
  34. def __init__(self):
  35. import msvcrt
  36.  
  37. def __call__(self):
  38. import msvcrt
  39. return msvcrt.getch()
  40.  
  41.  
  42. getch = _Getch()
  43.  
  44. code = open("code.bf", "r").read()
  45.  
  46. code = ''.join([x for x in code if x in "<>+-,.[]#"])
  47.  
  48. if code[0] == "#":
  49. debug = True
  50. else:
  51. debug = False
  52.  
  53. code = code.replace("#","")
  54.  
  55. def bmap(s):
  56. st = []
  57. bm = {}
  58.  
  59. for i in range(len(s)):
  60. if s[i] == "[":
  61. st.append(i)
  62. elif s[i] == "]":
  63. a = st.pop()
  64. bm[a] = i
  65. bm[i] = a
  66.  
  67. return bm
  68.  
  69. bm = bmap(code)
  70.  
  71. clen = 10
  72. cells = [0]*clen
  73. cellsf = ""
  74. arr = [""]*clen
  75. pout = []
  76. ci = 0
  77. ptr = 0
  78.  
  79. if debug:
  80. while ci < len(code):
  81. ins = code[ci]
  82.  
  83. if ins == ">":
  84. ptr += 1
  85. if ptr == len(cells):
  86. cells += [0]
  87. elif ins == "<":
  88. if ptr <= 0:
  89. ptr = 0
  90. else:
  91. ptr -= 1
  92. elif ins == "+":
  93. cells[ptr] = (cells[ptr]+1)%255
  94. elif ins == "-":
  95. cells[ptr] -= 1
  96. if cells[ptr] == -1:
  97. cells[ptr] = 255
  98. elif ins == "[" and cells[ptr] == 0:
  99. ci = bm[ci]
  100. elif ins == "]" and cells[ptr] != 0:
  101. ci = bm[ci]
  102. elif ins == ".":
  103. pout.append(chr(cells[ptr]))
  104. elif ins == ",":
  105. print("Provide input.")
  106. cells[ptr] = ord(getch())
  107.  
  108. cellsf = "|"+'|'.join([str(x) for x in cells])+"|"
  109.  
  110. start = 0
  111. end = min([len(code),20])
  112. carr = [" "]*(end)
  113.  
  114.  
  115. if ci == 0:
  116. pass
  117. elif ci >= end:
  118. diff = ci - end
  119. end += diff
  120. start += diff
  121. elif ci < start:
  122. diff = start - ci
  123. start -= diff
  124. end -= diff
  125.  
  126. if ci-start == len(carr):
  127. start += 1
  128. end += 1
  129.  
  130. codef = "|"+code[start:end]+"|"
  131. print("Code tape:")
  132. print(codef)
  133. carr[ci-start] = "^"
  134. carr = " "+''.join(carr)+" "
  135. print(carr)
  136.  
  137. print("Memory tape: ")
  138. arr = [[" "]*len(str(x)) for x in cells]
  139. arr[ptr][0] = "^"
  140. arr = " "+' '.join([''.join(x) for x in arr])
  141.  
  142. #os.system("cls||clear")
  143. print(cellsf)
  144. print(arr)
  145. print("\n")
  146. print("Output: \n"+''.join(pout))
  147. print("\n")
  148. time.sleep(0.05)
  149.  
  150. ci += 1
  151. else:
  152. while ci < len(code):
  153. ins = code[ci]
  154.  
  155. if ins == ">":
  156. ptr += 1
  157. if ptr == len(cells):
  158. cells += [0]
  159. elif ins == "<":
  160. if ptr <= 0:
  161. ptr = 0
  162. else:
  163. ptr -= 1
  164. elif ins == "+":
  165. cells[ptr] = (cells[ptr]+1)%255
  166. elif ins == "-":
  167. cells[ptr] -= 1
  168. if cells[ptr] == -1:
  169. cells[ptr] = 255
  170. elif ins == "[" and cells[ptr] == 0:
  171. ci = bm[ci]
  172. elif ins == "]" and cells[ptr] != 0:
  173. ci = bm[ci]
  174. elif ins == ".":
  175. sys.stdout.write(chr(cells[ptr]))
  176. elif ins == ",":
  177. print("Provide input.")
  178. cells[ptr] = ord(getch())
  179.  
  180. ci += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement