-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproofOfconcept.py
More file actions
74 lines (54 loc) · 1.98 KB
/
proofOfconcept.py
File metadata and controls
74 lines (54 loc) · 1.98 KB
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
import dis
class Interpreter:
def __init__(self):
self.stack = []
self.enviorment = {}
def LOAD_VALUE(self, value):
self.stack.append(value)
def STORE_NAME(self, name):
val = self.stack.pop()
self.enviorment[name] = val
def LOAD_NAME(self, name):
val = self.enviorment[name]
self.stack.append(val)
def parse_argument(self, instruction, argument, what_to_run):
numbers = ["LOAD_VALUE"]
names = ["LOAD_VALUE","STORE_NAME"]
if instruction in numbers:
argument = what_to_run["numbers"][argument]
elif instruction in names:
argument = what_to_run["names"][argument]
return argument
def PRINT_ANSWER(self):
ans = self.stack.pop()
print(ans)
def ADD_TWO_VALUES(self):
first_num = self.stack.pop()
second_num = self.stack.pop()
total = first_num + second_num
self.stack.append(total)
def execute(self, what_to_run):
instructions = what_to_run["instructions"]
for each_step in instructions:
instruction, argument = each_step
argument = self.parse_argument(instructions,argument,what_to_run)
bytecode_method = getattr(self,instruction)
if argument is None:
bytecode_method()
else:
bytecode_method(argument)
interpreter = Interpreter()
what_to_run = {
"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"]
}
interpreter.execute(what_to_run)
print(dis.dis(interpreter.execute))