diff --git a/src/include/types.h b/src/include/types.h index ae60731..8e4db0c 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -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; diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 7f6a96a..b8b5153 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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 tokenize(vector 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); diff --git a/test/tokenize.cpp b/test/tokenize.cpp index c97b5a3..8eb2e4a 100644 --- a/test/tokenize.cpp +++ b/test/tokenize.cpp @@ -11,7 +11,7 @@ int main() { /* All tokens */ vector inputs = { - "int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else" + "int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for" }; vector 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++) {