elna/source/lexer.cpp

271 lines
7.4 KiB
C++
Raw Normal View History

2024-02-22 21:29:25 +01:00
#include "elna/lexer.hpp"
namespace elna
{
source::source(const std::string& buffer)
: m_buffer(buffer)
{
}
source::const_iterator source::begin() const
{
2024-02-25 15:16:19 +01:00
return source::const_iterator(std::cbegin(m_buffer));
2024-02-22 21:29:25 +01:00
}
source::const_iterator source::end() const
{
2024-02-28 16:18:39 +01:00
Position end_position{ 0, 0 };
2024-02-22 21:29:25 +01:00
return source::const_iterator(std::cend(m_buffer), end_position);
}
2024-02-25 15:16:19 +01:00
source::const_iterator::const_iterator(std::string::const_iterator buffer,
2024-02-28 16:18:39 +01:00
const Position start_position)
2024-02-22 21:29:25 +01:00
: m_buffer(buffer), m_position(start_position)
{
}
2024-02-28 16:18:39 +01:00
const Position& source::const_iterator::position() const noexcept
2024-02-22 21:29:25 +01:00
{
return this->m_position;
}
source::const_iterator::reference source::const_iterator::operator*() const noexcept
{
return *m_buffer;
}
source::const_iterator::pointer source::const_iterator::operator->() const noexcept
{
return m_buffer.base();
}
source::const_iterator& source::const_iterator::operator++()
{
if (*this->m_buffer == '\n')
{
this->m_position.column = 1;
++this->m_position.line;
}
else
{
++this->m_position.column;
}
std::advance(this->m_buffer, 1);
return *this;
}
source::const_iterator& source::const_iterator::operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
bool source::const_iterator::operator==(const source::const_iterator& that) const noexcept
{
return this->m_buffer == that.m_buffer;
}
bool source::const_iterator::operator!=(const source::const_iterator& that) const noexcept
{
return !(*this == that);
}
2024-02-28 16:18:39 +01:00
Token::Token(const Type of, const char *value, Position position)
: m_type(of), m_position(position)
2024-02-22 21:29:25 +01:00
{
2024-02-28 16:18:39 +01:00
std::size_t value_length = strlen(value);
char *buffer = reinterpret_cast<char *>(malloc(value_length + 1));
std::memcpy(buffer, value, value_length);
buffer[value_length] = 0;
m_value.identifier = buffer;
}
Token::Token(const Type of, std::int32_t number, Position position)
: m_type(of), m_position(position)
{
m_value.number = number;
}
Token::Token(const Type of, Position position)
: m_type(of), m_position(position)
{
}
Token::Token(const Token& that)
: m_type(that.of()), m_position(that.position())
{
*this = that;
}
Token::Token(Token&& that)
: m_type(that.of()), m_position(that.position())
{
*this = std::move(that);
}
Token::~Token()
{
if (m_type == TOKEN_IDENTIFIER || m_type == TOKEN_OPERATOR)
{
std::free(const_cast<char*>(m_value.identifier));
}
}
Token& Token::operator=(const Token& that)
{
m_type = that.of();
m_position = that.position();
if (that.of() == TOKEN_IDENTIFIER || that.of() == TOKEN_OPERATOR)
{
std::size_t value_length = strlen(that.identifier());
char *buffer = reinterpret_cast<char *>(malloc(value_length + 1));
std::memcpy(buffer, that.identifier(), value_length);
buffer[value_length] = 0;
m_value.identifier = buffer;
}
else if (that.of() == TOKEN_NUMBER)
{
m_value.number = that.number();
}
return *this;
}
Token& Token::operator=(Token&& that)
{
m_type = that.of();
m_position = that.position();
if (that.of() == TOKEN_IDENTIFIER || that.of() == TOKEN_OPERATOR)
{
m_value.identifier = that.identifier();
that.m_value.identifier = nullptr;
}
else if (that.of() == TOKEN_NUMBER)
{
m_value.number = that.number();
}
return *this;
2024-02-22 21:29:25 +01:00
}
2024-02-28 16:18:39 +01:00
Token::Type Token::of() const noexcept
2024-02-22 21:29:25 +01:00
{
return m_type;
}
2024-02-28 16:18:39 +01:00
const char *Token::identifier() const noexcept
{
return m_value.identifier;
}
std::int32_t Token::number() const noexcept
2024-02-22 21:29:25 +01:00
{
2024-02-28 16:18:39 +01:00
return m_value.number;
2024-02-22 21:29:25 +01:00
}
2024-02-28 16:18:39 +01:00
const Position& Token::position() const noexcept
2024-02-22 21:29:25 +01:00
{
return m_position;
}
2024-02-28 16:18:39 +01:00
Token *lex(const char *buffer, CompileError *compile_error, std::size_t *length)
2024-02-22 21:29:25 +01:00
{
2024-02-28 16:18:39 +01:00
std::vector<Token> tokens;
2024-02-22 21:29:25 +01:00
source input{ buffer };
for (auto iterator = input.begin(); iterator != input.end();)
{
if (*iterator == ' ' || *iterator == '\n')
{
}
2024-02-28 16:18:39 +01:00
else if (std::isdigit(*iterator))
{
tokens.emplace_back(
Token::TOKEN_NUMBER,
static_cast<std::int32_t>(*iterator - '0'),
iterator.position()
);
}
else if (*iterator == '=')
{
tokens.emplace_back(Token::TOKEN_EQUALS, iterator.position());
}
else if (*iterator == '(')
2024-02-22 21:29:25 +01:00
{
2024-02-28 16:18:39 +01:00
tokens.emplace_back(Token::TOKEN_LEFT_PAREN, iterator.position());
}
else if (*iterator == ')')
{
tokens.emplace_back(Token::TOKEN_RIGHT_PAREN, iterator.position());
}
else if (*iterator == ';')
{
tokens.emplace_back(Token::TOKEN_SEMICOLON, iterator.position());
}
else if (*iterator == ',')
{
tokens.emplace_back(Token::TOKEN_COMMA, iterator.position());
}
else if (*iterator == '!')
{
tokens.emplace_back(Token::TOKEN_BANG, iterator.position());
}
else if (*iterator == '.')
{
tokens.emplace_back(Token::TOKEN_DOT, iterator.position());
}
else if (std::isalpha(*iterator))
{
std::string word;
auto i = iterator;
while (i != input.end() && std::isalpha(*i))
2024-02-22 21:29:25 +01:00
{
2024-02-28 16:18:39 +01:00
word.push_back(*i);
++i;
2024-02-22 21:29:25 +01:00
}
2024-02-28 16:18:39 +01:00
if (word == "const")
{
tokens.emplace_back(Token::TOKEN_LET, iterator.position());
}
else if (word == "var")
{
tokens.emplace_back(Token::TOKEN_VAR, iterator.position());
}
else
{
tokens.emplace_back(Token::TOKEN_IDENTIFIER, word.c_str(), iterator.position());
}
iterator = i;
2024-02-22 21:29:25 +01:00
continue;
}
2024-02-28 16:18:39 +01:00
else if (*iterator == '+' || *iterator == '-')
{
std::string _operator{ *iterator };
tokens.emplace_back(Token::TOKEN_OPERATOR, _operator.c_str(), iterator.position());
}
else
{
*compile_error = CompileError("Unexpected next character", iterator.position());
return nullptr;
}
2024-02-22 21:29:25 +01:00
++iterator;
}
2024-02-28 16:18:39 +01:00
Token *target = reinterpret_cast<Token *>(malloc(tokens.size() * sizeof(Token) + sizeof(Token)));
int i = 0;
for (auto& token : tokens)
{
target[i] = std::move(token);
++i;
}
*length = i;
2024-02-22 21:29:25 +01:00
2024-02-28 16:18:39 +01:00
return target;
2024-02-22 21:29:25 +01:00
}
}