-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualMachine.py
More file actions
131 lines (122 loc) · 4.64 KB
/
Copy pathVirtualMachine.py
File metadata and controls
131 lines (122 loc) · 4.64 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sys
class VirtualMachine:
def __init__(self, quadruples):
self.quadruples = quadruples
self.memory = {}
self.output = []
def declare_vars(self, declarations):
for var in declarations:
var_type, var_name = var
if var_type == 'int':
self.memory[var_name] = 0
elif var_type == 'float':
self.memory[var_name] = 0.0
elif var_type == 'string':
self.memory[var_name] = ""
def build(self):
pc = 0
while pc < len(self.quadruples):
op, arg1, arg2, res = self.quadruples[pc]
# ASSIGN
if op == '=':
# We make sure the variable has already been declared
if self.memory.get(res) is not None:
var_type = type(self.memory.get(res))
# We make sure they're both the same type
if var_type == type(arg1):
# Assign new value to variable
self.memory[res] = arg1
else:
sys.exit(f'ERROR: variable {res} and value {arg1} are not the same type.')
else:
sys.exit(f'ERROR: variable {res} was not declared.')
# LESS THAN
elif op == '<':
# First check if the value to assign has been declared or is a temp variable
if self.memory.get(res) is not None:
# Variable exists
self.memory[res] = arg1 < arg2
else:
# Variable doesn't exist, check if temp variable
if res.startswith('T'):
# Temp value
self.memory.update({res: arg1 < arg2})
else:
sys.exit(f'ERROR: variable {res} was not declared.')
# GREATER THAN
elif op == '>':
# First check if the value to assign has been declared or is a temp variable
if self.memory.get(res) is not None:
# Variable exists
self.memory[res] = arg1 > arg2
else:
# Variable doesn't exist, check if temp variable
if res.startswith('T'):
# Temp value
self.memory.update({res: arg1 > arg2})
else:
sys.exit(f'ERROR: variable {res} was not declared.')
# LESS THAN OR EQUALS
elif op == '<=':
# First check if the value to assign has been declared or is a temp variable
if self.memory.get(res) is not None:
# Variable exists
self.memory[res] = arg1 <= arg2
else:
# Variable doesn't exist, check if temp variable
if res.startswith('T'):
# Temp value
self.memory.update({res: arg1 <= arg2})
else:
sys.exit(f'ERROR: variable {res} was not declared.')
# GREATER THAN OR EQUALS
elif op == '>=':
# First check if the value to assign has been declared or is a temp variable
if self.memory.get(res) is not None:
# Variable exists
self.memory[res] = arg1 >= arg2
else:
# Variable doesn't exist, check if temp variable
if res.startswith('T'):
# Temp value
self.memory.update({res: arg1 >= arg2})
else:
sys.exit(f'ERROR: variable {res} was not declared.')
# NOT EQUALS
# PLUS
# MINUS
# TIMES
# DIVIDE
# WRITELN
# GOTO F
# GOTO V
# GOTO
pc += 1
def run(self):
print(self.output)
# Op Arg1 Arg2 Res
quadruples = [
('writeln', '"texto dump"', None, None), # 0
('=', 5, None, 'f'), # 1
('=', 0, None, 'x'), # 2
('<', 'x', 'f', 'T1'), # 3
('gotoF', 'T1', None, 11), # 4
('gotoV', 'T1', None, 8), # 5
('+', 'i', 1, 'i'), # 6
('goto', None, None, 4), # 7
('+', 'x', 'f', 'T2'), # 8
# ('=', 'T2', None, 'n'), # 9
# ('goto', None, None, 7), # 10
# ('writeln', 'n', None, None), # 11
]
declarations = [
('int', 'i'),
('int', 'n'),
('int', 'f'),
('int', 'x')
]
vm = VirtualMachine(quadruples)
vm.declare_vars(declarations)
print(vm.memory)
vm.build()
print(vm.memory)