forked from Grooben/Not-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.py
60 lines (49 loc) · 1.74 KB
/
symtable.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
# Author : Macauley Scullion
# symtable.py
import errorhandling
# Symbol class - Name, Type, and Attribute
class Symbol:
def __init__(self, name, type, line, value = None):
self.name = name
self.type = type
self.value = value
self.line = line
# Symbol table array
symbol_table = []
# Operations of symbol table
# Free - remove all entries and free storage
def free():
symbol_table.clear()
print("Symbol table cleared")
# Lookup - search for a name and return pointer to entry
def lookup(name):
# Check if the symbol table is empty
if len(symbol_table) == 0:
print("Symbol table empty")
return False
# For each symbol in the symbol table, check if the parameter name matches the symbol name
else:
for sym in symbol_table:
if sym.name == name:
print("symbol found: name - " + name)
return sym
return False
# Insert - insert a name and return pointer to entry
def insert(name, type, line, value = None):
# Lookup function call to check for matches
if (lookup(name) == False):
new_entry = Symbol(name, type, line, value)
symbol_table.append(new_entry)
print("Symbol appended: " + name + ' ' + type)
else:
errorhandling.error_dup(name, type)
# Set_attribute - associate an attribute with a given entry
def set_attribute(sym, value):
sym.value = value
# Get_attribute - get an attribute associated with an entry
def get_attribute(sym):
return sym.value
# Print Symbol table
def printTable():
for sym in symbol_table:
print("Item: \n"" Type:" + sym.type + "\n"" Name:"+ sym.name + "\n"" Value:", sym.value, "\n"" Line:", sym.line)