print each step format

This commit is contained in:
Sam Hadow 2024-04-10 22:16:41 +02:00
parent 0e8d507e36
commit 38813b42f2
2 changed files with 28 additions and 4 deletions

View File

@ -1 +0,0 @@

View File

@ -98,15 +98,40 @@ class Ram(object):
else:
self.current += 1
def __register_to_str(register):
if register[0] == '@':
return f'{register[3]}@{register[2]}{register[1]}'
elif register[0] == 'value':
return f'{register[1]}'
else:
return f'{register[0]}{register[1]}'
def __instr_to_str(self, instr_number):
instr = self.instr[instr_number]
op = instr["args"][0]
number = f'{instr_number} :'
instr_str = f' {number: <6}{op}('
if op == 'JUMP':
instr_str += f'{instr["args"][1]})'
elif op == 'JL' or op == 'JE':
instr_str += f'{instr["args"][1]}, '
instr_str += f'{Ram.__register_to_str(instr["args"][2])}, '
instr_str += f'{Ram.__register_to_str(instr["args"][3])})'
else:
instr_str += f'{Ram.__register_to_str(instr["args"][1])}, '
instr_str += f'{Ram.__register_to_str(instr["args"][2])}, '
instr_str += f'{Ram.__register_to_str(instr["args"][3])})'
return instr_str
def execute(self):
'''
Run machine to completion
'''
while self.current < len(self.instr):
print(self.current+2)
#print(*self.instr[self.current]['args'])
instr_str = self.__instr_to_str(self.current)
self.instr[self.current]['op'](self, *self.instr[self.current]['args'])
print(self.work_registers)
print(f'{instr_str: <28}{self.work_registers}')
def generate_graph(self):
'''