Skip to content

Commit 6b0d6ba

Browse files
Merge pull request #21 from Pip-Install-Party/jacobs999-develop
Added new states for nested if statements in states 0, 1, and 5
2 parents ffea93b + 6f09484 commit 6b0d6ba

File tree

2 files changed

+90
-54
lines changed

2 files changed

+90
-54
lines changed

tokenizer.cpp

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,17 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
5151
buffer << "Token: " << ch << "\n";
5252
return state0(inputStream, lineCount, buffer);
5353
} else if (isdigit(ch)) {
54+
//state12(inputStream, lineCount, buffer);
55+
//************************************************** wasnt working plz help
5456
std::string number;
5557
number += ch;
56-
// **********************************************************************************************
57-
// This needs to be its own state. There should not be any nested if statements.
58-
5958
// Keep reading until a non-digit character is found.
6059
while (inputStream.get(ch) && ch != ' ' && ch != ';' && ch != ')' && ch != ']') {
6160
number += ch;
6261
}
6362

6463
inputStream.putback(ch); // Put back the last non-digit character.
65-
64+
6665
// Check if the number is valid.
6766
if (!isValidInteger(number)) {
6867
std::cerr << "Syntax error on line " << lineCount << ": invalid integer\n";
@@ -71,26 +70,15 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
7170

7271
buffer << "\nToken type: INTEGER\n";
7372
buffer << "Token: " << number << "\n";
74-
// **********************************************************************************************
73+
//*****************************************************
7574
return state0(inputStream, lineCount, buffer);
7675
} else if (ch == ',') {
7776
buffer << "\nToken type: COMMA\n";
7877
buffer << "Token: " << ch << "\n";
7978
return state0(inputStream, lineCount, buffer);
8079
} else if (ch == '=') {
81-
// **********************************************************************************************
82-
// This needs to be its own state. There should not be any nested if statements.
83-
inputStream.get(ch);
84-
if (ch == '=') {
85-
buffer << "\nToken type: BOOLEAN_EQUAL\n";
86-
buffer << "Token: ==\n";
87-
} else {
88-
inputStream.putback(ch);
89-
buffer << "\nToken type: ASSIGNMENT_OPERATOR\n";
90-
buffer << "Token: =\n";
91-
}
80+
state13(inputStream, lineCount, buffer);
9281
return state0(inputStream, lineCount, buffer);
93-
// **********************************************************************************************
9482
} else if (ch == '+') {
9583
buffer << "\nToken type: PLUS\n";
9684
buffer << "Token: " << ch << "\n";
@@ -117,19 +105,7 @@ void Tokenizer::state0(std::istringstream &inputStream, int &lineCount, std::ost
117105
buffer << "Token: " << ch << "\n";
118106
return state0(inputStream, lineCount, buffer);
119107
} else if (ch == '&') {
120-
// **********************************************************************************************
121-
// This needs to be its own state. There should not be any nested if statements.
122-
123-
inputStream.get(ch);
124-
if (ch == '&') {
125-
buffer << "\nToken type: BOOLEAN_AND\n";
126-
buffer << "Token: &&\n";
127-
} else {
128-
inputStream.putback(ch);
129-
buffer << "\nToken type: AMPERSAND\n";
130-
buffer << "Token: &\n";
131-
}
132-
// **********************************************************************************************
108+
state10(inputStream, lineCount, buffer);
133109
return state0(inputStream, lineCount, buffer);
134110
} else if (ch == '"') {
135111
buffer << "\nToken type: DOUBLE_QUOTE\n";
@@ -177,17 +153,8 @@ void Tokenizer::state1(std::istringstream &inputStream, int &lineCount, std::ost
177153
buffer << "Token: " << ch << "\n";
178154
return;
179155
} else if (ch == '\\') { // Handle escape characters in strings
180-
// **********************************************************************************************
181-
// This needs to be its own state. There should not be any nested if statements.
182-
183156
buffer << ch; // Add the backslash
184-
inputStream.get(ch);
185-
if (ch == 'n') { // Case to add specifically 'n' to buffer
186-
buffer << "n";
187-
} else {
188-
buffer << ch; // Add other escape character other than /n
189-
}
190-
// **********************************************************************************************
157+
state9(inputStream, lineCount, buffer);
191158
} else if (ch == '\n') { // Real newline in input
192159
lineCount++;
193160
std::cerr << "Error: Unterminated string on line " << lineCount << "\n";
@@ -274,18 +241,7 @@ void Tokenizer::state5(std::istringstream &inputStream, int &lineCount, std::ost
274241
buffer << "\nToken type: CHAR_LITERAL\n";
275242
buffer << "Token: " << ch << "\n"; // Handle the literal inside the single quotes
276243
}
277-
278-
// Check for the closing single quote (to the non empty single quotes)
279-
inputStream.get(ch); // Why are we getting another character on the same iteration and checking for '\' again? Shouldn't we be calling state5() again instead? Repeated code?
280-
if (ch == '\'') {
281-
buffer << "\nToken type: SINGLE_QUOTE\n";
282-
buffer << "Token: " << ch << "\n"; // Handle the final character
283-
return state0(inputStream, lineCount, buffer);
284-
} else {
285-
std::cerr << "Error: Invalid character literal\n";
286-
exit(1);
287-
}
288-
244+
state11(inputStream, lineCount, buffer); // Found the char or escape character, now check for the closing quote
289245
}
290246

291247
void Tokenizer::state6(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
@@ -326,12 +282,85 @@ void Tokenizer::state8(std::istringstream &inputStream, int &lineCount, std::ost
326282
if (nextCh == 'n' || nextCh == 't' || nextCh == '\\' || nextCh == '0') { // Can add more if needed...
327283
buffer << "\nToken type: STRING\n";
328284
buffer << "Token: \\" << nextCh << "\n"; // Add the escaped character to the buffer
329-
} else { // Else, not a valid esc char
285+
} else { // Else, was not a valid esc character
330286
std::cerr << "Error: Invalid escape sequence '\\" << nextCh << "\n";
331287
exit(1);
332288
}
333289
}
334290

291+
void Tokenizer::state9(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
292+
char ch;
293+
inputStream.get(ch);
294+
if (ch == 'n') { // Case to add specifically 'n' to buffer
295+
buffer << "n";
296+
} else {
297+
buffer << ch; // Add other escape character other than /n
298+
}
299+
}
300+
301+
void Tokenizer::state10(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
302+
char ch;
303+
inputStream.get(ch);
304+
if (ch == '&') {
305+
buffer << "\nToken type: BOOLEAN_AND\n";
306+
buffer << "Token: &&\n";
307+
} else {
308+
inputStream.putback(ch);
309+
buffer << "\nToken type: AMPERSAND\n";
310+
buffer << "Token: &\n";
311+
}
312+
}
313+
314+
//Check for the closing single quote (to the non empty single quotes) of the char or escape character
315+
void Tokenizer::state11(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
316+
char ch;
317+
inputStream.get(ch);
318+
if (ch == '\'') {
319+
buffer << "\nToken type: SINGLE_QUOTE\n";
320+
buffer << "Token: " << ch << "\n"; // Handle the final character
321+
return state0(inputStream, lineCount, buffer);
322+
} else {
323+
std::cerr << "Error: Invalid character literal\n";
324+
exit(1);
325+
}
326+
}
327+
328+
void Tokenizer::state12(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
329+
char ch;
330+
inputStream.get(ch);
331+
inputStream.putback(ch);
332+
std::string number;
333+
number += ch;
334+
// Keep reading until a non-digit character is found.
335+
while (inputStream.get(ch) && ch != ' ' && ch != ';' && ch != ')' && ch != ']') {
336+
number += ch;
337+
}
338+
339+
inputStream.putback(ch); // Put back the last non-digit character.
340+
341+
// Check if the number is valid.
342+
if (!isValidInteger(number)) {
343+
std::cerr << "Syntax error on line " << lineCount << ": invalid integer\n";
344+
exit(1);
345+
}
346+
347+
buffer << "\nToken type: INTEGER\n";
348+
buffer << "Token: " << number << "\n";
349+
}
350+
351+
void Tokenizer::state13(std::istringstream &inputStream, int &lineCount, std::ostringstream& buffer) {
352+
char ch;
353+
inputStream.get(ch);
354+
if (ch == '=') {
355+
buffer << "\nToken type: BOOLEAN_EQUAL\n";
356+
buffer << "Token: ==\n";
357+
} else {
358+
inputStream.putback(ch);
359+
buffer << "\nToken type: ASSIGNMENT_OPERATOR\n";
360+
buffer << "Token: =\n";
361+
}
362+
return state0(inputStream, lineCount, buffer);
363+
}
335364

336365
bool Tokenizer::isValidInteger(const std::string& token) {
337366
try {

tokenizer.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class Tokenizer
2121
void state5(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer); // Handles character literals
2222
void state6(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
2323
void state7(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
24-
void state8(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
24+
void state8(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
25+
void state9(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
26+
void state10(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
27+
void state11(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
28+
void state12(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
29+
void state13(std::istringstream &inputStream, int &lineCount, std::ostringstream &buffer);
30+
31+
2532

2633
void error(const std::string &message, int lineCount);
2734
bool isValidInteger(const std::string &token);

0 commit comments

Comments
 (0)