-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservusSymbolTable.py
214 lines (182 loc) · 6.95 KB
/
servusSymbolTable.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'''
Symbol Table of the ServusBasic Programming Language
Author: Sebastian Rivera Gonzalez
Date: 08/OCT/2018
'''
import sys
class Symbol:
def __init__(self, name, type=float, r=1, c=1):
self.val = 0.0
self.name = name
self.type = type
self.rows = r
self.cols = c
# TODO Add one or to lists to store all the elements of the array or matrix
if(type == str):
self.val = ""
elif(type == float):
self.val = 0.0
if r > 1:
tVal = self.val
self.val = []
for i in range(self.rows):
self.val.append(tVal)
if c > 1:
tVal = self.val
self.val = []
for i in range(self.cols):
self.val.append(tVal.copy())
def printSymbol(self):
if self.rows == 1 and self.cols == 1:
print(self.name, "\t", self.type, "\t", self.rows, "\t", self.cols, "\t", self.val)
else:
print(self.name, "\t", self.type, "\t", self.rows, "\t", self.cols, "\n")
for r in range(self.rows):
print(self.val[r])
# TODO Reise error when trying to set a value outside of the valid range, i.e. i or j >= rows or cols
def setValue(self, val, i=1, j=1):
# numeric = {int, float}
# Retrieve the real value of val if necessary, i.e. val not in numeric
# if type(val) not in numeric:
# val = val.getValue()
# Three cases for setting the value:
# Normal variable
if self.rows == 1 and self.cols == 1:
self.val = val
else:
# Reise error if i or j are out of range
if i < 0 or i >= self.rows or j < 0 or j >= self.rows:
sys.exit("Reference out of range when indexing! i=%d, j=%d" % (i,j))
# 1D array
if self.rows > 1 and self.cols == 1:
self.val[int(i)] = val
# 2D matrix
elif self.rows > 1 and self.cols > 1:
self.val[int(i)][int(j)] = val
# TODO Same as the one from the function above
def getValue(self, i=1, j=1):
# Three cases for getting the value:
# Normal variable
if self.rows == 1 and self.cols == 1:
return self.val
else:
# Reise error if i or j are out of range
if i < 0 or i >= self.rows or j < 0 or j >= self.rows:
sys.exit("Reference out of range when indexing! i=%d, j=%d" % (i,j))
# 1D array
if self.rows > 1 and self.cols == 1:
return self.val[int(i)]
# 2D matrix
elif self.rows > 1 and self.cols > 1:
return self.val[int(i)][int(j)]
def printValue(self):
if self.rows > 1 and self.cols > 1:
for i in range(self.rows):
print(self.val[i])
else:
print(self.val)
class SymbolTable:
def __init__(self):
self.symbols = {}
def stripVar(self, s):
"""
RETURN VALUES:
- Normal variable: name(of the variable), 1, 1
- 1D Array: name, string(index) | # of rows, 1
- 2D Matrix: name, string(index) | # of rows, string(index) | # of cols
"""
# TODO Reise error if an element of more than two dimensions is declared
name = ""
row = 1
str_row = ""
col = 1
str_col = ""
index = 0
state = 0
# 3 states - 0 := name, 1 := row, 2 := col; change concatenation target
while index < len(s):
tC = s[index]
index += 1
if tC == '[':
state += 1
elif tC == ']':
pass
else:
if state == 0:
name += tC
elif state == 1:
str_row += tC
elif state == 2:
str_col += tC
if str_row != "":
try:
# Variable declaration
row = int(str_row)
except:
# Assign a new value
row = str_row
if str_col != "":
try:
col = int(str_col)
except:
col = str_col
# print(name, type(row), type(col))
return name, row, col
def addElements(self, varList, tType):
if(tType == "wort"):
tType = str
for elem in varList:
newName, newRow, newCol = self.stripVar(elem)
if newName not in self.symbols:
newVar = Symbol(newName, tType, newRow, newCol)
self.symbols[newName] = newVar
def getSymbolFromTable(self, varName):
tName, tRows, tCols = self.stripVar(varName)
# print(tName)
if self.symbols.__contains__(tName):
return self.symbols[tName]
else:
return None
def purifyRowsAndCols(self, tR, tC):
"""
Convert the indexes used to access a matrix or array element from
vars to an integer value.
"""
tRows = tR
tCols = tC
if type(tRows) == str: # Assignation using an index, e.g. i varriable
tRows = self.getValue(tRows)
if type(tCols) == str: # Assignation using an index, e.g. j variable
tCols = self.getValue(tCols)
return tRows, tCols
def getValue(self, varName):
numeric = {int, float}
# If a varName is a numeric constant, return the value and stop the processing
if type(varName) in numeric:
return varName
# Once here, for sure varName is a str, so begin the process.
tName, tRows, tCols = self.stripVar(varName)
# print(tName)
tRows, tCols = self.purifyRowsAndCols(tRows, tCols)
# tSymbol = self.getSymbolFromTable(tName) # Reference to Symbol on the SymbolTable
tSymbol = self.symbols.get(tName)
if tSymbol != None: # Verify that the variable exists already
# tSymbol.printSymbol()
return tSymbol.getValue(tRows, tCols)
else:
# print("No symbol found")
return None
def setValue(self, varName, newVal):
tName, tRows, tCols = self.stripVar(varName)
tRows, tCols = self.purifyRowsAndCols(tRows, tCols)
# print(tName, tRows, tCols)
symb = self.getSymbolFromTable(tName)
symb.setValue(newVal, tRows, tCols)
def displayTable(self):
print("\n###################################################################")
print("####################### TABLE OF SYMBOLS ##########################")
print("###################################################################\n")
print("Name\t\tType\tRows\tCols\tValue")
for key, val in self.symbols.items():
val.printSymbol()
print("\n###################################################################\n")