Allow extern variables

This commit is contained in:
2025-08-20 21:47:50 +02:00
parent 0c2a396320
commit 809e41bcc3
8 changed files with 45 additions and 12 deletions

View File

@@ -415,11 +415,23 @@ namespace elna::boot
{
}
variable_declaration::variable_declaration(const struct position position,
std::vector<identifier_definition>&& identifier, std::shared_ptr<type_expression> variable_type,
std::monostate)
: node(position), m_variable_type(variable_type), identifiers(std::move(identifier)), is_extern(true)
{
}
void variable_declaration::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
bool variable_declaration::has_initializer() const
{
return this->is_extern || this->body != nullptr;
}
type_expression& variable_declaration::variable_type()
{
return *m_variable_type;

View File

@@ -508,6 +508,12 @@ variable_declaration:
std::shared_ptr<boot::type_expression> shared_type{ $3 };
$$ = new boot::variable_declaration( boot::make_position(@2), std::move($1), shared_type);
}
| identifier_definitions ":" type_expression ":=" "extern" ";"
{
std::shared_ptr<boot::type_expression> shared_type{ $3 };
$$ = new boot::variable_declaration( boot::make_position(@2), std::move($1), shared_type,
std::monostate{});
}
| identifier_definitions ":" type_expression ":=" expression ";"
{
std::shared_ptr<boot::type_expression> shared_type{ $3 };
@@ -550,7 +556,7 @@ import_declarations:
std::swap($$, $3);
$$.emplace($$.cbegin(), new boot::import_declaration(boot::make_position(@1), std::move($1)));
}
| import_declaration
| import_declaration
{
$$.emplace_back(new boot::import_declaration(boot::make_position(@1), std::move($1)));
}

View File

@@ -609,7 +609,7 @@ namespace elna::boot
void declaration_visitor::visit(variable_declaration *declaration)
{
if (declaration->body != nullptr && declaration->identifiers.size() > 1)
if (declaration->has_initializer() && declaration->identifiers.size() > 1)
{
add_error<variable_initializer_error>(this->input_file, declaration->position());
}