@@ -20,7 +20,7 @@ void Tree::printTree(Token* head, Token* prevToken){
20
20
ignore = false ;
21
21
std::cout << " RETURN" ;
22
22
head = head->getSibling ();
23
- }else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
23
+ } else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
24
24
std::cout << " DECLARATION\n |\n |\n |\n |\n v\n " ;
25
25
while (head->getSibling () != nullptr ) {
26
26
head = head->getSibling ();
@@ -60,7 +60,7 @@ void Tree::printTree(Token* head, Token* prevToken){
60
60
forCount++;
61
61
}
62
62
63
- }else if (contains (varTypes, prevToken->getValue ())) {
63
+ } else if (contains (varTypes, prevToken->getValue ())) {
64
64
ignore = true ; // this was false but i think should be true
65
65
std::cout << " DECLARATION" ;
66
66
if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " ," ) {
@@ -72,7 +72,7 @@ void Tree::printTree(Token* head, Token* prevToken){
72
72
}
73
73
std::cout << " \n |\n |\n |\n |\n v\n " ;
74
74
}
75
- } else if (head->getType () == " IDENTIFIER" ) {
75
+ } else if (head->getType () == " IDENTIFIER" ) {
76
76
if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " =" ) {
77
77
ignore = false ;
78
78
std::cout << " ASSIGNMENT" << " ----> " ;
@@ -107,24 +107,67 @@ bool Tree::contains(const std::vector<std::string> reserved, std::string type){
107
107
return false ;
108
108
}
109
109
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
+
110
139
Token* Tree::handleAssignment (Token* head) {
111
140
std::vector<Token*> equationAsVec;
141
+ bool isFunctionCall = false ;
142
+
112
143
if (head->getValue () != " (" ) {
113
144
equationAsVec.push_back (head);
114
145
}
146
+
115
147
head = head->getSibling ();
116
148
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
+
119
160
if (head ->getSibling () != nullptr ) {
120
161
head = head->getSibling ();
121
- } else {
162
+ }
163
+ else {
122
164
break ;
123
165
}
124
166
}
125
167
126
168
// Convert infix to postfix
127
- std::vector<Token*> postFix = infixToPostfix (equationAsVec);
169
+ std::vector<Token*> postFix = infixToPostfix (equationAsVec, isFunctionCall);
170
+
128
171
// std::cout << "Size<: " << postFix.size() << std::endl;
129
172
for (int i = 0 ; i < postFix.size (); i++) {
130
173
std::string tokenValue = postFix.at (i)->getValue ();
@@ -154,7 +197,7 @@ bool Tree::isOperator(std::string c) {
154
197
return c == " +" || c == " -" || c == " *" || c == " /" || c == " =" ;
155
198
}
156
199
157
- std::vector<Token*> Tree::infixToPostfix (const std::vector<Token*> infix) {
200
+ std::vector<Token*> Tree::infixToPostfix (const std::vector<Token*> infix, bool isFunctionCall ) {
158
201
std::stack<Token*> operators;
159
202
std::vector<Token*> postfix;
160
203
@@ -173,23 +216,40 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix) {
173
216
}
174
217
// If it's a left parenthesis, push it onto the stack
175
218
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
+ }
177
224
}
178
225
// If it's a right parenthesis, pop until the left parenthesis
179
226
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
+ }
183
238
}
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);
186
246
}
187
247
}
188
248
// If it's an operator
189
249
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
190
250
// Pop all operators with higher or equal precedence from the stack
191
251
while (!operators.empty () && operators.top ()->getType () != " L_PAREN" &&
192
- getPrecedence (operators.top ()->getValue ()) >= getPrecedence (tokenValue)) {
252
+ getPrecedence (operators.top ()->getValue ()) >= getPrecedence (tokenValue)) {
193
253
postfix.push_back (operators.top ());
194
254
operators.pop ();
195
255
}
0 commit comments