/ interpriter.py
interpriter.py
 1  import time
 2  import sys
 3  import lineExecuter as le
 4  import globalVar as gv
 5  
 6  if sys.platform.startswith('win'):
 7      import msvcrt
 8  
 9      def read_char():
10          char = msvcrt.getch().decode()
11          print(char, end='', flush=True)
12          return char
13  
14  else:
15      import tty
16      import termios
17  
18      def read_char():
19          fd = sys.stdin.fileno()
20          old_settings = termios.tcgetattr(fd)
21          try:
22              tty.setraw(fd)
23              ch = sys.stdin.read(1)
24          finally:
25              termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
26          print(ch, end='', flush=True)
27          return ch
28  
29  def write_fixed_line(filename, line_num, value):
30      with open(filename, 'r+b') as file:
31          offset = line_num * 9  # 8 bits + newline
32          file.seek(offset)
33          file.write((value + '\n').encode())
34  
35  
36  
37  def createMemoryFile(bytes):
38      with open("memory.bhm", "w") as file:
39          for i in range(bytes):
40              file.write("00000000\n")
41  line_executer = le.executor()
42  def interprit(code, arg=None, o44=False):
43      gv.code=code
44      if arg is not None:
45          gv.memory[int("10000000",2)] = arg
46      newCode=code.split("\n")
47      while gv.currentLine < len(newCode):
48          if o44==True:
49              gv.currentLine=0
50          time.sleep(0.01)
51          line = newCode[gv.currentLine]
52          line_executer.execute_line(line)