diff --git a/src/lexer/lexer.cpp b/src/lexer/lexer.cpp index 38fb901..e9b87bc 100644 --- a/src/lexer/lexer.cpp +++ b/src/lexer/lexer.cpp @@ -14,12 +14,16 @@ namespace lexer { switch (c) { case '=': + if (input.peek() == '=') + return {token::type::EQ, std::string{c, (char) input.get()}}; return {token::type::ASSIGN, c}; case '+': return {token::type::PLUS, c}; case '-': return {token::type::MINUS, c}; case '!': + if (input.peek() == '=') + return {token::type::NEQ, std::string{c, (char) input.get()}}; return {token::type::BANG, c}; case '*': return {token::type::ASTERISK, c}; diff --git a/src/token/type.hpp b/src/token/type.hpp index 0f2c6b8..aeaca36 100644 --- a/src/token/type.hpp +++ b/src/token/type.hpp @@ -30,6 +30,8 @@ namespace token { X(ELSE, "ELSE") \ X(TRUE, "TRUE") \ X(FALSE, "FALSE") \ + X(EQ, "==") \ + X(NEQ, "!=") \ X(RETURN, "RETURN") // Define the TokenType enum using the X-macro diff --git a/test/lexer.cpp b/test/lexer.cpp index 19137ce..696b346 100644 --- a/test/lexer.cpp +++ b/test/lexer.cpp @@ -56,6 +56,9 @@ if (5 < 10) {\ } else {\ return false;\ }\ +\ +10 == 10;\ +10 != 9;\ "); lexer::lexer l{ss}; @@ -137,6 +140,16 @@ if (5 < 10) {\ {token::type::SEMICOLON, ";"}, {token::type::RBRACE, "}"}, + + {token::type::INT, "10"}, + {token::type::EQ, "=="}, + {token::type::INT, "10"}, + {token::type::SEMICOLON, ";"}, + + {token::type::INT, "10"}, + {token::type::NEQ, "!="}, + {token::type::INT, "9"}, + {token::type::SEMICOLON, ";"}, // clang-format on };