Assign variables
This commit is contained in:
@ -3,6 +3,60 @@
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
void empty_visitor::visit(declaration *declaration)
|
||||
{
|
||||
}
|
||||
|
||||
void empty_visitor::visit(definition *definition)
|
||||
{
|
||||
definition->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(bang_statement *statement)
|
||||
{
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(compound_statement *statement)
|
||||
{
|
||||
for (auto& nested_statement : statement->statements())
|
||||
{
|
||||
nested_statement->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(assign_statement *statement)
|
||||
{
|
||||
statement->rvalue().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(block *block)
|
||||
{
|
||||
for (const auto& block_definition : block->definitions())
|
||||
{
|
||||
block_definition->accept(this);
|
||||
}
|
||||
for (const auto& block_declaration : block->declarations())
|
||||
{
|
||||
block_declaration->accept(this);
|
||||
}
|
||||
block->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(binary_expression *expression)
|
||||
{
|
||||
expression->lhs().accept(this);
|
||||
expression->rhs().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(variable_expression *variable)
|
||||
{
|
||||
}
|
||||
|
||||
void empty_visitor::visit(integer_literal *number)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
@ -48,7 +102,9 @@ namespace elna::source
|
||||
block::block(std::vector<std::unique_ptr<definition>>&& definitions,
|
||||
std::vector<std::unique_ptr<declaration>>&& declarations,
|
||||
std::unique_ptr<statement>&& body)
|
||||
: m_definitions(std::move(definitions)), m_declarations(std::move(declarations)), m_body(std::move(body))
|
||||
: m_definitions(std::move(definitions)),
|
||||
m_declarations(std::move(declarations)), m_body(std::move(body)),
|
||||
m_table(std::make_shared<symbol_table>())
|
||||
{
|
||||
}
|
||||
|
||||
@ -72,6 +128,11 @@ namespace elna::source
|
||||
return m_declarations;
|
||||
}
|
||||
|
||||
std::shared_ptr<symbol_table> block::table()
|
||||
{
|
||||
return m_table;
|
||||
}
|
||||
|
||||
integer_literal::integer_literal(const std::int32_t value)
|
||||
: m_number(value)
|
||||
{
|
||||
@ -175,13 +236,28 @@ namespace elna::source
|
||||
return m_statements;
|
||||
}
|
||||
|
||||
void assignment_statement::accept(parser_visitor *visitor)
|
||||
void assign_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
parser::parser(const std::vector<token>& tokens)
|
||||
: tokens(tokens.cbegin()), end(tokens.cend())
|
||||
assign_statement::assign_statement(const std::string& lvalue, std::unique_ptr<expression>&& rvalue)
|
||||
: m_lvalue(lvalue), m_rvalue(std::move(rvalue))
|
||||
{
|
||||
}
|
||||
|
||||
std::string& assign_statement::lvalue() noexcept
|
||||
{
|
||||
return m_lvalue;
|
||||
}
|
||||
|
||||
expression& assign_statement::rvalue()
|
||||
{
|
||||
return *m_rvalue;
|
||||
}
|
||||
|
||||
parser::parser(lexer&& tokens)
|
||||
: iterator(std::move(tokens))
|
||||
{
|
||||
}
|
||||
|
||||
@ -190,27 +266,32 @@ namespace elna::source
|
||||
return parse_block();
|
||||
}
|
||||
|
||||
const std::list<std::unique_ptr<error>>& parser::errors() const noexcept
|
||||
{
|
||||
return iterator.errors();
|
||||
}
|
||||
|
||||
std::unique_ptr<expression> parser::parse_factor()
|
||||
{
|
||||
if (tokens->of() == source::token::type::identifier)
|
||||
if (iterator->of() == source::token::type::identifier)
|
||||
{
|
||||
auto result = std::make_unique<variable_expression>(tokens->identifier());
|
||||
++tokens;
|
||||
auto result = std::make_unique<variable_expression>(iterator->identifier());
|
||||
++iterator;
|
||||
return result;
|
||||
}
|
||||
else if (tokens->of() == source::token::token::type::number)
|
||||
else if (iterator->of() == source::token::token::type::number)
|
||||
{
|
||||
auto result = std::make_unique<integer_literal>(tokens->number());
|
||||
++tokens;
|
||||
auto result = std::make_unique<integer_literal>(iterator->number());
|
||||
++iterator;
|
||||
return result;
|
||||
}
|
||||
else if (tokens->of() == source::token::type::left_paren)
|
||||
else if (iterator->of() == source::token::type::left_paren)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
|
||||
auto expression = parse_expression();
|
||||
|
||||
++tokens;
|
||||
++iterator;
|
||||
|
||||
return expression;
|
||||
}
|
||||
@ -220,14 +301,14 @@ namespace elna::source
|
||||
std::unique_ptr<expression> parser::parse_term()
|
||||
{
|
||||
auto lhs = parse_factor();
|
||||
if (lhs == nullptr || tokens == end || tokens->of() != source::token::type::factor_operator)
|
||||
if (lhs == nullptr || iterator.current().of() != source::token::type::factor_operator)
|
||||
{
|
||||
return lhs;
|
||||
}
|
||||
while (tokens->of() == source::token::type::factor_operator)
|
||||
while (iterator->of() == source::token::type::factor_operator)
|
||||
{
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
auto _operator = iterator->identifier()[0];
|
||||
++iterator;
|
||||
|
||||
auto rhs = parse_factor();
|
||||
lhs = std::make_unique<binary_expression>(std::move(lhs),
|
||||
@ -239,14 +320,14 @@ namespace elna::source
|
||||
std::unique_ptr<expression> parser::parse_expression()
|
||||
{
|
||||
auto term = parse_term();
|
||||
if (term == nullptr || tokens == end || tokens->of() != source::token::type::term_operator)
|
||||
if (term == nullptr || iterator.current().of() != source::token::type::term_operator)
|
||||
{
|
||||
return term;
|
||||
}
|
||||
while (tokens->of() == source::token::type::term_operator)
|
||||
while (iterator->of() == source::token::type::term_operator)
|
||||
{
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
auto _operator = iterator->identifier()[0];
|
||||
++iterator;
|
||||
|
||||
auto rhs = parse_term();
|
||||
term = std::make_unique<binary_expression>(std::move(term),
|
||||
@ -257,22 +338,22 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<definition> parser::parse_definition()
|
||||
{
|
||||
auto definition_identifier = advance(token::type::identifier);
|
||||
auto definition_identifier = iterator.advance(token::type::identifier);
|
||||
|
||||
if (!definition_identifier.has_value())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
if (!skip(token::type::equals))
|
||||
if (!iterator.skip(token::type::equals))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (tokens->of() == source::token::type::number)
|
||||
if (iterator->of() == source::token::type::number)
|
||||
{
|
||||
auto result = std::make_unique<definition>(definition_identifier.value().get().identifier(),
|
||||
std::make_unique<integer_literal>(tokens->number()));
|
||||
++tokens;
|
||||
std::make_unique<integer_literal>(iterator->number()));
|
||||
++iterator;
|
||||
return result;
|
||||
}
|
||||
return nullptr;
|
||||
@ -280,7 +361,7 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<declaration> parser::parse_declaration()
|
||||
{
|
||||
auto declaration_identifier = advance(token::type::identifier);
|
||||
auto declaration_identifier = iterator.advance(token::type::identifier);
|
||||
|
||||
if (!declaration_identifier.has_value())
|
||||
{
|
||||
@ -291,21 +372,25 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<statement> parser::parse_statement()
|
||||
{
|
||||
if (tokens->of() == source::token::type::bang)
|
||||
if (iterator.look_ahead(token::type::assignment))
|
||||
{
|
||||
return parse_assign_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::bang))
|
||||
{
|
||||
return parse_bang_statement();
|
||||
}
|
||||
else if (tokens->of() == source::token::type::begin)
|
||||
else if (iterator.current(token::type::begin))
|
||||
{
|
||||
return parse_compound_statement();
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(unexpected_token{ *tokens }));
|
||||
iterator.add_error(*iterator);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<bang_statement> parser::parse_bang_statement()
|
||||
{
|
||||
if (!advance(token::type::bang))
|
||||
if (!iterator.advance(token::type::bang))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
@ -321,7 +406,7 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<compound_statement> parser::parse_compound_statement()
|
||||
{
|
||||
if (!advance(token::type::begin))
|
||||
if (!iterator.advance(token::type::begin))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
@ -332,18 +417,18 @@ namespace elna::source
|
||||
{
|
||||
result->statements().push_back(std::move(next_statement));
|
||||
|
||||
if (tokens->of() == token::type::semicolon)
|
||||
if (iterator->of() == token::type::semicolon)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
}
|
||||
else if (tokens->of() == token::type::end)
|
||||
else if (iterator->of() == token::type::end)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
iterator.add_error(*iterator);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -351,33 +436,49 @@ namespace elna::source
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unique_ptr<assign_statement> parser::parse_assign_statement()
|
||||
{
|
||||
auto name = iterator.advance(token::type::identifier);
|
||||
if (!name.has_value() || !iterator.skip(token::type::assignment))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto rvalue = parse_expression();
|
||||
|
||||
if (rvalue == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<assign_statement>(name.value().get().identifier(), std::move(rvalue));
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<definition>> parser::parse_definitions()
|
||||
{
|
||||
std::vector<std::unique_ptr<definition>> definitions;
|
||||
|
||||
if (tokens->of() != token::type::let)
|
||||
if (iterator->of() != token::type::let)
|
||||
{
|
||||
return definitions;
|
||||
}
|
||||
++tokens; // Skip const.
|
||||
++iterator; // Skip const.
|
||||
|
||||
std::unique_ptr<definition> parsed_definition;
|
||||
while ((parsed_definition = parse_definition()) != nullptr)
|
||||
{
|
||||
definitions.push_back(std::move(parsed_definition));
|
||||
|
||||
if (tokens->of() == source::token::type::comma)
|
||||
if (iterator->of() == source::token::type::comma)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
}
|
||||
else if (tokens->of() == source::token::type::semicolon)
|
||||
else if (iterator->of() == source::token::type::semicolon)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
iterator.add_error(*iterator);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -388,29 +489,29 @@ namespace elna::source
|
||||
{
|
||||
std::vector<std::unique_ptr<declaration>> declarations;
|
||||
|
||||
if (tokens->of() != token::type::var)
|
||||
if (iterator->of() != token::type::var)
|
||||
{
|
||||
return declarations;
|
||||
}
|
||||
++tokens; // Skip var.
|
||||
++iterator; // Skip var.
|
||||
|
||||
std::unique_ptr<declaration> parsed_declaration;
|
||||
while ((parsed_declaration = parse_declaration()) != nullptr)
|
||||
{
|
||||
declarations.push_back(std::move(parsed_declaration));
|
||||
|
||||
if (tokens->of() == token::type::comma)
|
||||
if (iterator->of() == token::type::comma)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
}
|
||||
else if (tokens->of() == token::type::semicolon)
|
||||
else if (iterator->of() == token::type::semicolon)
|
||||
{
|
||||
++tokens;
|
||||
++iterator;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
iterator.add_error(*iterator);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -430,25 +531,4 @@ namespace elna::source
|
||||
return std::make_unique<block>(std::move(definitions),
|
||||
std::move(declarations), std::move(parsed_statement));
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<const token>> parser::advance(const token::type token_type)
|
||||
{
|
||||
if (tokens->of() == token_type)
|
||||
{
|
||||
return std::make_optional<>(std::cref(*tokens++));
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
return std::optional<std::reference_wrapper<const token>>();
|
||||
}
|
||||
|
||||
bool parser::skip(const token::type token_type)
|
||||
{
|
||||
if (tokens->of() == token_type)
|
||||
{
|
||||
++tokens;
|
||||
return true;
|
||||
}
|
||||
errors.push_back(std::make_unique<unexpected_token>(*tokens));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user