Skip to content

Commit ef4c0d3

Browse files
Merge pull request #13 from Pip-Install-Party/holdenkea-develop
added comments, fixed scope issues all tests work now on my end
2 parents 03189b5 + c085cf4 commit ef4c0d3

File tree

2 files changed

+100
-47
lines changed

2 files changed

+100
-47
lines changed

table.cpp

Lines changed: 99 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,95 @@ void Table::build(Token* token, Token* prevToken, Entry* prevEntry) {
3030
}
3131
Entry* entry = prevEntry; // Start with the previous entry
3232

33-
if (!pause && contains(prevToken->getValue()) && token->getValue() != "{" && token->getValue() != "}" && token->getValue() != "(" && token->getValue() != ")" && token->getValue() != "[" && token->getValue() != "]") {
34-
// Create a new entry based on the token values
35-
pause = true;
33+
// Checks if function isn't paused, if prevToken is reserved, and token isn't {,},(,),[,]
34+
if (!pause && contains(prevToken->getValue()) &&
35+
token->getValue() != "{" && token->getValue() != "}" &&
36+
token->getValue() != "(" && token->getValue() != ")" &&
37+
token->getValue() != "[" && token->getValue() != "]") {
38+
39+
pause = true; // Pauses identifier creation until function parameters or array info is processed
40+
3641
if (prevToken->getValue() == "procedure" || prevToken->getValue() == "function") {
37-
scope++;
38-
if (prevToken->getValue() == "procedure") {
39-
exists(token, scope);
42+
scope++;
43+
inProcOrFuncScope = true;
44+
45+
if (prevToken->getValue() == "procedure") { // If prevToken is a procedure
46+
exists(token, scope);
47+
48+
// New entry created for this procedure
4049
entry = new Entry(token->getValue(), prevToken->getValue(), "NOT APPLICABLE", false, 0, scope);
41-
if (prevEntry == nullptr ){
50+
51+
if (prevEntry == nullptr){ //
4252
head = entry;
4353
}
44-
} else {
54+
}
55+
else { // If prevToken is a function
4556
exists(token, scope);
57+
58+
// New entry created for this function
4659
entry = new Entry(token->getSibling()->getValue(), prevToken->getValue(), token->getValue(), false, 0, scope);
47-
if (prevEntry == nullptr ){
60+
61+
if (prevEntry == nullptr){ // If prevEntry is null, set head to entry
4862
head = entry;
49-
} else {
50-
prevEntry->setNext(entry); // Update the next pointer of prevEntry
63+
}
64+
else {
65+
prevEntry->setNext(entry); // Links entry to prevEntry
5166
}
52-
return build(token->getSibling()->getSibling(), token->getSibling(), entry );
67+
// Recursive call to process the next sibling
68+
return build(token->getSibling()->getSibling(), token->getSibling(), entry);
5369
}
54-
} else {
55-
exists(token, scope);
56-
entry = new Entry(token->getValue(), "datatype", prevToken->getValue(), false, 0, scope);
57-
if (prevEntry == nullptr ){
70+
}
71+
else { // prevToken is not a procedure or function
72+
if (inProcOrFuncScope){
73+
exists(token, scope);
74+
entry = new Entry(token->getValue(), "datatype", prevToken->getValue(), false, 0, scope);
75+
}
76+
else{
77+
exists(token, 0);
78+
entry = new Entry(token->getValue(), "datatype", prevToken->getValue(), false, 0, 0);
79+
}
80+
81+
82+
83+
if (prevEntry == nullptr){
5884
head = entry;
59-
}
60-
if (token->getSibling() != nullptr) {
85+
}
6186

62-
if (token->getSibling()->getValue() == "[") {
63-
if ( prevEntry != nullptr ) {
64-
prevEntry->setNext(entry); // Update the next pointer of prevEntry
87+
if (token->getSibling() != nullptr){ // If token has a sibling, process sibling token
88+
89+
if (token->getSibling()->getValue() == "["){ // Indicates start of an array
90+
91+
if (prevEntry != nullptr){
92+
prevEntry->setNext(entry); // Links entry to prevEntry
6593
}
66-
return setArray(token->getSibling(), entry);
67-
} else if (token->getSibling()->getValue() == ",") {
68-
if ( prevEntry != nullptr ) {
69-
prevEntry->setNext(entry); // Update the next pointer of prevEntry
94+
95+
return setArray(token->getSibling(), entry); // Handles the array
96+
}
97+
else if (token->getSibling()->getValue() == ","){ // Indicates an initalizer list for variables
98+
99+
if (prevEntry != nullptr){
100+
prevEntry->setNext(entry); // Links entry to prevEntry
70101
}
71-
return handleInitList(prevToken->getValue(), token->getSibling()->getSibling(), entry);
102+
103+
return handleInitList(prevToken->getValue(), token->getSibling()->getSibling(), entry); // Handles initializer list
72104
}
73105
}
74106
}
75-
if ( prevEntry != nullptr ) {
76-
prevEntry->setNext(entry); // Update the next pointer of prevEntry
107+
108+
if (prevEntry != nullptr){
109+
prevEntry->setNext(entry); // Update the next pointer of prevEntry
77110
}
111+
78112
}
79-
// Check if it's a function or procedure again and handle parameters
80-
else if (pause == true) {
81-
// Process parameters for procedure
113+
else if (pause == true){ // If true, we are processing parameters for a function or procedure
82114
if (contains(prevToken->getValue()) && token->getType() == "IDENTIFIER"){
83115
exists(token, scope);
116+
117+
// Entry created for a parameter
84118
Entry *newEntry = new Entry(token->getValue(), "parameter", prevToken->getValue(), false, 0, scope);
85-
// Process for array parameters
86-
if (token->getSibling()->getValue() == "[") {
119+
120+
// Processes an array parameter
121+
if (token->getSibling()->getValue() == "["){
87122
token = token->getSibling()->getSibling();
88123
newEntry->setIsArray();
89124
newEntry->setArray(stoi(token->getValue()));
@@ -92,18 +127,26 @@ void Table::build(Token* token, Token* prevToken, Entry* prevEntry) {
92127
}
93128
}
94129

130+
// Checks for the ending of a function or procedure, or a '}'
131+
if (token->getValue() == "}") {
132+
pause = false; // Resume normal processing
133+
inProcOrFuncScope = false;
134+
}
135+
95136
// Recursively traverse siblings or children
96-
if (token->getSibling() != nullptr) {
137+
if (token->getSibling() != nullptr){
97138
build(token->getSibling(), token, entry); // Traverse sibling
98-
} else if (token->getChild() != nullptr) {
139+
}
140+
else if (token->getChild() != nullptr){
99141
pause = false;
100142
build(token->getChild(), token, entry); // Traverse child
101143
}
144+
102145
}
103146

104147
// Checks if the token is a reserved word
105148
bool Table::contains(std::string token){
106-
for (int i = 0; i < reserved.size(); i++) {
149+
for (int i = 0; i < reserved.size(); i++){
107150
if (token == reserved.at(i)){
108151
return true;
109152
}
@@ -114,24 +157,24 @@ bool Table::contains(std::string token){
114157
// Checks if the token exists in the symbol table
115158
void Table::exists(Token* token, int scope){
116159
auto temp = head;
117-
while(temp != nullptr) {
160+
while(temp != nullptr){
118161
if (temp->getIDName() == token->getValue() && !contains(token->getValue()) ){
119-
if (temp->getScope() == 0) {
162+
if (temp->getScope() == 0){
120163
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined globally\n";
121164
exit(1);
122165
}
123-
if (temp->getScope() == scope) {
166+
if (temp->getScope() == scope){
124167
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined locally\n";
125168
exit(1);
126169
}
127170
}
128-
for (int i = 0; i < temp->parameters.size(); i++) {
171+
for (int i = 0; i < temp->parameters.size(); i++){
129172
if (temp->parameters.at(i)->getIDName() == token->getValue() && !contains(token->getValue()) ){
130-
if (temp->parameters.at(i)->getScope() == 0) {
173+
if (temp->parameters.at(i)->getScope() == 0){
131174
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined globally\n";
132175
exit(1);
133176
}
134-
if (temp->parameters.at(i)->getScope() == scope) {
177+
if (temp->parameters.at(i)->getScope() == scope){
135178
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined locally\n";
136179
exit(1);
137180
}
@@ -149,11 +192,20 @@ void Table::setArray(Token* token, Entry* entry){
149192

150193

151194
void Table::handleInitList(std::string type, Token* token, Entry* prevEntry){
152-
Entry *entry = new Entry(token->getValue(), "datatype", type, false, 0, scope);
195+
Entry *entry;
196+
if(inProcOrFuncScope){
197+
entry = new Entry(token->getValue(), "datatype", type, false, 0, scope);
198+
}
199+
else{
200+
entry = new Entry(token->getValue(), "datatype", type, false, 0, 0);
201+
}
202+
153203
prevEntry->setNext(entry); // Update the next pointer of prevEntry
154-
if (token->getSibling()->getValue() == ";") {
204+
205+
if (token->getSibling()->getValue() == ";"){
155206
return build(token->getSibling()->getChild(), token->getSibling(), entry);
156207
}
208+
157209
return handleInitList(type, token->getSibling()->getSibling(), entry);
158210
}
159211

@@ -163,7 +215,7 @@ void Table::printTable(){
163215
//make temp head pointer
164216
Entry* tempHead = this->head;
165217

166-
while(tempHead != nullptr) {
218+
while(tempHead != nullptr){
167219
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_NAME: " << tempHead->getIDName() << std::endl;
168220
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_TYPE: " << tempHead->getIDType() << std::endl;
169221
std::cout << std::setw(colonWidth) << std::right << "DATATYPE: " << tempHead->getDType() << std::endl;
@@ -184,8 +236,8 @@ void Table::printParameters(){
184236
const int colonWidth = 25;
185237

186238
// Cycle through all entries in the table
187-
while(tempHead != nullptr) {
188-
if (tempHead->parameters.size() > 0) { // If current entry has parameters...
239+
while(tempHead != nullptr){
240+
if (tempHead->parameters.size() > 0){ // If current entry has parameters...
189241
std::cout << std::setw(colonWidth) << std::right << "PARAMETER LIST FOR: " << tempHead->getIDName() << std::endl;
190242
for (int i = 0; i < tempHead->parameters.size(); i++) { // Print each parameter
191243
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_NAME: " << tempHead->parameters.at(i)->getIDName() << std::endl;

table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Table {
1313
Entry* head = nullptr;
1414
bool pause = false;
1515
short scope = 0;
16+
bool inProcOrFuncScope = false;
1617
bool contains(std::string);
1718
void exists(Token*, int);
1819
void build(Token*, Token*, Entry*);

0 commit comments

Comments
 (0)