Add while and for to lexer
This commit is contained in:
parent
1dec4b3c4f
commit
5925582de8
@ -11,7 +11,7 @@ using namespace std;
|
|||||||
/**
|
/**
|
||||||
* Tokens definition
|
* 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 };
|
enum class Type { Int };
|
||||||
|
|
||||||
using TokenData = variant<int, string, Type>;
|
using TokenData = variant<int, string, Type>;
|
||||||
|
@ -92,6 +92,12 @@ void _debug_print_token(Token token) {
|
|||||||
case TokenType::Else:
|
case TokenType::Else:
|
||||||
cout << "Else";
|
cout << "Else";
|
||||||
break;
|
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::RCurlyBracket: return "RCurlyBracket";
|
||||||
case TokenType::If: return "If";
|
case TokenType::If: return "If";
|
||||||
case TokenType::Else: return "Else";
|
case TokenType::Else: return "Else";
|
||||||
|
case TokenType::While: return "While";
|
||||||
|
case TokenType::For: return "For";
|
||||||
default: return "Unknown";
|
default: return "Unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,6 +184,16 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
|
|||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
j += 4;
|
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("++")) {
|
else if (str.starts_with("++")) {
|
||||||
Token token = { .type = TokenType::DoublePlus, .pos = pos };
|
Token token = { .type = TokenType::DoublePlus, .pos = pos };
|
||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
|
@ -11,7 +11,7 @@ int main() {
|
|||||||
|
|
||||||
/* All tokens */
|
/* All tokens */
|
||||||
vector<string> inputs = {
|
vector<string> inputs = {
|
||||||
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else"
|
"int", "a", "=", "x", "++", "--", "==", "&&", "||", "<", ">", "<=", ">=", "!=", "!", "*", "/", "%", "=", ";", "(", ")", "{", "}", "if", "else", "while", "for"
|
||||||
};
|
};
|
||||||
|
|
||||||
vector<TokenType> expectedTypes = {
|
vector<TokenType> expectedTypes = {
|
||||||
@ -20,7 +20,7 @@ int main() {
|
|||||||
TokenType::Lor, TokenType::Lt, TokenType::Gt, TokenType::Leq, TokenType::Geq, TokenType::NotEqual,
|
TokenType::Lor, TokenType::Lt, TokenType::Gt, TokenType::Leq, TokenType::Geq, TokenType::NotEqual,
|
||||||
TokenType::Not, TokenType::Star, TokenType::Slash, TokenType::Percent, TokenType::Equal,
|
TokenType::Not, TokenType::Star, TokenType::Slash, TokenType::Percent, TokenType::Equal,
|
||||||
TokenType::Semicolon, TokenType::LParenthese, TokenType::RParenthese, TokenType::LCurlyBracket,
|
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++) {
|
for (size_t i = 0; i < inputs.size(); i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user