Add more logical operators

This commit is contained in:
ala89 2023-11-22 16:20:20 +01:00
parent fb261e26f9
commit c61b26e692
2 changed files with 19 additions and 1 deletions

View File

@ -11,7 +11,7 @@ using namespace std;
/** /**
* Tokens definition * Tokens definition
*/ */
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, 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 };
enum class Type { Int }; enum class Type { Int };
using TokenData = variant<int, string, Type>; using TokenData = variant<int, string, Type>;
@ -52,6 +52,8 @@ Comp ->
| Sum > Comp | Sum > Comp
| Sum <= Comp | Sum <= Comp
| Sum >= Comp | Sum >= Comp
| Sum && Comp
| Sum || Comp
Sum -> Sum ->
| Term | Term

View File

@ -35,6 +35,12 @@ void _debug_print_token(Token token) {
case TokenType::DoubleEqual: case TokenType::DoubleEqual:
cout << "=="; cout << "==";
break; break;
case TokenType::Land:
cout << "&&";
break;
case TokenType::Lor:
cout << "||";
break;
case TokenType::Lt: case TokenType::Lt:
cout << "<"; cout << "<";
break; break;
@ -152,6 +158,16 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
tokens.emplace_back(token); tokens.emplace_back(token);
j += 2; j += 2;
} }
else if (str.starts_with("&&")) {
Token token = { .type = TokenType::Land, .pos = pos };
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with("||")) {
Token token = { .type = TokenType::Lor, .pos = pos };
tokens.emplace_back(token);
j += 2;
}
else if (str.starts_with("<")) { else if (str.starts_with("<")) {
Token token = { .type = TokenType::Lt, .pos = pos }; Token token = { .type = TokenType::Lt, .pos = pos };
tokens.emplace_back(token); tokens.emplace_back(token);