From 2a3907eef727d0d9d524af661d5e1fd4c3d242e1 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Mon, 25 Mar 2024 12:19:58 +0100 Subject: [PATCH] unpack arguments --- src/ram.py | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/ram.py b/src/ram.py index 02d1ad9..d0aceaa 100644 --- a/src/ram.py +++ b/src/ram.py @@ -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)