Skip to content

added handle index functionality #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void Tree::printTree(Token* head, Token* prevToken){
}
if (head->getSibling() != nullptr && contains(equationOperators, head->getSibling()->getValue())) {
std::cout << " ----> ";
head = handleAssignment(head);
prevToken = nullptr;
head = handleAssignment(head);
prevToken = nullptr;
}
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
std::cout << "DECLARATION\n|\n|\n|\n|\nv\n";
Expand Down Expand Up @@ -89,7 +89,7 @@ void Tree::printTree(Token* head, Token* prevToken){
std::cout << "\n|\n|\n|\n|\nv\nDECLARATION";
}
}
std::cout << "\n|\n|\n|\n|\nv\n";
//std::cout << "\n|\n|\n|\n|\nv\n"; // ****** removed for test case 4
}
} else if (head->getType() == "IDENTIFIER") {
if (head->getSibling() != nullptr && head->getSibling()->getValue() == "=") {
Expand All @@ -98,6 +98,10 @@ void Tree::printTree(Token* head, Token* prevToken){
// This needs to break out to handleAssignment();
head = handleAssignment(head);
prevToken = nullptr;
} 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
std::cout << "ASSIGNMENT" << " ----> ";
head = handleAssignment(head);
std::cout << "\n|\n|\n|\n|\nv\n";
} else if (isFunction(head->getValue())){
ignore = false;
isCall = true;
Expand Down Expand Up @@ -166,10 +170,27 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo
return head->getSibling();
}

Token* Tree::handleIndex(Token * head, std::vector<Token*>& equationAsVec) {
while (head->getSibling() != nullptr && head->getSibling()->getType() != "R_BRACKET") {
head = head->getSibling(); // Should be getting L_BRACKET of the function first time through
equationAsVec.push_back(head);
head = head->getSibling();
}
if (head->getSibling()->getType() == "R_BRACKET") {
head = head->getSibling();
equationAsVec.push_back(head);
} else {
std::cout << "error incorrectly formatted index" << std::endl;
}
equationAsVec.push_back(head);
return head->getSibling();
}

Token* Tree::handleAssignment(Token* head) {
std::vector<Token*> equationAsVec;
Token* prev = nullptr;
isCall = isFunction(head->getValue());
bool isIndex = Tree::isIndex(head->getType());

if (head->getValue() != "(") {
equationAsVec.push_back(head);
Expand All @@ -194,6 +215,10 @@ Token* Tree::handleAssignment(Token* head) {
}

}
else if (isIndex) {
equationAsVec.push_back(head); // Function's name
head = handleIndex(head, equationAsVec);
}
// Process head as an identifier followed by '('
else if (head->getType() == "IDENTIFIER" && head->getSibling() != nullptr && head->getSibling()->getValue() == "(" && isCall) {
auto temp = prev;
Expand Down Expand Up @@ -349,3 +374,10 @@ bool Tree::isFunction(std::string tokenName){
}
return false;
}

bool Tree::isIndex(std::string headType) {
if(headType == "L_BRACKET") {
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class Tree {
bool contains(const std::vector<std::string>, std::string);
bool isFunction(std::string tokenName);
Token* handleAssignment(Token*);
Token* handleIndex(Token *, std::vector<Token*>&);
Token* handleFunction(Token *,std::vector<Token*>& equationAsVec, bool& isFunctionCall);
int getPrecedence(std::string op);
bool isIndex(std::string);
bool isOperator(std::string c);
std::vector<Token*> infixToPostfix(const std::vector<Token*> infix, bool isFunctionCall);

Expand Down