Check assignment type

This commit is contained in:
Eugen Wissner 2024-07-07 18:41:54 +02:00
parent 4dbc5aca0d
commit 0dbbd3f403
4 changed files with 16 additions and 4 deletions

3
TODO
View File

@ -3,7 +3,6 @@
- Catch exceptions thrown by the argument parser and print them normally.
- Structs or records.
- Unions.
- Type checking.
- Support immediates greater than 12 bits.
- It seems instructions are correctly encoded only if the compiler is running
on a little endian architecture.
@ -13,5 +12,3 @@
- Error message with an empty file wrongly says that a ")" is expected.
- Support any expressions for constants.
- Name analysis should fail if there are undefined symbols.
- type_mismatch error should get second type argument for the
expected type.

View File

@ -137,7 +137,8 @@ namespace elna::source
argument,
arithmetic,
comparison,
condition
condition,
assignment
};
/**

View File

@ -88,5 +88,6 @@ namespace elna::source
void visit(constant_definition *definition) override;
void visit(while_statement *statement) override;
void visit(if_statement *statement) override;
void visit(assign_statement *statement) override;
};
}

View File

@ -305,6 +305,19 @@ namespace elna::source
statement->body().accept(this);
}
void type_analysis_visitor::visit(assign_statement *statement)
{
statement->rvalue().accept(this);
auto lvalue_info = std::dynamic_pointer_cast<typed_info>(this->table->lookup(statement->lvalue()));
if (statement->rvalue().data_type != nullptr && lvalue_info->type() == statement->rvalue().data_type)
{
auto new_error = std::make_unique<type_mismatch>(statement->rvalue().data_type,
type_mismatch::operation::assignment, this->filename, statement->position());
m_errors.push_back(std::move(new_error));
}
}
const std::list<std::unique_ptr<error>>& type_analysis_visitor::errors() const noexcept
{
return m_errors;