From 5cc7147909632bc5e9348e0735ff6d0af69907ab Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Mon, 30 Jun 2025 00:27:30 +0200 Subject: [PATCH] extended single char tokens --- src/lexer/lexer.cpp | 12 +++++++ src/token/type.hpp | 6 ++++ test/lexer.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/lexer/lexer.cpp b/src/lexer/lexer.cpp index 9f52162..38fb901 100644 --- a/src/lexer/lexer.cpp +++ b/src/lexer/lexer.cpp @@ -17,6 +17,18 @@ namespace lexer { return {token::type::ASSIGN, c}; case '+': return {token::type::PLUS, c}; + case '-': + return {token::type::MINUS, c}; + case '!': + return {token::type::BANG, c}; + case '*': + return {token::type::ASTERISK, c}; + case '/': + return {token::type::SLASH, c}; + case '<': + return {token::type::LT, c}; + case '>': + return {token::type::GT, c}; case ',': return {token::type::COMMA, c}; case ';': diff --git a/src/token/type.hpp b/src/token/type.hpp index ff7a91e..13e969b 100644 --- a/src/token/type.hpp +++ b/src/token/type.hpp @@ -12,6 +12,12 @@ namespace token { X(INT, "INT") \ X(ASSIGN, "=") \ X(PLUS, "+") \ + X(MINUS, "-") \ + X(BANG, "!") \ + X(ASTERISK, "*") \ + X(SLASH, "/") \ + X(LT, "<") \ + X(GT, ">") \ X(COMMA, ",") \ X(SEMICOLON, ";") \ X(LPAREN, "(") \ diff --git a/test/lexer.cpp b/test/lexer.cpp index a2f215d..6625032 100644 --- a/test/lexer.cpp +++ b/test/lexer.cpp @@ -36,7 +36,7 @@ TEST_CASE("Single character token") { } }; -TEST_CASE("Full tokens") { +TEST_CASE("More tokens") { struct test { token::type expectedType; std::string expectedLiteral; @@ -102,3 +102,86 @@ let result = add(five, ten);\ CHECK(tok.literal == t.expectedLiteral); } }; + +TEST_CASE("Even more tokens (simple)") { + struct test { + token::type expectedType; + std::string expectedLiteral; + }; + + std::istringstream ss("let five = 5;\ +let ten = 10;\ +let add = fn(x, y) {\ + x + y;\ +};\ +let result = add(five, ten);\ +!-/*5;\ +5 < 10 > 5;\ +"); + + lexer::lexer l{ss}; + + test tests[] = { + // clang-format off + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "five"}, + {token::type::ASSIGN, "="}, + {token::type::INT, "5"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "ten"}, + {token::type::ASSIGN, "="}, + {token::type::INT, "10"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "add"}, + {token::type::ASSIGN, "="}, + {token::type::FUNCTION, "fn"}, + {token::type::LPAREN, "("}, + {token::type::IDENTIFIER, "x"}, + {token::type::COMMA, ","}, + {token::type::IDENTIFIER, "y"}, + {token::type::RPAREN, ")"}, + {token::type::LBRACE, "{"}, + {token::type::IDENTIFIER, "x"}, + {token::type::PLUS, "+"}, + {token::type::IDENTIFIER, "y"}, + {token::type::SEMICOLON, ";"}, + {token::type::RBRACE, "}"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "result"}, + {token::type::ASSIGN, "="}, + {token::type::IDENTIFIER, "add"}, + {token::type::LPAREN, "("}, + {token::type::IDENTIFIER, "five"}, + {token::type::COMMA, ","}, + {token::type::IDENTIFIER, "ten"}, + {token::type::RPAREN, ")"}, + {token::type::SEMICOLON, ";"}, + + {token::type::BANG, "!"}, + {token::type::MINUS, "-"}, + {token::type::SLASH, "/"}, + {token::type::ASTERISK, "*"}, + {token::type::INT, "5"}, + {token::type::SEMICOLON, ";"}, + + {token::type::INT, "5"}, + {token::type::LT, "<"}, + {token::type::INT, "10"}, + {token::type::GT, ">"}, + {token::type::INT, "5"}, + {token::type::SEMICOLON, ";"}, + // clang-format on + }; + + for (const auto& t : tests) { + token::token tok = l.next_token(); + CHECK(tok.type == t.expectedType); + CHECK(tok.literal == t.expectedLiteral); + } +};