Skip to content

Commit 61c664c

Browse files
Merge pull request #7 from Pip-Install-Party/officialblake-develop
update logic
2 parents 677d69b + 84cf636 commit 61c664c

File tree

6 files changed

+91
-24
lines changed

6 files changed

+91
-24
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
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
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.exe *.o *.txt
23+
rm -f tree.x *.o *.txt

main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ void abstractSyntaxTree(std::ifstream& testFile, std::ostringstream& outputFile,
100100
Parser *parser = new Parser(tokenList);
101101
parser->begin();
102102

103-
Tree* tree = new Tree(parser->getHead());
103+
Table *table = new Table;
104+
105+
table->begin(parser->getHead());
106+
107+
Tree* tree = new Tree(parser->getHead(), table);
104108

105109
}
106110

parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ extern std::string tokenType; //= TokenTypes::IDENTIFIER; // or TokenTypes::INTE
4646
class Parser
4747
{
4848
private:
49-
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool", "string", "procedure", "function"};
49+
std::vector<std::string> reserved = {"printf", "int", "void", "char", "bool", "procedure", "function"};
5050

5151
Token *head;
52-
// Vector holding all of the tokens in order from the tokenizer.
52+
// Vector holding all of the tokens in order from the tokenizer.s
5353
std::vector<Token> tokenList;
5454

5555
// Variable to track the current position in the token list.

table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Table {
2525
void begin(Token*);
2626
void printTable();
2727
void printParameters();
28+
Entry* getHead() {return head;}
2829
~Table(){};
2930
};
3031

tree.cpp

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ void Tree::printTree(Token* head, Token* prevToken){
2020
ignore = false;
2121
std::cout << "RETURN";
2222
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+
}
2331
} else if (head->getValue() == "procedure" || head->getValue() == "function") {
2432
std::cout << "DECLARATION\n|\n|\n|\n|\nv\n";
2533
while(head->getSibling() != nullptr) {
@@ -33,7 +41,18 @@ void Tree::printTree(Token* head, Token* prevToken){
3341
head = handleAssignment(head);
3442
prevToken = nullptr;
3543

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"){
3756
ignore = true;
3857
std::cout << "PRINTF";
3958
while (head->getSibling() != nullptr) {
@@ -60,7 +79,7 @@ void Tree::printTree(Token* head, Token* prevToken){
6079
forCount++;
6180
}
6281

63-
} else if (contains(varTypes, prevToken->getValue())) {
82+
} else if (prevToken != nullptr && contains(varTypes, prevToken->getValue())) {
6483
ignore = true; // this was false but i think should be true
6584
std::cout << "DECLARATION";
6685
if (head->getSibling() != nullptr && head->getSibling()->getValue() == ",") {
@@ -79,7 +98,17 @@ void Tree::printTree(Token* head, Token* prevToken){
7998
// This needs to break out to handleAssignment();
8099
head = handleAssignment(head);
81100
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+
}
83112
} else if (head->getSibling() == nullptr && head->getChild() != nullptr && head->getValue() == ";"){
84113
std::cout << "\n|\n|\n|\n|\nv\n";
85114
}
@@ -93,6 +122,7 @@ void Tree::printTree(Token* head, Token* prevToken){
93122
if (!ignore) {
94123
std::cout << "\n|\n|\n|\n|\nv\n";
95124
}
125+
isCall = false;
96126
return printTree(head->getChild(), head);
97127
}
98128
return;
@@ -138,26 +168,42 @@ Token* Tree::handleFunction(Token *head, std::vector<Token*>& equationAsVec, boo
138168

139169
Token* Tree::handleAssignment(Token* head) {
140170
std::vector<Token*> equationAsVec;
141-
bool isFunctionCall = false;
142-
171+
Token* prev = nullptr;
172+
isCall = isFunction(head->getValue());
173+
143174
if (head->getValue() != "(") {
144175
equationAsVec.push_back(head);
145176
}
146177

178+
147179
head = head->getSibling();
148180

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")) {
150182
// 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) {
152187
// Call to handleFunction since we encountered an identifier with a L_PAREN
188+
auto temp = prev;
153189
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+
}
155195
}
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+
}
158203
}
159204

160205
if (head ->getSibling() != nullptr) {
206+
prev = head;
161207
head = head->getSibling();
162208
}
163209
else {
@@ -166,7 +212,7 @@ Token* Tree::handleAssignment(Token* head) {
166212
}
167213

168214
// Convert infix to postfix
169-
std::vector<Token*> postFix = infixToPostfix(equationAsVec, isFunctionCall);
215+
std::vector<Token*> postFix = infixToPostfix(equationAsVec, isCall);
170216

171217
//std::cout << "Size<: " << postFix.size() << std::endl;
172218
for (int i = 0; i < postFix.size(); i++) {
@@ -211,7 +257,7 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix, bool i
211257
std::string tokenValue = t->getValue();
212258

213259
// 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 == "!"){
215261
postfix.push_back(t);
216262
}
217263
// 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
239285
}
240286
// Add array brackets here if it is a function call, if not, it is a normal operator.
241287
else if (tokenType == "L_BRACKET" || tokenType == "R_BRACKET") {
242-
if (isFunctionCall) {
288+
//if (isFunctionCall) {
243289
postfix.push_back(t);
244-
} else {
245-
operators.push(t);
246-
}
290+
// } else {
291+
// operators.push(t);
292+
// }
247293
}
248294
// If it's an operator
249295
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
266312

267313
return postfix;
268314
}
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+
}

tree.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
#define TREE_H
33
#include <iostream>
44
#include "token.h"
5+
#include "table.h"
56
#include <vector>
67

78
const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"};
8-
const std::vector<std::string> equationOperators = {"+", "-", "/", "%", "*", "(", ")", "=", "'", "<", ">", "<=", ">=", "&&"};
9+
const std::vector<std::string> equationOperators = {"+", "-", "/", "%", "*", "(", ")", "=", "==", "'", "<", ">", "<=", ">=", "&&"};
910

1011
class Tree {
1112
private:
13+
Table* symbolTable;
14+
bool isCall = false;
1215
void printTree(Token*, Token*);
1316
bool contains(const std::vector<std::string>, std::string);
17+
bool isFunction(std::string tokenName);
1418
Token* handleAssignment(Token*);
1519
Token* handleFunction(Token *,std::vector<Token*>& equationAsVec, bool& isFunctionCall);
1620
int getPrecedence(std::string op);
@@ -19,7 +23,7 @@ class Tree {
1923

2024

2125
public:
22-
Tree(Token* head) { printTree(head, nullptr); }
26+
Tree(Token* head, Table* table) { symbolTable = table; printTree(head, nullptr); }
2327
~Tree();
2428
};
2529

0 commit comments

Comments
 (0)