-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.py
82 lines (75 loc) · 3.17 KB
/
Stack.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
from Class import Class
class Stack:
def __init__(self , globalVariables):
self.stack = []
self.globalVariables = []
for var in globalVariables:
self.globalVariables.append(Var(var['id'],var['type']))
def getVar(self, name):
# Searching in Local Scope of function
for i in range(len(self.stack)-1,-1,-1):
if self.stack[i].name == name:
return self.stack[i]
# Searching in Class Properties ( if the function is defined in Class )
this = None
for i in range(len(self.stack) - 1, -1, -1):
if self.stack[i].name == "this":
this = self.stack[i]
break
if this != None:
c = Class.searchClass(this.type)
if c is None:
raise Exception("Class with name " + this.type + " not exists !")
if c.variableExists(name):
var = c.getVariable(name)
return Var(var['name'] , var['type'])
# Searching in Global Scope
for i in range(len(self.globalVariables)-1,-1,-1):
if self.globalVariables[i].name == name:
return self.globalVariables[i]
raise Exception("No such variable name : " + name)
def getAddress(self , name):
# Searching in Local Scope of function
for i in range(len(self.stack)-1,-1,-1):
if self.stack[i].name == name:
code = "addi $s7 , $fp , " + str((len(self.stack) - i - 1) * 4) + "\n"
return code
# Searching in Class Properties ( if the function is defined in Class )
this = None
offsetThis = None
for i in range(len(self.stack) - 1, -1, -1):
if self.stack[i].name == "this":
this = self.stack[i]
offsetThis = len(self.stack) - i - 1
break
if this != None:
c = Class.searchClass(this.type)
if c is None:
raise Exception("Class with name " + this.type + " not exists !")
if c.variableExists(name):
offset = c.variableOffset(name)
code = "# Loading Class Variable : " + name + "\n"
code += "addi $s7 , $fp , " + str(offsetThis * 4) + "\n"
code += "lw $s7 , 0($s7)\n"
code += "addi $s7 , $s7 , " + str(offset) + " # add offset of " + name + " from base object pointer\n"
return code
# Searching in Global Scope
for i in range(len(self.globalVariables)-1,-1,-1):
if self.globalVariables[i].name == name:
code = "# Loading Global Variable : " + name + "\n"
code += "la $s7 , data_" + name + "\n"
return code
raise Exception("No such variable name : " + name)
def push(self , var):
self.stack.append(var)
def pop(self , size):
for i in range(size):
self.stack.pop()
def log(self):
pprint.pprint("---------------------")
for var in self.stack:
pprint.pprint(var.name + " - " + var.type)
class Var:
def __init__(self, name, var_type):
self.name = name
self.type = var_type