lexer can now read single character tokens
This commit is contained in:
@ -0,0 +1,34 @@
|
|||||||
|
#include "lexer.hpp"
|
||||||
|
|
||||||
|
#include "token/token.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
#include "token/token.hpp"
|
#include "token/token.hpp"
|
||||||
|
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
namespace lexer {
|
namespace lexer {
|
||||||
struct lexer {
|
struct lexer {
|
||||||
|
std::istream& input;
|
||||||
token::token next_token();
|
token::token next_token();
|
||||||
};
|
};
|
||||||
} // namespace lexer
|
} // namespace lexer
|
||||||
|
@ -8,5 +8,9 @@ namespace token {
|
|||||||
struct token {
|
struct token {
|
||||||
::token::type type;
|
::token::type type;
|
||||||
std::string literal;
|
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
|
} // namespace token
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "token/type.hpp"
|
#include "token/type.hpp"
|
||||||
|
|
||||||
#include <doctest.h>
|
#include <doctest.h>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
TEST_CASE("next token") {
|
TEST_CASE("next token") {
|
||||||
@ -12,8 +13,9 @@ TEST_CASE("next token") {
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::string input = "=+(){},;";
|
std::string input = "=+(){},;";
|
||||||
|
std::istringstream ss(input);
|
||||||
|
|
||||||
lexer::lexer l{};
|
lexer::lexer l{ss};
|
||||||
|
|
||||||
test tests[] = {
|
test tests[] = {
|
||||||
{token::type::ASSIGN, "="},
|
{token::type::ASSIGN, "="},
|
||||||
|
Reference in New Issue
Block a user