Skip to content

Commit c049ff8

Browse files
committed
added handle index functionality
1 parent 668e3ce commit c049ff8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

tree.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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|\nv\n";
@@ -89,7 +89,7 @@ void Tree::printTree(Token* head, Token* prevToken){
8989
std::cout << "\n|\n|\n|\n|\nv\nDECLARATION";
9090
}
9191
}
92-
std::cout << "\n|\n|\n|\n|\nv\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|\nv\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+
169189
Token* 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+
}

tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class Tree {
1616
bool contains(const std::vector<std::string>, std::string);
1717
bool isFunction(std::string tokenName);
1818
Token* handleAssignment(Token*);
19+
Token* handleIndex(Token *, std::vector<Token*>&);
1920
Token* handleFunction(Token *,std::vector<Token*>& equationAsVec, bool& isFunctionCall);
2021
int getPrecedence(std::string op);
22+
bool isIndex(std::string);
2123
bool isOperator(std::string c);
2224
std::vector<Token*> infixToPostfix(const std::vector<Token*> infix, bool isFunctionCall);
2325

0 commit comments

Comments
 (0)