@@ -25,8 +25,8 @@ void Tree::printTree(Token* head, Token* prevToken){
25
25
}
26
26
if (head->getSibling () != nullptr && contains (equationOperators, head->getSibling ()->getValue ())) {
27
27
std::cout << " ----> " ;
28
- head = handleAssignment (head);
29
- prevToken = nullptr ;
28
+ head = handleAssignment (head);
29
+ prevToken = nullptr ;
30
30
}
31
31
} else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
32
32
std::cout << " DECLARATION\n |\n |\n |\n |\n v\n " ;
@@ -89,7 +89,7 @@ void Tree::printTree(Token* head, Token* prevToken){
89
89
std::cout << " \n |\n |\n |\n |\n v\n DECLARATION" ;
90
90
}
91
91
}
92
- std::cout << " \n |\n |\n |\n |\n v\n " ;
92
+ // std::cout << "\n|\n|\n|\n|\nv\n"; // ****** removed for test case 4
93
93
}
94
94
} else if (head->getType () == " IDENTIFIER" ) {
95
95
if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " =" ) {
@@ -98,6 +98,10 @@ void Tree::printTree(Token* head, Token* prevToken){
98
98
// This needs to break out to handleAssignment();
99
99
head = handleAssignment (head);
100
100
prevToken = nullptr ;
101
+ } else if (head->getSibling () != nullptr && isIndex (head->getSibling ()->getType ())){ // checks for "L_BRACKET" to see if assigning an index... if so print extra characters and resume as normal
102
+ std::cout << " ASSIGNMENT" << " ----> " ;
103
+ head = handleAssignment (head);
104
+ std::cout << " \n |\n |\n |\n |\n v\n " ;
101
105
} else if (isFunction (head->getValue ())){
102
106
ignore = false ;
103
107
isCall = true ;
@@ -166,10 +170,27 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo
166
170
return head->getSibling ();
167
171
}
168
172
173
+ Token* Tree::handleIndex (Token * head, std::vector<Token*>& equationAsVec) {
174
+ while (head->getSibling () != nullptr && head->getSibling ()->getType () != " R_BRACKET" ) {
175
+ head = head->getSibling (); // Should be getting L_BRACKET of the function first time through
176
+ equationAsVec.push_back (head);
177
+ head = head->getSibling ();
178
+ }
179
+ if (head->getSibling ()->getType () == " R_BRACKET" ) {
180
+ head = head->getSibling ();
181
+ equationAsVec.push_back (head);
182
+ } else {
183
+ std::cout << " error incorrectly formatted index" << std::endl;
184
+ }
185
+ equationAsVec.push_back (head);
186
+ return head->getSibling ();
187
+ }
188
+
169
189
Token* Tree::handleAssignment (Token* head) {
170
190
std::vector<Token*> equationAsVec;
171
191
Token* prev = nullptr ;
172
192
isCall = isFunction (head->getValue ());
193
+ bool isIndex = Tree::isIndex (head->getType ());
173
194
174
195
if (head->getValue () != " (" ) {
175
196
equationAsVec.push_back (head);
@@ -194,6 +215,10 @@ Token* Tree::handleAssignment(Token* head) {
194
215
}
195
216
196
217
}
218
+ else if (isIndex) {
219
+ equationAsVec.push_back (head); // Function's name
220
+ head = handleIndex (head, equationAsVec);
221
+ }
197
222
// Process head as an identifier followed by '('
198
223
else if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" && isCall) {
199
224
auto temp = prev;
@@ -349,3 +374,10 @@ bool Tree::isFunction(std::string tokenName){
349
374
}
350
375
return false ;
351
376
}
377
+
378
+ bool Tree::isIndex (std::string headType) {
379
+ if (headType == " L_BRACKET" ) {
380
+ return true ;
381
+ }
382
+ return false ;
383
+ }
0 commit comments