forked from yesusbc/MaeStar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadruple.py
97 lines (77 loc) · 3.48 KB
/
quadruple.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
88
89
90
91
92
93
94
95
96
97
import pickle
# Avail
avail = ['T'+str(i) for i in range(1, 100)]
avail_dict = dict()
class Quadruple:
"""
The Quadruple object contains the list of quadruples and helper functions
"""
def __init__(self):
self.quadruple_number = 0
self.quadruple_dict = dict()
def square(self, op_code, operand1, operand2, result):
self.quadruple_dict[self.quadruple_number] = [op_code, operand1, operand2, result]
self.quadruple_number += 1
def get_num(self):
return self.quadruple_number
def get_avail():
"""
:return: Return the list of avails used
"""
global avail_dict
print("\n\n"+"*"*20 + "\n" + "Avail Dict: " + "\n"+"*"*20)
print(avail_dict)
avail_dict = dict.fromkeys(avail_dict, 0)
with open('availDict', 'wb') as file_handler:
pickle.dump(avail_dict, file_handler)
print("Avail Dictionary imported correctly")
def generate_quadruple(p, square_obj, operand_stack):
"""
:param p: p object, obtaine from Yacc
:param square_obj: square_obj of class Quadruple
:param operand_stack: a list, representing the stack of operands
:return: a generated quadruple
"""
operand2 = operand_stack.pop() # op2 = pop operand stack
operand1 = operand_stack.pop() # op1 = pop operand stack
temporal = avail.pop(0) # Tr = Temporal avail
avail_dict[temporal] = operand1 + operand2 # Tr = op1 + op2
# op_code, operand1, operand2, result
square_obj.square(p[2], operand1, operand2, temporal) # Save square
operand_stack.append(temporal) # push to operand stack Tr
# If op1 or op2 are temporal, return them to avail
# if operand1 in avail_dict or operand2 in avail_dict:
# avail.append(temporal)
# del avail_dict[temporal]
def generate_quadruple_for(p, square_obj, operand_stack):
"""
:param p: p object, obtaine from Yacc
:param square_obj: square_obj of class Quadruple
:param operand_stack: a list, representing the stack of operands
:return: a generated quadruple specifically for For Loop
"""
operand2 = operand_stack.pop() # op2 = pop operand stack
operand1 = operand_stack.pop() # op1 = pop operand stack
temporal = avail.pop(0) # Tr = Temporal avail
avail_dict[temporal] = operand1 + operand2 # Tr = op1 + op2
# op_code, operand1, operand2, result
square_obj.square(p[2], operand1, operand2, temporal) # Save square
square_obj.square('gotoF', temporal, '_', '_')
# If op1 or op2 are temporal, return them to avail
# if operand1 in avail_dict or operand2 in avail_dict:
# avail.append(temporal)
# del avail_dict[temporal]
def generate_quadruple_inc_dec(p, square_obj, operand_stack):
"""
:param p: p object, obtaine from Yacc
:param square_obj: square_obj of class Quadruple
:param operand_stack: a list, representing the stack of operands
:return: a generated quadruple specifically for ++ / --
"""
operand2 = '1' # op2 = pop operand stack
operand1 = operand_stack.pop() # op1 = pop operand stack
temporal = avail.pop(0) # Tr = Temporal avail
avail_dict[temporal] = operand1 + operand2 # Tr = op1 + op2
# op_code, operand1, operand2, result
square_obj.square(p[2], operand1, operand2, temporal) # Save square
square_obj.square('=', temporal, '_', operand1) # Save square