@@ -20,6 +20,14 @@ void Tree::printTree(Token* head, Token* prevToken){
20
20
ignore = false ;
21
21
std::cout << " RETURN" ;
22
22
head = head->getSibling ();
23
+ while (head->getValue () == " (" ){
24
+ head = head->getSibling ();
25
+ }
26
+ if (head->getSibling () != nullptr && contains (equationOperators, head->getSibling ()->getValue ())) {
27
+ std::cout << " ----> " ;
28
+ head = handleAssignment (head);
29
+ prevToken = nullptr ;
30
+ }
23
31
} else if (head->getValue () == " procedure" || head->getValue () == " function" ) {
24
32
std::cout << " DECLARATION\n |\n |\n |\n |\n v\n " ;
25
33
while (head->getSibling () != nullptr ) {
@@ -33,7 +41,18 @@ void Tree::printTree(Token* head, Token* prevToken){
33
41
head = handleAssignment (head);
34
42
prevToken = nullptr ;
35
43
36
- } else if (head->getValue () == " printf" ){
44
+ } else if (head->getValue () == " while" ){
45
+ ignore = false ;
46
+ std::cout << " WHILE ----> " ;
47
+ // This needs to break out to handleAssignment();
48
+ head = head->getSibling ();
49
+ while (head->getSibling ()->getValue () == " (" ){
50
+ head = head->getSibling ();
51
+ }
52
+ head = handleAssignment (head);
53
+ prevToken = nullptr ;
54
+
55
+ }else if (head->getValue () == " printf" ){
37
56
ignore = true ;
38
57
std::cout << " PRINTF" ;
39
58
while (head->getSibling () != nullptr ) {
@@ -60,7 +79,7 @@ void Tree::printTree(Token* head, Token* prevToken){
60
79
forCount++;
61
80
}
62
81
63
- } else if (contains (varTypes, prevToken->getValue ())) {
82
+ } else if (prevToken != nullptr && contains (varTypes, prevToken->getValue ())) {
64
83
ignore = true ; // this was false but i think should be true
65
84
std::cout << " DECLARATION" ;
66
85
if (head->getSibling () != nullptr && head->getSibling ()->getValue () == " ," ) {
@@ -79,7 +98,17 @@ void Tree::printTree(Token* head, Token* prevToken){
79
98
// This needs to break out to handleAssignment();
80
99
head = handleAssignment (head);
81
100
prevToken = nullptr ;
82
- }
101
+ } else if (isFunction (head->getValue ())){
102
+ ignore = false ;
103
+ isCall = true ;
104
+ std::cout << " CALL" ;
105
+ while (head->getSibling () != nullptr ) {
106
+ head = head->getSibling ();
107
+ if (head->getType () == " IDENTIFIER" ){
108
+ std::cout << " ----> " << head->getValue ();
109
+ }
110
+ }
111
+ }
83
112
} else if (head->getSibling () == nullptr && head->getChild () != nullptr && head->getValue () == " ;" ){
84
113
std::cout << " \n |\n |\n |\n |\n v\n " ;
85
114
}
@@ -93,6 +122,7 @@ void Tree::printTree(Token* head, Token* prevToken){
93
122
if (!ignore) {
94
123
std::cout << " \n |\n |\n |\n |\n v\n " ;
95
124
}
125
+ isCall = false ;
96
126
return printTree (head->getChild (), head);
97
127
}
98
128
return ;
@@ -138,26 +168,42 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo
138
168
139
169
Token* Tree::handleAssignment (Token* head) {
140
170
std::vector<Token*> equationAsVec;
141
- bool isFunctionCall = false ;
142
-
171
+ Token* prev = nullptr ;
172
+ isCall = isFunction (head->getValue ());
173
+
143
174
if (head->getValue () != " (" ) {
144
175
equationAsVec.push_back (head);
145
176
}
146
177
178
+
147
179
head = head->getSibling ();
148
180
149
- while (head->getValue () != " ;" && (contains (equationOperators, head->getValue ()) || head->getType () == " IDENTIFIER" || head->getType () == " INTEGER" || head->getType () == " CHARACTER" || head->getType () == " STRING" || head->getType () == " DOUBLE_QUOTE" )) {
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" )) {
150
182
// Check for function call (identifier with a L_PAREN)
151
- if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" ) {
183
+ if (isFunction (head->getValue ())){
184
+ isCall = true ;
185
+ }
186
+ if (head->getType () == " IDENTIFIER" && head->getSibling () != nullptr && head->getSibling ()->getValue () == " (" && isCall) {
152
187
// Call to handleFunction since we encountered an identifier with a L_PAREN
188
+ auto temp = prev;
153
189
equationAsVec.push_back (head);
154
- head = handleFunction (head, equationAsVec, isFunctionCall);
190
+ prev = head;
191
+ head = handleFunction (head, equationAsVec, isCall);
192
+ if (temp->getValue () == " !" ) {
193
+ equationAsVec.push_back (temp);
194
+ }
155
195
}
156
- else {
157
- equationAsVec.push_back (head);
196
+ else if (head->getValue () != " (" && head->getValue () != " !" ){
197
+ if (prev != nullptr && prev->getValue () == " !" ){
198
+ equationAsVec.push_back (head);
199
+ equationAsVec.push_back (prev);
200
+ } else {
201
+ equationAsVec.push_back (head);
202
+ }
158
203
}
159
204
160
205
if (head ->getSibling () != nullptr ) {
206
+ prev = head;
161
207
head = head->getSibling ();
162
208
}
163
209
else {
@@ -166,7 +212,7 @@ Token* Tree::handleAssignment(Token* head) {
166
212
}
167
213
168
214
// Convert infix to postfix
169
- std::vector<Token*> postFix = infixToPostfix (equationAsVec, isFunctionCall );
215
+ std::vector<Token*> postFix = infixToPostfix (equationAsVec, isCall );
170
216
171
217
// std::cout << "Size<: " << postFix.size() << std::endl;
172
218
for (int i = 0 ; i < postFix.size (); i++) {
@@ -211,7 +257,7 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
211
257
std::string tokenValue = t->getValue ();
212
258
213
259
// If it's an operand, add it to the postfix output
214
- if (tokenType == " INTEGER" || tokenType == " STRING" || tokenType == " CHARACTER" || tokenType == " IDENTIFIER" || tokenType == " SINGLE_QUOTE" || tokenType == " DOUBLE_QUOTE" ) {
260
+ if (tokenType == " INTEGER" || tokenType == " STRING" || tokenType == " CHARACTER" || tokenType == " IDENTIFIER" || tokenType == " SINGLE_QUOTE" || tokenType == " DOUBLE_QUOTE" || tokenValue == " ! " ) {
215
261
postfix.push_back (t);
216
262
}
217
263
// If it's a left parenthesis, push it onto the stack
@@ -239,11 +285,11 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
239
285
}
240
286
// Add array brackets here if it is a function call, if not, it is a normal operator.
241
287
else if (tokenType == " L_BRACKET" || tokenType == " R_BRACKET" ) {
242
- if (isFunctionCall) {
288
+ // if (isFunctionCall) {
243
289
postfix.push_back (t);
244
- } else {
245
- operators.push (t);
246
- }
290
+ // } else {
291
+ // operators.push(t);
292
+ // }
247
293
}
248
294
// If it's an operator
249
295
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
@@ -266,3 +312,15 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
266
312
267
313
return postfix;
268
314
}
315
+
316
+
317
+ bool Tree::isFunction (std::string tokenName){
318
+ Entry* tableHead = symbolTable->getHead ();
319
+ while (tableHead != nullptr ) {
320
+ if ((tableHead->getIDType () == " procedure" || tableHead->getIDType () == " function" ) && tableHead->getIDName () == tokenName) {
321
+ return true ;
322
+ }
323
+ tableHead = tableHead->getNext ();
324
+ }
325
+ return false ;
326
+ }
0 commit comments