/ lineExecuter.py
lineExecuter.py
  1  import time
  2  import globalVar as gv
  3  import sys
  4  import threading
  5  if sys.platform.startswith('win'):
  6      import msvcrt
  7  
  8      def read_char():
  9          char = msvcrt.getch().decode()
 10          print(char, end='', flush=True)
 11          return char
 12  
 13  else:
 14      import tty
 15      import termios
 16  
 17      def read_char():
 18          fd = sys.stdin.fileno()
 19          old_settings = termios.tcgetattr(fd)
 20          try:
 21              tty.setraw(fd)
 22              ch = sys.stdin.read(1)
 23          finally:
 24              termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
 25          print(ch, end='', flush=True)
 26          return ch
 27  
 28  class executor:
 29      def init():
 30          pass
 31          
 32      def threadIT(self,  startLine, endLine, code):
 33          newCode = code[startLine:endLine]
 34          threading.Thread(target=self.newThread, args=(newCode, )).start()
 35  
 36      def newThread(self,  code):
 37          current_line=0
 38          while current_line < len(code.splitlines()):
 39              line = code[current_line]
 40              self.execute_line(line)
 41      def execute_line(self,  line):
 42          if len(" ".join(line)) < 7:
 43              gv.currentLine+=1
 44              return False
 45          line = line.split(" ")
 46          if line[0] == "00000001":
 47              gv.memory[int(line[1], 2)] = gv.memory[int(line[2], 2)]
 48          elif line[0] == "00000010":
 49              gv.memory[int(line[1], 2)] = "0"+bin(ord(read_char()))[2:]
 50          elif line[0] == "00000011":
 51              address = int(line[1], 2)
 52              print(chr(int(gv.memory[address],2)), end='', flush=True)
 53          elif line[0] == "00000100":
 54              gv.memory[int(line[1],2)] = line[2]
 55  
 56          # mathamatical operations
 57          elif line[0] == "00010100":
 58              gv.memory[int(line[1],2)] = int(gv.memory[int(line[2],2)],2) + int(gv.memory[int(line[3],2)],2)
 59          elif line[0] == "00011100":
 60              gv.memory[int(line[1],2)] = int(gv.memory[int(line[2],2)],2) * int(gv.memory[int(line[3],2)],2)
 61          elif line[0] == "00011000":
 62              gv.memory[int(line[1],2)] = int(gv.memory[int(line[2],2)],2) - int(gv.memory[int(line[3],2)],2)
 63          elif line[0] == "00011010":
 64              gv.memory[int(line[1],2)] = int(gv.memory[int(line[2],2)],2) / int(gv.memory[int(line[3],2)],2)
 65          gv.currentLine+=1
 66  
 67          if line[0][0] == "1": #if statements
 68              if line[0] == "00100001":
 69                  if gv.memory[int(line[1], 2)] == gv.memory[int(line[2], 2)]:
 70                      gv.currentLine=int(line[3], 2)
 71                      print(f"goto {int(line[3], 2)}")
 72              elif line[0] == "00100010":
 73                  if gv.memory[int(line[1],2)] >= gv.memory[int(line[2],2)]:
 74                      gv.currentLine=int(line[3], 2)
 75              elif line[0] == "00100011":
 76                  if gv.memory[int(line[1],2)] <= gv.memory[int(line[2],2)]:
 77                      gv.currentLine=int(line[3], 2)
 78              elif line[0] == "00100110":
 79                  if gv.memory[int(line[1],2)] > gv.memory[int(line[2],2)]:
 80                      gv.currentLine=int(line[3], 2)
 81              elif line[0] == "00100111":
 82                  if gv.memory[int(line[1],2)] < gv.memory[int(line[2],2)]:
 83                      gv.currentLine=int(line[3], 2)
 84              elif line[0] == "00100101":
 85                  if gv.memory[int(line[1],2)] != gv.memory[int(line[2],2)]:
 86                      gv.currentLine=int(line[3], 2)
 87          elif line[0] == "00001010":  # reading a specific line from file
 88              target_line = int(gv.memory[int(line[1], 2)], 2)
 89              result = '00000000'  # default value if line not found
 90  
 91              with open("gv.memory.bhm", "r") as file:
 92                  for current_line_number, line_text in enumerate(file):
 93                      if current_line_number == target_line:
 94                          result = line_text.strip()
 95                          break  # stop reading file once we get our line
 96  
 97              gv.memory[int(line[2], 2)] = result
 98  
 99          elif line[0] == "00010101": #writing to gv.memory
100              whatToWrite=gv.memory[int(line[1], 2)]
101              where = int(line[2],2)
102              with open("gv.memory.bhm", "r") as file:
103                  file_contents = file.read().splitlines()
104  
105              file_contents[where] = whatToWrite
106  
107              with open("gv.memory.bhm", "w") as file:
108                  file.write("\n".join(file_contents) + "\n")
109          
110          elif line[0] == "00001111":
111              # fetch command token from gv.memory (could be str/int/char) and normalize to 8-bit binary string
112              raw_cmd = gv.memory[int(line[1], 2)]
113              if isinstance(raw_cmd, str) and all(c in '01' for c in raw_cmd) and len(raw_cmd) == 8:
114                  cmd_bin = raw_cmd
115              else:
116                  # try parse as binary-string, then as int, then as single-char fallback
117                  try:
118                      cmd_bin = format(int(str(raw_cmd), 2), '08b')
119                  except Exception:
120                      try:
121                          cmd_bin = format(int(raw_cmd), '08b')
122                      except Exception:
123                          cmd_bin = format(ord(str(raw_cmd)[0]), '08b')
124  
125              # collect and normalize args
126              arg_bins = []
127              for a in line[2:]:
128                  val = gv.memory[int(a, 2)]
129                  if isinstance(val, str) and all(c in '01' for c in val) and len(val) == 8:
130                      arg_bins.append(val)
131                  else:
132                      try:
133                          arg_bins.append(format(int(str(val), 2), '08b'))
134                      except Exception:
135                          try:
136                              arg_bins.append(format(int(val), '08b'))
137                          except Exception:
138                              arg_bins.append(format(ord(str(val)[0]), '08b'))
139  
140              command_line = " ".join([cmd_bin] + arg_bins)
141  
142              # run the constructed single-line command in isolation
143              saved_current = gv.currentLine
144              gv.currentLine = 0               # ensure nested run starts at its own beginning
145              self.interprit(command_line, o44=True)
146              gv.currentLine = saved_current   # restore outer execution point
147          elif line[0] == "01000000":
148              gv.currentLine = int(line[1], 2)
149          elif line[0] == "01011111": #threading
150              startLine=int(line[0],2)
151              endLine=int(line[1],2)
152              self.startTime=int(line[2],2)
153              self.threadIT(startLine,endLine,gv.code)
154          elif line[0] == "01001010":
155              time.sleep(int(line[1], 2))