Skip to content

Commit 677d69b

Browse files
Merge pull request #6 from Pip-Install-Party/holdenkea-develop
Tests 1 and 2 fully working, implemented function parameters and arra…
2 parents fedbefd + d412f18 commit 677d69b

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tree.x: main.o commentDFA.o tokenizer.o parser.o table.o tree.o
2-
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o tree.o -o tree.x
1+
tree.exe: main.o commentDFA.o tokenizer.o parser.o table.o tree.o
2+
g++ -std=c++17 -g main.o commentDFA.o tokenizer.o parser.o table.o tree.o -o tree.exe
33

44
main.o: main.cpp commentDFA.h tokenizer.h parser.h testFiles.h
55
g++ -std=c++17 -g main.cpp -o main.o -c
@@ -20,4 +20,4 @@ tree.o: tree.cpp tree.h
2020
g++ -std=c++17 -g tree.cpp -o tree.o -c
2121

2222
clean:
23-
rm -f tree.x *.o *.txt
23+
rm -f tree.exe *.o *.txt

tree.cpp

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void Tree::printTree(Token* head, Token* prevToken){
2020
ignore = false;
2121
std::cout << "RETURN";
2222
head = head->getSibling();
23-
}else if (head->getValue() == "procedure" || head->getValue() == "function") {
23+
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
2424
std::cout << "DECLARATION\n|\n|\n|\n|\nv\n";
2525
while(head->getSibling() != nullptr) {
2626
head = head->getSibling();
@@ -60,7 +60,7 @@ void Tree::printTree(Token* head, Token* prevToken){
6060
forCount++;
6161
}
6262

63-
}else if (contains(varTypes, prevToken->getValue())) {
63+
} else if (contains(varTypes, prevToken->getValue())) {
6464
ignore = true; // this was false but i think should be true
6565
std::cout << "DECLARATION";
6666
if (head->getSibling() != nullptr && head->getSibling()->getValue() == ",") {
@@ -72,7 +72,7 @@ void Tree::printTree(Token* head, Token* prevToken){
7272
}
7373
std::cout << "\n|\n|\n|\n|\nv\n";
7474
}
75-
} else if (head->getType() == "IDENTIFIER") {
75+
} else if (head->getType() == "IDENTIFIER") {
7676
if (head->getSibling() != nullptr && head->getSibling()->getValue() == "=") {
7777
ignore = false;
7878
std::cout << "ASSIGNMENT" << " ----> ";
@@ -107,24 +107,67 @@ bool Tree::contains(const std::vector<std::string> reserved, std::string type){
107107
return false;
108108
}
109109

110+
// Function that handles functions and single normal and single array parameters
111+
Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, bool &isFunctionCall) {
112+
head = head->getSibling(); // Should be getting L_PAREN of the function
113+
equationAsVec.push_back(head);
114+
isFunctionCall = true; // For infixToPostfix to know we want to eventually output () or []
115+
116+
while (head->getValue() != ")") {
117+
head = head->getSibling();
118+
// Array declaration check here, []
119+
if (head->getType() == "IDENTIFIER" && head->getSibling() != nullptr && head->getSibling()->getValue() == "[") {
120+
equationAsVec.push_back(head); // Identifier pushed back
121+
head = head->getSibling();
122+
123+
equationAsVec.push_back(head); // Starting '[' pushed back
124+
head = head->getSibling();
125+
126+
equationAsVec.push_back(head); // Array size or index here
127+
head = head->getSibling();
128+
129+
equationAsVec.push_back(head); // Ending ']' pusehd back
130+
}
131+
else if (head->getValue() != ")" && head->getValue() != "(") {
132+
equationAsVec.push_back(head); // Normal function parameter added ex.func(param)
133+
}
134+
}
135+
equationAsVec.push_back(head);
136+
return head->getSibling();
137+
}
138+
110139
Token* Tree::handleAssignment(Token* head) {
111140
std::vector<Token*> equationAsVec;
141+
bool isFunctionCall = false;
142+
112143
if (head->getValue() != "(") {
113144
equationAsVec.push_back(head);
114145
}
146+
115147
head = head->getSibling();
116148

117-
while(head->getValue() != ";" &&(contains(equationOperators, head->getValue()) || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) {
118-
equationAsVec.push_back(head);
149+
while(head->getValue() != ";" && (contains(equationOperators, head->getValue()) || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) {
150+
// Check for function call (identifier with a L_PAREN)
151+
if (head->getType() == "IDENTIFIER" && head->getSibling() != nullptr && head->getSibling()->getValue() == "(") {
152+
// Call to handleFunction since we encountered an identifier with a L_PAREN
153+
equationAsVec.push_back(head);
154+
head = handleFunction(head, equationAsVec, isFunctionCall);
155+
}
156+
else {
157+
equationAsVec.push_back(head);
158+
}
159+
119160
if (head ->getSibling() != nullptr) {
120161
head = head->getSibling();
121-
} else {
162+
}
163+
else {
122164
break;
123165
}
124166
}
125167

126168
// Convert infix to postfix
127-
std::vector<Token*> postFix = infixToPostfix(equationAsVec);
169+
std::vector<Token*> postFix = infixToPostfix(equationAsVec, isFunctionCall);
170+
128171
//std::cout << "Size<: " << postFix.size() << std::endl;
129172
for (int i = 0; i < postFix.size(); i++) {
130173
std::string tokenValue = postFix.at(i)->getValue();
@@ -154,7 +197,7 @@ bool Tree::isOperator(std::string c) {
154197
return c == "+" || c == "-" || c == "*" || c == "/" || c == "=";
155198
}
156199

157-
std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix) {
200+
std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool isFunctionCall) {
158201
std::stack<Token*> operators;
159202
std::vector<Token*> postfix;
160203

@@ -173,23 +216,40 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix) {
173216
}
174217
// If it's a left parenthesis, push it onto the stack
175218
else if (tokenType == "L_PAREN") {
176-
operators.push(t);
219+
if (isFunctionCall) {
220+
postfix.push_back(t); // Push '(' if it's part of a function call to output later
221+
} else {
222+
operators.push(t); // Processing a subexpression here (not a function)
223+
}
177224
}
178225
// If it's a right parenthesis, pop until the left parenthesis
179226
else if (tokenType == "R_PAREN") {
180-
while (!operators.empty() && operators.top()->getValue() != "(") {
181-
postfix.push_back(operators.top());
182-
operators.pop();
227+
if (isFunctionCall) {
228+
postfix.push_back(t); // Push ')' if it's part of a function call to output later
229+
} else {
230+
// Processing it as a regular subexpression, pop operators until '(' is found
231+
while (!operators.empty() && operators.top()->getValue() != "(") {
232+
postfix.push_back(operators.top());
233+
operators.pop();
234+
}
235+
if (!operators.empty()) {
236+
operators.pop();
237+
}
183238
}
184-
if (!operators.empty()) { // Pop the left parenthesis
185-
operators.pop();
239+
}
240+
// Add array brackets here if it is a function call, if not, it is a normal operator.
241+
else if (tokenType == "L_BRACKET" || tokenType == "R_BRACKET") {
242+
if (isFunctionCall) {
243+
postfix.push_back(t);
244+
} else {
245+
operators.push(t);
186246
}
187247
}
188248
// If it's an operator
189249
else if (isOperator(tokenValue) || tokenType == "GT_EQUAL" || tokenType == "LT_EQUAL" || tokenType == "GT" || tokenType == "LT" || tokenType == "BOOLEAN_EQUAL" || tokenType == "BOOLEAN_AND" || tokenType == "BOOLEAN_NOT") { // can code this to check for all explicit tokens like prev if checks above... can add other tokens to operators too
190250
// Pop all operators with higher or equal precedence from the stack
191251
while (!operators.empty() && operators.top()->getType() != "L_PAREN" &&
192-
getPrecedence(operators.top()->getValue()) >= getPrecedence(tokenValue)) {
252+
getPrecedence(operators.top()->getValue()) >= getPrecedence(tokenValue)) {
193253
postfix.push_back(operators.top());
194254
operators.pop();
195255
}

tree.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TREE_H
33
#include <iostream>
44
#include "token.h"
5+
#include <vector>
56

67
const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"};
78
const std::vector<std::string> equationOperators = {"+", "-", "/", "%", "*", "(", ")", "=", "'", "<", ">", "<=", ">=", "&&"};
@@ -11,9 +12,10 @@ class Tree {
1112
void printTree(Token*, Token*);
1213
bool contains(const std::vector<std::string>, std::string);
1314
Token* handleAssignment(Token*);
15+
Token* handleFunction(Token *,std::vector<Token*>& equationAsVec, bool& isFunctionCall);
1416
int getPrecedence(std::string op);
1517
bool isOperator(std::string c);
16-
std::vector<Token*> infixToPostfix(const std::vector<Token*> infix);
18+
std::vector<Token*> infixToPostfix(const std::vector<Token*> infix, bool isFunctionCall);
1719

1820

1921
public:

0 commit comments

Comments
 (0)