Skip to content

Commit

Permalink
feat: check for ambiguous field type name
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Oct 1, 2023
1 parent b32900a commit 06ccf92
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
60 changes: 26 additions & 34 deletions ecsact/interpret/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,39 +222,6 @@ std::optional<ecsact_field_id> find_field_by_name(
return {};
}

std::optional<ecsact_enum_id> find_enum_by_name(
ecsact_package_id package_id,
std::string_view target_enum_name
) {
using ecsact::meta::get_enum_ids;

for(auto& enum_id : get_enum_ids(package_id)) {
std::string enum_name = ecsact_meta_enum_name(enum_id);
if(enum_name == target_enum_name) {
return enum_id;
}
}

return {};
}

std::optional<ecsact_field_type> find_user_field_type_by_name(
ecsact_package_id package_id,
std::string_view user_type_name,
int32_t length
) {
auto enum_id = find_enum_by_name(package_id, user_type_name);
if(enum_id) {
return ecsact_field_type{
.kind = ECSACT_TYPE_KIND_ENUM,
.type{.enum_id = *enum_id},
.length = length,
};
}

return {};
}

template<typename R, typename T>
static std::optional<R> cast_optional_id(std::optional<T> opt_id) {
if(opt_id) {
Expand Down Expand Up @@ -460,6 +427,24 @@ std::optional<ecsact_component_like_id> find_by_name(
return {};
}

auto find_user_field_type_by_name(
ecsact_package_id package_id,
std::string_view user_type_name,
int32_t length
) -> std::optional<ecsact_field_type> {
auto enum_id =
find_by_name<ecsact_enum_id>(package_id, std::string{user_type_name});
if(enum_id) {
return ecsact_field_type{
.kind = ECSACT_TYPE_KIND_ENUM,
.type{.enum_id = *enum_id},
.length = length,
};
}

return {};
}

auto find_field_by_full_name(
ecsact_package_id package_id,
std::string_view field_full_name
Expand Down Expand Up @@ -973,7 +958,14 @@ static ecsact_eval_error eval_user_type_field_statement(
const ecsact_statement& statement
) {
auto& data = statement.data.user_type_field_statement;
auto [context, err] = expect_context(context_stack, {ECSACT_STATEMENT_NONE});
auto [context, err] = expect_context(
context_stack,
{
ECSACT_STATEMENT_COMPONENT,
ECSACT_STATEMENT_TRANSIENT,
ECSACT_STATEMENT_ACTION,
}
);

if(err.code != ECSACT_EVAL_OK) {
err.relevant_content = data.user_type_name;
Expand Down
2 changes: 1 addition & 1 deletion test/errors/ambiguous_field_type.ecsact
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package error.ambiguous_field_type;
import Amby;

component Amby {
i32 numnum;
i32 amby;
}

component ExampleComponent {
Expand Down
4 changes: 2 additions & 2 deletions test/errors/ambiguous_field_type_import.ecsact
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package Amby;

component amby {
i32 ambys_field;
enum amby {
SOME_VAL = 10;
}

0 comments on commit 06ccf92

Please sign in to comment.