-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
88 lines (72 loc) · 2.11 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Interpreter:
def __init__(self):
self.environment = {}
self.stack = []
def LOAD_VALUE(self, value):
self.stack.append(value)
def STORE_NAME(self, name):
exp = self.stack.pop()
self.environment[name] = exp
def LOAD_NAME(self, name):
self.stack.append(self.environment[name])
def ADD_TWO_VALUES(self):
second = self.stack.pop()
first = self.stack.pop()
total = first + second
self.stack.append(total)
def PRINT_ANSWER(self):
answer = self.stack.pop()
print(answer)
def _parse_ptr(self, ptr, instruction, code_obj):
"""
Helper method to get the right value for
:ptr: associated with each instruction
"""
arg = None
numbers = ["LOAD_VALUE"]
names = ["STORE_NAME", "LOAD_NAME"]
if instruction in numbers:
arg = code_obj["numbers"][ptr]
elif instruction in names:
arg = code_obj["names"][ptr]
return arg
def execute(self, code_obj):
instruction_sets = code_obj.get("instructions")
for instruction_set in instruction_sets:
instruction, ptr = instruction_set
arg = self._parse_ptr(ptr, instruction, code_obj)
method = getattr(self, instruction)
if arg is None:
method()
else:
method(arg)
instruction_sets = [
{
"instructions": [
("LOAD_VALUE", 0),
("LOAD_VALUE", 1),
("ADD_TWO_VALUES", None),
("LOAD_VALUE", 2),
("ADD_TWO_VALUES", None),
("PRINT_ANSWER", None)
],
"numbers" : [6, 7, 8]
},
{
"instructions": [
("LOAD_VALUE", 0),
("STORE_NAME", 0),
("LOAD_VALUE", 1),
("STORE_NAME", 1),
("LOAD_NAME", 0),
("LOAD_NAME", 1),
("ADD_TWO_VALUES", None),
("PRINT_ANSWER", None)],
"numbers": [1, 2],
"names": ["a", "b"]
}
]
for instruction_set in instruction_sets:
interpreter = Interpreter()
interpreter.execute(instruction_set)
print(interpreter.environment)