From c61b26e6928def404ee521c97d5058a863504769 Mon Sep 17 00:00:00 2001 From: ala89 Date: Wed, 22 Nov 2023 16:20:20 +0100 Subject: [PATCH] Add more logical operators --- src/include/types.h | 4 +++- src/tokenize.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/include/types.h b/src/include/types.h index 20d27ef..7cb57fa 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, 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 }; using TokenData = variant; @@ -52,6 +52,8 @@ Comp -> | Sum > Comp | Sum <= Comp | Sum >= Comp + | Sum && Comp + | Sum || Comp Sum -> | Term diff --git a/src/tokenize.cpp b/src/tokenize.cpp index b8b31a2..b18b855 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -35,6 +35,12 @@ void _debug_print_token(Token token) { case TokenType::DoubleEqual: cout << "=="; break; + case TokenType::Land: + cout << "&&"; + break; + case TokenType::Lor: + cout << "||"; + break; case TokenType::Lt: cout << "<"; break; @@ -152,6 +158,16 @@ vector tokenize(vector input, int initial_line) { tokens.emplace_back(token); 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("<")) { Token token = { .type = TokenType::Lt, .pos = pos }; tokens.emplace_back(token);