Support procedure calls without arguments

This commit is contained in:
2024-03-26 23:04:20 +01:00
parent 30f80bcb88
commit 12869f0ec7
13 changed files with 317 additions and 113 deletions

View File

@ -19,7 +19,10 @@ namespace elna::source
void empty_visitor::visit(call_statement *statement)
{
statement->arguments().accept(this);
for (auto& argument : statement->arguments())
{
argument->accept(this);
}
}
void empty_visitor::visit(compound_statement *statement)
@ -87,8 +90,8 @@ namespace elna::source
{
}
declaration::declaration(const std::string& identifier)
: m_identifier(identifier)
declaration::declaration(const std::string& identifier, const std::string& type)
: m_identifier(identifier), m_type(type)
{
}
@ -102,6 +105,11 @@ namespace elna::source
return m_identifier;
}
std::string& declaration::type() noexcept
{
return m_type;
}
definition::definition(const std::string& identifier)
: m_identifier(identifier)
{
@ -142,12 +150,16 @@ namespace elna::source
return *m_body;
}
std::vector<std::unique_ptr<declaration>>& procedure_definition::parameters() noexcept
{
return m_parameters;
}
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_table(std::make_shared<symbol_table>())
m_declarations(std::move(declarations)), m_body(std::move(body))
{
}
@ -171,11 +183,6 @@ namespace elna::source
return m_declarations;
}
std::shared_ptr<symbol_table> block::table()
{
return m_table;
}
program::program(std::vector<std::unique_ptr<definition>>&& definitions,
std::vector<std::unique_ptr<declaration>>&& declarations,
std::unique_ptr<statement>&& body)
@ -276,8 +283,8 @@ namespace elna::source
return m_operator;
}
call_statement::call_statement(const std::string& name, std::unique_ptr<expression>&& body)
: m_name(name), m_body(std::move(body))
call_statement::call_statement(const std::string& name)
: m_name(name)
{
}
@ -291,9 +298,9 @@ namespace elna::source
return m_name;
}
expression& call_statement::arguments()
std::vector<std::unique_ptr<expression>>& call_statement::arguments() noexcept
{
return *m_body;
return m_arguments;
}
compound_statement::compound_statement(std::vector<std::unique_ptr<statement>>&& statements)
@ -537,7 +544,8 @@ namespace elna::source
{
return nullptr;
}
return std::make_unique<declaration>(declaration_identifier.value().get().identifier());
return std::make_unique<declaration>(declaration_identifier.value().get().identifier(),
type_identifier.value().get().identifier());
}
std::unique_ptr<statement> parser::parse_statement()
@ -573,11 +581,27 @@ namespace elna::source
{
return nullptr;
}
auto bang_body = parse_expression();
auto call = std::make_unique<call_statement>(function_name->get().identifier());
std::unique_ptr<expression> argument_expression;
if (bang_body != nullptr && iterator.skip(token::type::right_paren))
if (iterator.current(token::type::right_paren))
{
return std::make_unique<call_statement>(function_name->get().identifier(), std::move(bang_body));
++iterator;
return call;
}
while ((argument_expression = parse_expression()) != nullptr)
{
call->arguments().push_back(std::move(argument_expression));
if (iterator.current(token::type::right_paren))
{
++iterator;
return call;
}
if (!iterator.skip(token::type::comma))
{
break;
}
}
return nullptr;
}