@@ -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 |\n v\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 |\n v\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+
110139Token* 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 }
0 commit comments