unpack arguments

This commit is contained in:
Sam Hadow 2024-03-25 12:19:58 +01:00
parent 5ced180990
commit 2a3907eef7

View File

@ -51,25 +51,25 @@ class Ram(object):
def op(self, type_op, r1, r2, r3):
if type_op == 'add':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
value_3 = value_1 + value_2
self.write_register(value_3, r3)
self.write_register(value_3, *r3)
elif type_op == 'sub':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
value_3 = value_1 - value_2
self.write_register(value_3, r3)
self.write_register(value_3, *r3)
elif type_op == 'div':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
value_3 = value_1 / value_2
self.write_register(value_3, r3)
self.write_register(value_3, *r3)
elif type_op == 'mul':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
value_3 = value_1 * value_2
self.write_register(value_3, r3)
self.write_register(value_3, *r3)
def op_ctrl(self, type_op, z, r1=None, r2=None):
if type_op == 'jump':
@ -78,17 +78,37 @@ class Ram(object):
else:
raise ValueError("wrong operand type (should be integer)")
if type_op == 'je':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
if value_1 == value_2:
self.op_ctrl('jump', z)
else:
self.current += 1
if type_op == 'jl':
value_1 = self.read_register(r1)
value_2 = self.read_register(r2)
value_1 = self.read_register(*r1)
value_2 = self.read_register(*r2)
if value_1 > value_2:
self.op_ctrl('jump', z)
else:
self.current += 1
def execute(self):
while self.current < len(self.instr):
print(self.current)
print(*self.instr[self.current]['args'])
self.instr[self.current]['op'](self, *self.instr[self.current]['args'])
### example
input_registers = [10, 5]
instructions = [
{'op': Ram.op, 'args': ('add', ('input', 0), ('value', 1), ('output', 2))},
]
ram = Ram(instructions, input_registers)
ram.execute()
print("Result:", ram.output_registers)