elna/source/parser.cpp

295 lines
7.1 KiB
C++
Raw Normal View History

#include "elna/source/parser.hpp"
2024-03-01 10:13:55 +01:00
#include <stdexcept>
namespace elna
{
namespace source
2024-03-01 10:13:55 +01:00
{
/**
* AST node.
*/
void node::accept(ParserVisitor *)
2024-03-01 10:13:55 +01:00
{
}
definition::definition(std::string&& identifier, std::unique_ptr<integer_literal>&& body)
: m_identifier(std::move(identifier)), m_body(std::move(body))
2024-03-01 10:13:55 +01:00
{
}
void definition::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
std::string& definition::identifier() noexcept
{
return m_identifier;
}
integer_literal& definition::body()
{
return *m_body;
}
block::block(std::vector<std::unique_ptr<definition>>&& definitions, std::unique_ptr<statement>&& body)
: m_definitions(std::move(definitions)), m_body(std::move(body))
{
}
void block::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
statement& block::body()
{
return *m_body;
}
std::vector<std::unique_ptr<definition>>& block::definitions() noexcept
{
return m_definitions;
}
integer_literal::integer_literal(const std::int32_t value)
: m_number(value)
{
}
void integer_literal::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
std::int32_t integer_literal::number() const noexcept
{
return m_number;
}
variable::variable(const std::string& name)
: m_name(name)
{
}
void variable::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
const std::string& variable::name() const noexcept
2024-03-01 10:13:55 +01:00
{
return m_name;
}
2024-03-01 10:13:55 +01:00
binary_expression::binary_expression(std::unique_ptr<expression>&& lhs,
std::unique_ptr<expression>&& rhs, const unsigned char operation)
: m_lhs(std::move(lhs)), m_rhs(std::move(rhs))
{
switch (operation)
2024-03-01 10:13:55 +01:00
{
2024-03-03 13:11:39 +01:00
case '+':
this->m_operator = binary_operator::sum;
2024-03-03 13:11:39 +01:00
break;
case '-':
this->m_operator = binary_operator::subtraction;
2024-03-03 13:11:39 +01:00
break;
case '*':
this->m_operator = binary_operator::multiplication;
2024-03-03 13:11:39 +01:00
break;
case '/':
this->m_operator = binary_operator::division;
2024-03-03 13:11:39 +01:00
break;
default:
throw std::logic_error("Invalid binary operator");
2024-03-01 10:13:55 +01:00
}
}
void binary_expression::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
expression& binary_expression::lhs()
{
return *m_lhs;
}
expression& binary_expression::rhs()
{
return *m_rhs;
}
binary_operator binary_expression::operation() const noexcept
{
return m_operator;
}
bang_statement::bang_statement(std::unique_ptr<expression>&& body)
: m_body(std::move(body))
{
}
void bang_statement::accept(ParserVisitor *visitor)
2024-03-01 10:13:55 +01:00
{
visitor->visit(this);
}
expression& bang_statement::body()
{
return *m_body;
}
parser::parser(const std::vector<token>& tokens)
: tokens(tokens.cbegin()), end(tokens.cend())
{
}
std::unique_ptr<block> parser::parse()
2024-03-01 10:13:55 +01:00
{
return parse_block();
2024-03-01 10:13:55 +01:00
}
std::unique_ptr<expression> parser::parse_factor()
2024-03-01 10:13:55 +01:00
{
if (tokens->of() == source::token::type::identifier)
2024-03-01 10:13:55 +01:00
{
auto result = std::make_unique<variable>(tokens->identifier());
++tokens;
return result;
2024-03-01 10:13:55 +01:00
}
else if (tokens->of() == source::token::token::type::number)
2024-03-01 10:13:55 +01:00
{
auto result = std::make_unique<integer_literal>(tokens->number());
++tokens;
return result;
2024-03-01 10:13:55 +01:00
}
else if (tokens->of() == source::token::type::left_paren)
2024-03-01 10:13:55 +01:00
{
++tokens;
2024-03-01 10:13:55 +01:00
auto expression = parse_expression();
2024-03-01 10:13:55 +01:00
++tokens;
2024-03-01 10:13:55 +01:00
return expression;
}
return nullptr;
}
std::unique_ptr<expression> parser::parse_term()
2024-03-01 10:13:55 +01:00
{
auto lhs = parse_factor();
if (lhs == nullptr || tokens == end || tokens->of() != source::token::type::factor_operator)
2024-03-03 13:11:39 +01:00
{
return lhs;
}
auto _operator = tokens->identifier()[0];
++tokens;
2024-03-03 13:11:39 +01:00
auto rhs = parse_factor();
2024-03-03 13:11:39 +01:00
if (rhs != nullptr)
{
return std::make_unique<binary_expression>(std::move(lhs),
std::move(rhs), _operator);
2024-03-03 13:11:39 +01:00
}
return nullptr;
2024-03-01 10:13:55 +01:00
}
std::unique_ptr<expression> parser::parse_expression()
2024-03-01 10:13:55 +01:00
{
auto term = parse_term();
if (term == nullptr || tokens == end || tokens->of() != source::token::type::term_operator)
2024-03-01 10:13:55 +01:00
{
return term;
}
auto _operator = tokens->identifier()[0];
++tokens;
2024-03-01 10:13:55 +01:00
auto rhs = parse_expression();
2024-03-01 10:13:55 +01:00
if (rhs != nullptr)
2024-03-01 10:13:55 +01:00
{
return std::make_unique<binary_expression>(std::move(term),
std::move(rhs), _operator);
2024-03-01 10:13:55 +01:00
}
2024-03-03 13:11:39 +01:00
return nullptr;
2024-03-01 10:13:55 +01:00
}
std::unique_ptr<definition> parser::parse_definition()
2024-03-01 10:13:55 +01:00
{
std::string definition_identifier = tokens->identifier(); // Copy.
2024-03-01 10:13:55 +01:00
++tokens;
++tokens; // Skip the equals sign.
2024-03-01 10:13:55 +01:00
if (tokens->of() == source::token::type::number)
2024-03-01 10:13:55 +01:00
{
auto result = std::make_unique<definition>(std::move(definition_identifier),
std::make_unique<integer_literal>(tokens->number()));
++tokens;
return result;
2024-03-01 10:13:55 +01:00
}
return nullptr;
}
std::unique_ptr<statement> parser::parse_bang_statement()
2024-03-01 10:13:55 +01:00
{
if (tokens->of() == source::token::type::bang)
2024-03-01 10:13:55 +01:00
{
++tokens;
auto bang_body = parse_expression();
if (bang_body != nullptr)
2024-03-01 10:13:55 +01:00
{
return std::make_unique<bang_statement>(std::move(bang_body));
2024-03-01 10:13:55 +01:00
}
}
return nullptr;
}
std::vector<std::unique_ptr<definition>> parser::parse_definitions()
2024-03-01 10:13:55 +01:00
{
++tokens; // Skip const.
2024-03-01 10:13:55 +01:00
std::vector<std::unique_ptr<definition>> definitions;
2024-03-01 10:13:55 +01:00
while (tokens != end)
2024-03-01 10:13:55 +01:00
{
auto parsed_definition = parse_definition();
if (parsed_definition == nullptr)
2024-03-01 10:13:55 +01:00
{
return definitions;
2024-03-01 10:13:55 +01:00
}
definitions.push_back(std::move(parsed_definition));
2024-03-01 10:13:55 +01:00
if (tokens->of() == source::token::type::semicolon)
2024-03-01 10:13:55 +01:00
{
break;
}
if (tokens->of() == source::token::type::comma)
2024-03-01 10:13:55 +01:00
{
++tokens;
2024-03-01 10:13:55 +01:00
}
}
return definitions;
}
std::unique_ptr<block> parser::parse_block()
2024-03-01 10:13:55 +01:00
{
std::vector<std::unique_ptr<definition>> definitions;
if (tokens->of() == source::token::type::let)
2024-03-01 10:13:55 +01:00
{
definitions = parse_definitions();
++tokens;
2024-03-01 10:13:55 +01:00
}
auto parsed_statement = parse_bang_statement();
if (parsed_statement == nullptr)
2024-03-01 10:13:55 +01:00
{
return nullptr;
}
return std::make_unique<block>(std::move(definitions), std::move(parsed_statement));
2024-03-01 10:13:55 +01:00
}
}
}