@@ -25,8 +25,8 @@ void Tree::printTree(Token* head, Token* prevToken){
2525 }
2626 if (head->getSibling () != nullptr && contains (equationOperators, head->getSibling ()->getValue ())) {
2727 std::cout << " ----> " ;
28- head = handleAssignment (head);
29- prevToken = nullptr ;
28+ head = handleAssignment (head);
29+ prevToken = nullptr ;
3030 }
3131 } else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
3232 std::cout << " DECLARATION\n |\n |\n |\n |\n v\n " ;
@@ -89,7 +89,7 @@ void Tree::printTree(Token* head, Token* prevToken){
8989 std::cout << " \n |\n |\n |\n |\n v\n DECLARATION" ;
9090 }
9191 }
92- std::cout << " \n |\n |\n |\n |\n v\n " ;
92+ // std::cout << "\n|\n|\n|\n|\nv\n"; // ****** removed for test case 4
9393 }
9494 } else if (head->getType () == " IDENTIFIER" ) {
9595 if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " =" ) {
@@ -98,6 +98,10 @@ void Tree::printTree(Token* head, Token* prevToken){
9898 // This needs to break out to handleAssignment();
9999 head = handleAssignment (head);
100100 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 " ;
101105 } else if (isFunction (head->getValue ())){
102106 ignore = false ;
103107 isCall = true ;
@@ -166,10 +170,27 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo
166170 return head->getSibling ();
167171}
168172
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+
169189Token* Tree::handleAssignment (Token* head) {
170190 std::vector<Token*> equationAsVec;
171191 Token* prev = nullptr ;
172192 isCall = isFunction (head->getValue ());
193+ bool isIndex = Tree::isIndex (head->getType ());
173194
174195 if (head->getValue () != " (" ) {
175196 equationAsVec.push_back (head);
@@ -194,6 +215,10 @@ Token* Tree::handleAssignment(Token* head) {
194215 }
195216
196217 }
218+ else if (isIndex) {
219+ equationAsVec.push_back (head); // Function's name
220+ head = handleIndex (head, equationAsVec);
221+ }
197222 // Process head as an identifier followed by '('
198223 else if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" && isCall) {
199224 auto temp = prev;
@@ -349,3 +374,10 @@ bool Tree::isFunction(std::string tokenName){
349374 }
350375 return false ;
351376}
377+
378+ bool Tree::isIndex (std::string headType) {
379+ if (headType == " L_BRACKET" ) {
380+ return true ;
381+ }
382+ return false ;
383+ }
0 commit comments