From ffff13b2e01123d4b05dcfe10abb8c3ae8e3c797 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sun, 29 Jun 2025 12:33:37 +0200 Subject: [PATCH] lexer can now read single character tokens --- src/lexer/lexer.cpp | 34 ++++++++++++++++++++++++++++++++++ src/lexer/lexer.hpp | 3 +++ src/token/token.hpp | 4 ++++ test/lexer.cpp | 4 +++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/lexer/lexer.cpp b/src/lexer/lexer.cpp index e69de29..874969a 100644 --- a/src/lexer/lexer.cpp +++ b/src/lexer/lexer.cpp @@ -0,0 +1,34 @@ +#include "lexer.hpp" + +#include "token/token.hpp" + +#include + +namespace lexer { + token::token lexer::next_token() { + char c; + if (!(input >> c)) + return {token::type::END_OF_FILE, ""}; + + switch (c) { + case '=': + return {token::type::ASSIGN, c}; + case '+': + return {token::type::PLUS, c}; + case ',': + return {token::type::COMMA, c}; + case ';': + return {token::type::SEMICOLON, c}; + case '(': + return {token::type::LPAREN, c}; + case ')': + return {token::type::RPAREN, c}; + case '{': + return {token::type::LBRACE, c}; + case '}': + return {token::type::RBRACE, c}; + } + return {token::type::ILLEGAL, c}; + }; + +} // namespace lexer diff --git a/src/lexer/lexer.hpp b/src/lexer/lexer.hpp index af2a122..4a6d918 100644 --- a/src/lexer/lexer.hpp +++ b/src/lexer/lexer.hpp @@ -1,7 +1,10 @@ #include "token/token.hpp" +#include + namespace lexer { struct lexer { + std::istream& input; token::token next_token(); }; } // namespace lexer diff --git a/src/token/token.hpp b/src/token/token.hpp index 79a2bec..227baac 100644 --- a/src/token/token.hpp +++ b/src/token/token.hpp @@ -8,5 +8,9 @@ namespace token { struct token { ::token::type type; std::string literal; + + token(::token::type t, std::string s): type(t), literal(s) {} + + token(::token::type t, char c): type(t), literal(1, c) {} }; } // namespace token diff --git a/test/lexer.cpp b/test/lexer.cpp index 36521e4..061099e 100644 --- a/test/lexer.cpp +++ b/test/lexer.cpp @@ -3,6 +3,7 @@ #include "token/type.hpp" #include +#include #include TEST_CASE("next token") { @@ -12,8 +13,9 @@ TEST_CASE("next token") { }; std::string input = "=+(){},;"; + std::istringstream ss(input); - lexer::lexer l{}; + lexer::lexer l{ss}; test tests[] = { {token::type::ASSIGN, "="},