@@ -175,45 +175,70 @@ Token* Tree::handleAssignment(Token* head) {
175
175
equationAsVec.push_back (head);
176
176
}
177
177
178
-
179
178
head = head->getSibling ();
180
-
181
- while (head->getValue () != " ;" && (contains (equationOperators, head->getValue ()) || head->getValue () != " ;" || head->getValue () != " [" || head->getValue () != " ]" || head->getType () == " IDENTIFIER" || head->getType () == " INTEGER" || head->getType () == " CHARACTER" || head->getType () == " STRING" || head->getType () == " DOUBLE_QUOTE" )) {
182
- // Check for function call (identifier with a L_PAREN)
183
- if (isFunction (head->getValue ())){
184
- isCall = true ;
185
- }
186
- if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" && isCall) {
187
- // Call to handleFunction since we encountered an identifier with a L_PAREN
179
+
180
+
181
+ // while(head != nullptr && head->getValue() != ";" && (contains(equationOperators, head->getValue()) || head->getValue() != ";" || head->getValue() != "[" || head->getValue() != "]" || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) {
182
+ // Tokens processed until semicolon is reached
183
+ while (head != nullptr && head->getValue () != " ;" ) {
184
+
185
+ // Process head as a function, since it is a function name followed by L_PAREN
186
+ if (isFunction (head->getValue ()) && head->getSibling () && head->getSibling ()->getValue () == " (" ){
187
+ equationAsVec.push_back (head); // Function's name
188
+ head = handleFunction (head, equationAsVec, isCall); // Process function parameters
189
+
190
+ // Checking if function has a '!'
191
+ if (prev != nullptr && prev->getValue () == " !" ) {
192
+ equationAsVec.push_back (prev);
193
+ prev = nullptr ;
194
+ }
195
+
196
+ }
197
+ // Process head as an identifier followed by '('
198
+ else if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" && isCall) {
188
199
auto temp = prev;
189
- equationAsVec.push_back (head);
200
+ equationAsVec.push_back (head); // Add identifier
190
201
prev = head;
191
- head = handleFunction (head, equationAsVec, isCall);
192
- if (temp->getValue () == " !" ) {
193
- equationAsVec.push_back (temp);
202
+ head = handleFunction (head, equationAsVec, isCall); // Process function
203
+
204
+ // Checking if function has a '!'
205
+ if (prev != nullptr && prev->getValue () == " !" ) {
206
+ equationAsVec.push_back (prev);
207
+ prev = nullptr ;
194
208
}
195
209
}
196
- else if (head->getValue () != " (" && head->getValue () != " !" ){
210
+ // Checking for a '!' before function or expressions and set to prev until we process function or identifier
211
+ else if (head->getValue () == " !" ) {
212
+ prev = head;
213
+ }
214
+ // Checking for parentheses
215
+ else if (head->getValue () == " (" || head->getValue () == " )" ) {
216
+ equationAsVec.push_back (head);
217
+ }
218
+ // Any other operand pushed here
219
+ else if (head->getValue () != " (" && head->getValue () != " !" ){
220
+ equationAsVec.push_back (head);
221
+
222
+ // Any '!' before operand
197
223
if (prev != nullptr && prev->getValue () == " !" ){
198
- equationAsVec.push_back (head);
199
224
equationAsVec.push_back (prev);
200
- } else {
201
- equationAsVec.push_back (head);
202
- }
225
+ prev = nullptr ;
226
+ }
203
227
}
204
228
229
+ // Go to next token or break if none left
205
230
if (head ->getSibling () != nullptr ) {
206
231
prev = head;
207
232
head = head->getSibling ();
208
233
}
209
234
else {
210
235
break ;
211
236
}
237
+
212
238
}
213
239
214
240
// Convert infix to postfix
215
241
std::vector<Token*> postFix = infixToPostfix (equationAsVec, isCall);
216
-
217
242
// std::cout << "Size<: " << postFix.size() << std::endl;
218
243
for (int i = 0 ; i < postFix.size (); i++) {
219
244
std::string tokenValue = postFix.at (i)->getValue ();
0 commit comments