-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntermiadeCodeGenerator.py
52 lines (40 loc) · 1.4 KB
/
IntermiadeCodeGenerator.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
from typing import List
from Quad import Quad
class IntermiadeCodeGenerator():
latestQuadNumber = 0
latestTempNumber = 0
listOfQuads : List[Quad]
fileName = ""
def __init__(self, fileName) -> None:
self.listOfQuads = self.emptyList()
self.fileName = fileName.split('.')[0] + ".int"
open(self.fileName, 'w') #clear file
def nextQuad(self):
return self.latestQuadNumber + 1
def genQuad(self, op, x, y, z):
quad = Quad(self.nextQuad(), op, x, y, z)
self.latestQuadNumber += 1
self.listOfQuads.append(quad)
return quad
def newTemp(self):
temp = self.latestTempNumber
self.latestTempNumber += 1
return f"T_{temp}"
def emptyList(self):
return []
def makeList(self, x):
return [x]
def merge(self, p1, p2):
return p1 + p2
def backPatch(self, list, z):
for quadNumber in list:
matching_quad = next((quad for quad in self.listOfQuads if quad.number == quadNumber), None)
if matching_quad is not None:
matching_quad.z = z
def printAllQuads(self):
for quad in self.listOfQuads:
print(quad)
def outputToFile(self):
with open(self.fileName, 'w') as file:
for quad in self.listOfQuads:
file.write(str(quad) + "\n")