@@ -30,60 +30,95 @@ void Table::build(Token* token, Token* prevToken, Entry* prevEntry) {
30
30
}
31
31
Entry* entry = prevEntry; // Start with the previous entry
32
32
33
- if (!pause && contains (prevToken->getValue ()) && token->getValue () != " {" && token->getValue () != " }" && token->getValue () != " (" && token->getValue () != " )" && token->getValue () != " [" && token->getValue () != " ]" ) {
34
- // Create a new entry based on the token values
35
- pause = true ;
33
+ // Checks if function isn't paused, if prevToken is reserved, and token isn't {,},(,),[,]
34
+ if (!pause && contains (prevToken->getValue ()) &&
35
+ token->getValue () != " {" && token->getValue () != " }" &&
36
+ token->getValue () != " (" && token->getValue () != " )" &&
37
+ token->getValue () != " [" && token->getValue () != " ]" ) {
38
+
39
+ pause = true ; // Pauses identifier creation until function parameters or array info is processed
40
+
36
41
if (prevToken->getValue () == " procedure" || prevToken->getValue () == " function" ) {
37
- scope++;
38
- if (prevToken->getValue () == " procedure" ) {
39
- exists (token, scope);
42
+ scope++;
43
+ inProcOrFuncScope = true ;
44
+
45
+ if (prevToken->getValue () == " procedure" ) { // If prevToken is a procedure
46
+ exists (token, scope);
47
+
48
+ // New entry created for this procedure
40
49
entry = new Entry (token->getValue (), prevToken->getValue (), " NOT APPLICABLE" , false , 0 , scope);
41
- if (prevEntry == nullptr ){
50
+
51
+ if (prevEntry == nullptr ){ //
42
52
head = entry;
43
53
}
44
- } else {
54
+ }
55
+ else { // If prevToken is a function
45
56
exists (token, scope);
57
+
58
+ // New entry created for this function
46
59
entry = new Entry (token->getSibling ()->getValue (), prevToken->getValue (), token->getValue (), false , 0 , scope);
47
- if (prevEntry == nullptr ){
60
+
61
+ if (prevEntry == nullptr ){ // If prevEntry is null, set head to entry
48
62
head = entry;
49
- } else {
50
- prevEntry->setNext (entry); // Update the next pointer of prevEntry
63
+ }
64
+ else {
65
+ prevEntry->setNext (entry); // Links entry to prevEntry
51
66
}
52
- return build (token->getSibling ()->getSibling (), token->getSibling (), entry );
67
+ // Recursive call to process the next sibling
68
+ return build (token->getSibling ()->getSibling (), token->getSibling (), entry);
53
69
}
54
- } else {
55
- exists (token, scope);
56
- entry = new Entry (token->getValue (), " datatype" , prevToken->getValue (), false , 0 , scope);
57
- if (prevEntry == nullptr ){
70
+ }
71
+ else { // prevToken is not a procedure or function
72
+ if (inProcOrFuncScope){
73
+ exists (token, scope);
74
+ entry = new Entry (token->getValue (), " datatype" , prevToken->getValue (), false , 0 , scope);
75
+ }
76
+ else {
77
+ exists (token, 0 );
78
+ entry = new Entry (token->getValue (), " datatype" , prevToken->getValue (), false , 0 , 0 );
79
+ }
80
+
81
+
82
+
83
+ if (prevEntry == nullptr ){
58
84
head = entry;
59
- }
60
- if (token->getSibling () != nullptr ) {
85
+ }
61
86
62
- if (token->getSibling ()->getValue () == " [" ) {
63
- if ( prevEntry != nullptr ) {
64
- prevEntry->setNext (entry); // Update the next pointer of prevEntry
87
+ if (token->getSibling () != nullptr ){ // If token has a sibling, process sibling token
88
+
89
+ if (token->getSibling ()->getValue () == " [" ){ // Indicates start of an array
90
+
91
+ if (prevEntry != nullptr ){
92
+ prevEntry->setNext (entry); // Links entry to prevEntry
65
93
}
66
- return setArray (token->getSibling (), entry);
67
- } else if (token->getSibling ()->getValue () == " ," ) {
68
- if ( prevEntry != nullptr ) {
69
- prevEntry->setNext (entry); // Update the next pointer of prevEntry
94
+
95
+ return setArray (token->getSibling (), entry); // Handles the array
96
+ }
97
+ else if (token->getSibling ()->getValue () == " ," ){ // Indicates an initalizer list for variables
98
+
99
+ if (prevEntry != nullptr ){
100
+ prevEntry->setNext (entry); // Links entry to prevEntry
70
101
}
71
- return handleInitList (prevToken->getValue (), token->getSibling ()->getSibling (), entry);
102
+
103
+ return handleInitList (prevToken->getValue (), token->getSibling ()->getSibling (), entry); // Handles initializer list
72
104
}
73
105
}
74
106
}
75
- if ( prevEntry != nullptr ) {
76
- prevEntry->setNext (entry); // Update the next pointer of prevEntry
107
+
108
+ if (prevEntry != nullptr ){
109
+ prevEntry->setNext (entry); // Update the next pointer of prevEntry
77
110
}
111
+
78
112
}
79
- // Check if it's a function or procedure again and handle parameters
80
- else if (pause == true ) {
81
- // Process parameters for procedure
113
+ else if (pause == true ){ // If true, we are processing parameters for a function or procedure
82
114
if (contains (prevToken->getValue ()) && token->getType () == " IDENTIFIER" ){
83
115
exists (token, scope);
116
+
117
+ // Entry created for a parameter
84
118
Entry *newEntry = new Entry (token->getValue (), " parameter" , prevToken->getValue (), false , 0 , scope);
85
- // Process for array parameters
86
- if (token->getSibling ()->getValue () == " [" ) {
119
+
120
+ // Processes an array parameter
121
+ if (token->getSibling ()->getValue () == " [" ){
87
122
token = token->getSibling ()->getSibling ();
88
123
newEntry->setIsArray ();
89
124
newEntry->setArray (stoi (token->getValue ()));
@@ -92,18 +127,26 @@ void Table::build(Token* token, Token* prevToken, Entry* prevEntry) {
92
127
}
93
128
}
94
129
130
+ // Checks for the ending of a function or procedure, or a '}'
131
+ if (token->getValue () == " }" ) {
132
+ pause = false ; // Resume normal processing
133
+ inProcOrFuncScope = false ;
134
+ }
135
+
95
136
// Recursively traverse siblings or children
96
- if (token->getSibling () != nullptr ) {
137
+ if (token->getSibling () != nullptr ){
97
138
build (token->getSibling (), token, entry); // Traverse sibling
98
- } else if (token->getChild () != nullptr ) {
139
+ }
140
+ else if (token->getChild () != nullptr ){
99
141
pause = false ;
100
142
build (token->getChild (), token, entry); // Traverse child
101
143
}
144
+
102
145
}
103
146
104
147
// Checks if the token is a reserved word
105
148
bool Table::contains (std::string token){
106
- for (int i = 0 ; i < reserved.size (); i++) {
149
+ for (int i = 0 ; i < reserved.size (); i++){
107
150
if (token == reserved.at (i)){
108
151
return true ;
109
152
}
@@ -114,24 +157,24 @@ bool Table::contains(std::string token){
114
157
// Checks if the token exists in the symbol table
115
158
void Table::exists (Token* token, int scope){
116
159
auto temp = head;
117
- while (temp != nullptr ) {
160
+ while (temp != nullptr ){
118
161
if (temp->getIDName () == token->getValue () && !contains (token->getValue ()) ){
119
- if (temp->getScope () == 0 ) {
162
+ if (temp->getScope () == 0 ){
120
163
std::cerr << " Error on line: " << token->getLineNumber () << " variable \" " << token->getValue () << " \" already defined globally\n " ;
121
164
exit (1 );
122
165
}
123
- if (temp->getScope () == scope) {
166
+ if (temp->getScope () == scope){
124
167
std::cerr << " Error on line: " << token->getLineNumber () << " variable \" " << token->getValue () << " \" already defined locally\n " ;
125
168
exit (1 );
126
169
}
127
170
}
128
- for (int i = 0 ; i < temp->parameters .size (); i++) {
171
+ for (int i = 0 ; i < temp->parameters .size (); i++){
129
172
if (temp->parameters .at (i)->getIDName () == token->getValue () && !contains (token->getValue ()) ){
130
- if (temp->parameters .at (i)->getScope () == 0 ) {
173
+ if (temp->parameters .at (i)->getScope () == 0 ){
131
174
std::cerr << " Error on line: " << token->getLineNumber () << " variable \" " << token->getValue () << " \" already defined globally\n " ;
132
175
exit (1 );
133
176
}
134
- if (temp->parameters .at (i)->getScope () == scope) {
177
+ if (temp->parameters .at (i)->getScope () == scope){
135
178
std::cerr << " Error on line: " << token->getLineNumber () << " variable \" " << token->getValue () << " \" already defined locally\n " ;
136
179
exit (1 );
137
180
}
@@ -149,11 +192,20 @@ void Table::setArray(Token* token, Entry* entry){
149
192
150
193
151
194
void Table::handleInitList (std::string type, Token* token, Entry* prevEntry){
152
- Entry *entry = new Entry (token->getValue (), " datatype" , type, false , 0 , scope);
195
+ Entry *entry;
196
+ if (inProcOrFuncScope){
197
+ entry = new Entry (token->getValue (), " datatype" , type, false , 0 , scope);
198
+ }
199
+ else {
200
+ entry = new Entry (token->getValue (), " datatype" , type, false , 0 , 0 );
201
+ }
202
+
153
203
prevEntry->setNext (entry); // Update the next pointer of prevEntry
154
- if (token->getSibling ()->getValue () == " ;" ) {
204
+
205
+ if (token->getSibling ()->getValue () == " ;" ){
155
206
return build (token->getSibling ()->getChild (), token->getSibling (), entry);
156
207
}
208
+
157
209
return handleInitList (type, token->getSibling ()->getSibling (), entry);
158
210
}
159
211
@@ -163,7 +215,7 @@ void Table::printTable(){
163
215
// make temp head pointer
164
216
Entry* tempHead = this ->head ;
165
217
166
- while (tempHead != nullptr ) {
218
+ while (tempHead != nullptr ){
167
219
std::cout << std::setw (colonWidth) << std::right << " IDENTIFIER_NAME: " << tempHead->getIDName () << std::endl;
168
220
std::cout << std::setw (colonWidth) << std::right << " IDENTIFIER_TYPE: " << tempHead->getIDType () << std::endl;
169
221
std::cout << std::setw (colonWidth) << std::right << " DATATYPE: " << tempHead->getDType () << std::endl;
@@ -184,8 +236,8 @@ void Table::printParameters(){
184
236
const int colonWidth = 25 ;
185
237
186
238
// Cycle through all entries in the table
187
- while (tempHead != nullptr ) {
188
- if (tempHead->parameters .size () > 0 ) { // If current entry has parameters...
239
+ while (tempHead != nullptr ){
240
+ if (tempHead->parameters .size () > 0 ){ // If current entry has parameters...
189
241
std::cout << std::setw (colonWidth) << std::right << " PARAMETER LIST FOR: " << tempHead->getIDName () << std::endl;
190
242
for (int i = 0 ; i < tempHead->parameters .size (); i++) { // Print each parameter
191
243
std::cout << std::setw (colonWidth) << std::right << " IDENTIFIER_NAME: " << tempHead->parameters .at (i)->getIDName () << std::endl;
0 commit comments