-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table_class.py
63 lines (55 loc) · 1.79 KB
/
symbol_table_class.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
class SymbolTable:
"""
SymbolTable class is responsible for storing information about identifiers and constants
"""
def __init__(self):
"""
Initializer of SymbolTable class
Values
======
id (int) = Global id which acts as unique id for a symbol (identifier/constant)
symbol_table (dict) = Dictionary containing the actual symbol table
"""
self.id = 1
self.symbol_table = {}
def entry(self, value, type, typedata):
"""
Returns id in symbol table after making an entry
Params
======
value (string) = Value to be stored in symbol table (identifier/constant)
type (string) = Datatype of symbol
typedata (string) = Type of data (constant/variable)
Returns
=======
int: The id of the current entry in symbol table
"""
self.symbol_table[self.id] = [value, type, typedata]
self.id += 1
return self.id - 1
def get_by_id(self, id):
"""
Returns symbol table entry by integer unique id
Params
======
id (id) = Integer unique id of a symbol in the table
Returns
=======
list: [value, type, typedata], typedata = constant/variable
"""
return self.symbol_table.get(id, [None, None, None])
def get_by_symbol(self, value):
"""
Returns unique id of a given value
Params
======
value (string) = Value to be searched in the symbol table
Returns
=======
int: The unique id of the entry in symbol table
"""
id = -1
for ids, value_list in self.symbol_table.items():
if value_list[0] == value:
return ids
return id