Add while and for to lexer

This commit is contained in:
ala89 2023-11-29 11:46:01 +01:00
parent 1dec4b3c4f
commit 5925582de8
3 changed files with 21 additions and 3 deletions

View File

@ -11,7 +11,7 @@ using namespace std;
/**
* Tokens definition
*/
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land, Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else };
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land, Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else, While, For };
enum class Type { Int };
using TokenData = variant<int, string, Type>;

View File

@ -92,6 +92,12 @@ void _debug_print_token(Token token) {
case TokenType::Else:
cout << "Else";
break;
case TokenType::While:
cout << "While";
break;
case TokenType::For:
cout << "For";
break;
}
}
@ -124,6 +130,8 @@ string _debug_print_token_type(TokenType type) {
case TokenType::RCurlyBracket: return "RCurlyBracket";
case TokenType::If: return "If";
case TokenType::Else: return "Else";
case TokenType::While: return "While";
case TokenType::For: return "For";
default: return "Unknown";
}
}
@ -176,6 +184,16 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
tokens.emplace_back(token);
j += 4;
}
else if (str.starts_with("while")) {
Token token = { .type = TokenType::While, .pos = pos };
tokens.emplace_back(token);
j += 5;
}
else if (str.starts_with("for")) {
Token token = { .type = TokenType::For, .pos = pos };
tokens.emplace_back(token);
j += 3;
}
else if (str.starts_with("++")) {
Token token = { .type = TokenType::DoublePlus, .pos = pos };
tokens.emplace_back(token);

View File

@ -11,7 +11,7 @@ int main() {
/* All tokens */
vector<string> inputs = {
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else"
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for"
};
vector<TokenType> expectedTypes = {
@ -20,7 +20,7 @@ int main() {
TokenType::Lor, TokenType::Lt, TokenType::Gt, TokenType::Leq, TokenType::Geq, TokenType::NotEqual,
TokenType::Not, TokenType::Star, TokenType::Slash, TokenType::Percent, TokenType::Equal,
TokenType::Semicolon, TokenType::LParenthese, TokenType::RParenthese, TokenType::LCurlyBracket,
TokenType::RCurlyBracket, TokenType::If, TokenType::Else
TokenType::RCurlyBracket, TokenType::If, TokenType::Else, TokenType::While, TokenType::For
};
for (size_t i = 0; i < inputs.size(); i++) {