added EQ and NEQ

This commit is contained in:
Karma Riuk
2025-07-01 18:01:45 +02:00
parent 7973f7522c
commit aee7a741b1
3 changed files with 19 additions and 0 deletions

View File

@@ -14,12 +14,16 @@ namespace lexer {
switch (c) { switch (c) {
case '=': case '=':
if (input.peek() == '=')
return {token::type::EQ, std::string{c, (char) input.get()}};
return {token::type::ASSIGN, c}; return {token::type::ASSIGN, c};
case '+': case '+':
return {token::type::PLUS, c}; return {token::type::PLUS, c};
case '-': case '-':
return {token::type::MINUS, c}; return {token::type::MINUS, c};
case '!': case '!':
if (input.peek() == '=')
return {token::type::NEQ, std::string{c, (char) input.get()}};
return {token::type::BANG, c}; return {token::type::BANG, c};
case '*': case '*':
return {token::type::ASTERISK, c}; return {token::type::ASTERISK, c};

View File

@@ -30,6 +30,8 @@ namespace token {
X(ELSE, "ELSE") \ X(ELSE, "ELSE") \
X(TRUE, "TRUE") \ X(TRUE, "TRUE") \
X(FALSE, "FALSE") \ X(FALSE, "FALSE") \
X(EQ, "==") \
X(NEQ, "!=") \
X(RETURN, "RETURN") X(RETURN, "RETURN")
// Define the TokenType enum using the X-macro // Define the TokenType enum using the X-macro

View File

@@ -56,6 +56,9 @@ if (5 < 10) {\
} else {\ } else {\
return false;\ return false;\
}\ }\
\
10 == 10;\
10 != 9;\
"); ");
lexer::lexer l{ss}; lexer::lexer l{ss};
@@ -137,6 +140,16 @@ if (5 < 10) {\
{token::type::SEMICOLON, ";"}, {token::type::SEMICOLON, ";"},
{token::type::RBRACE, "}"}, {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 // clang-format on
}; };