-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTManager.py
More file actions
143 lines (113 loc) · 4.46 KB
/
Copy pathSTManager.py
File metadata and controls
143 lines (113 loc) · 4.46 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
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
"""
Symbol Table manager
"""
from SymbolTable import SymbolTable as ST
class STManager():
def __init__(self):
# create the root symbol table
self.root = ST()
# intialize the stack of symbol tables for activation record
self.activeSTs = [self.root]
# table of funcitons
self.ftable = {};
""" Make Symbol Table
create a new table and return a pointer to new table
@params _prev {SymbolTable} -- parent symbol table pointer
@return {SymbolTable} -- newly created symbol table
"""
def makeTable(self, _prev):
# create a new symbol table table
newST = ST()
# make _prev the parent of new table
newST.parent = _prev
# return the new symbol table
return newST
""" Lookup
Loopup for the symbol in the activation record
@params _symbol {string} -- symbol for which look to be done
@return {bool} -- symbol found or not
"""
def lookup(self, _symbol):
return self.currActive.search(_symbol)
""" Lookup In Root for a symbol. Used to lookup for globals and functions
Loopup for the symbol in the activation record
@params _symbol {string} -- symbol for which look to be done
@return {bool} -- symbol found or not
"""
def lookupInRoot(self, _symbol):
return self.root.search(_symbol)
""" Push
push symbol table onto symbol tables stack
@params _st {SymbolTable} -- symbol table to be pushed onto the stack
"""
def push(self, _st):
# push the new symbol on activation record's stack
self.activeSTs.append(_st)
""" Pop
remove the top of the symbol tables stack
@return {SymbolTable} -- removed symbol table
"""
def pop(self):
return self.activeSTs.pop()
""" Insert into symbol table
create a new entry into symbol table and put the data
@params _name {string} -- name (key | id) of the new entry
@params _type {string} -- type of name (an attribute)
@params _offset {integer} -- size | offset for the name
"""
def insert(self, _name, _type, _width, _scope=None):
return self.currActive.insert(_name, _type, _width, _scope)
""" Add link to global variable into the current active symbol table
"""
def linkGlobalSym(self, _name, _attrs):
self.currActive.linkGlobalSym(_name, _attrs)
""" Enter a new entry for a procedure
Create a new entry for a procedure.
@params _name {string} -- name of the procedure
@params _lineNumber {int} -- liner number where function is defined
@params _procST {SymbolTable} -- symbol table for a procedure
"""
def enterProc(self, _name, _lineNumber, _numParams, _procST):
self.ftable[_lineNumber] = {
"place": _name,
"numParams": _numParams,
"st": _procST
}
return self.currActive.enterProc(_name, _lineNumber, _numParams, _procST)
""" Set attribute
set attributes for a symbol in symbol table
@params _symbol {string} -- symbol for which attribute is to be set
@params _key {string} -- key for the attribute
@params _val {object} -- value for the attribute
"""
def setAttr(self, _symbol, _key, _val):
self.currActive.setAttr(_symbol, _key, _val)
""" Get attribute
get attribute for a symbol in symbol table
@params _symbol {string} -- symbol for which attribute should be fetched
@params _key {string} -- attribute key
@return {object} -- value of attribute for key if found else None
"""
def getAttr(self, _symbol, _key):
return self.currActive.getAttr(_symbol, _key)
""" Get all the attributes
get attribute for a symbol in symbol table
@params _symbol {string} -- symbol for which attribute should be fetched
@return {object} -- value of attribute for key if found else None
"""
def getAttrs(self, _symbol):
return self.currActive.getAttrs(_symbol)
""" Current Active Symbol Table
(getter) get the current active symbol table. Just a convenient method
"""
@property
def currActive(self):
# return the top
return self.activeSTs[-1]
""" Insert all the keywords
insert the keywords into the root of the symbol table
so that they can be easily accessible
"""
def insertKeywords(self):
return None