unpack arguments
This commit is contained in:
parent
5ced180990
commit
2a3907eef7
52
src/ram.py
52
src/ram.py
@ -51,25 +51,25 @@ class Ram(object):
|
|||||||
|
|
||||||
def op(self, type_op, r1, r2, r3):
|
def op(self, type_op, r1, r2, r3):
|
||||||
if type_op == 'add':
|
if type_op == 'add':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
value_3 = value_1 + value_2
|
value_3 = value_1 + value_2
|
||||||
self.write_register(value_3, r3)
|
self.write_register(value_3, *r3)
|
||||||
elif type_op == 'sub':
|
elif type_op == 'sub':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
value_3 = value_1 - value_2
|
value_3 = value_1 - value_2
|
||||||
self.write_register(value_3, r3)
|
self.write_register(value_3, *r3)
|
||||||
elif type_op == 'div':
|
elif type_op == 'div':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
value_3 = value_1 / value_2
|
value_3 = value_1 / value_2
|
||||||
self.write_register(value_3, r3)
|
self.write_register(value_3, *r3)
|
||||||
elif type_op == 'mul':
|
elif type_op == 'mul':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
value_3 = value_1 * value_2
|
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):
|
def op_ctrl(self, type_op, z, r1=None, r2=None):
|
||||||
if type_op == 'jump':
|
if type_op == 'jump':
|
||||||
@ -78,17 +78,37 @@ class Ram(object):
|
|||||||
else:
|
else:
|
||||||
raise ValueError("wrong operand type (should be integer)")
|
raise ValueError("wrong operand type (should be integer)")
|
||||||
if type_op == 'je':
|
if type_op == 'je':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
if value_1 == value_2:
|
if value_1 == value_2:
|
||||||
self.op_ctrl('jump', z)
|
self.op_ctrl('jump', z)
|
||||||
else:
|
else:
|
||||||
self.current += 1
|
self.current += 1
|
||||||
if type_op == 'jl':
|
if type_op == 'jl':
|
||||||
value_1 = self.read_register(r1)
|
value_1 = self.read_register(*r1)
|
||||||
value_2 = self.read_register(r2)
|
value_2 = self.read_register(*r2)
|
||||||
if value_1 > value_2:
|
if value_1 > value_2:
|
||||||
self.op_ctrl('jump', z)
|
self.op_ctrl('jump', z)
|
||||||
else:
|
else:
|
||||||
self.current += 1
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user