Skip to content

feat: new parallel parameter values support #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module(
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "magic_enum", version = "0.9.3")
bazel_dep(name = "ecsact_runtime", version = "0.6.4")
bazel_dep(name = "ecsact_parse", version = "0.5.0")
bazel_dep(name = "ecsact_runtime", version = "0.6.5")
bazel_dep(name = "ecsact_parse", version = "0.5.1")

bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
Expand Down
81 changes: 77 additions & 4 deletions ecsact/interpret/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ auto statement_param<bool>( //
return result;
}

template<>
auto statement_param<std::string_view>( //
const ecsact_statement& statement,
std::string_view param_name
) -> std::optional<std::string_view> {
auto result = std::optional<std::string_view>{};
for(auto& param : view_statement_params(statement)) {
if(std::string_view{
param.name.data,
static_cast<size_t>(param.name.length)
} != param_name) {
continue;
}

if(param.value.type != ECSACT_STATEMENT_PARAM_VALUE_TYPE_STRING) {
break;
}

result = std::string_view{
param.value.data.string_value.data,
static_cast<size_t>(param.value.data.string_value.length),
};
}

return result;
}

template<typename FirstT, typename SecondT>
auto statement_param( //
const ecsact_statement& statement,
Expand All @@ -162,6 +189,38 @@ auto statement_param( //
return std::nullopt;
}

static auto parallel_param(const ecsact_statement& statement
) -> std::variant<ecsact_parallel_execution, ecsact_eval_error_code> {
using result_t =
std::variant<ecsact_parallel_execution, ecsact_eval_error_code>;

auto param = statement_param<bool, std::string_view>(statement, "parallel");
if(!param) {
return ECSACT_PAR_EXEC_AUTO;
}

auto result = std::visit(
overloaded{
[&](bool param) -> result_t {
return param ? ECSACT_PAR_EXEC_PREFERRED : ECSACT_PAR_EXEC_DENY;
},
[&](std::string_view param) -> result_t {
if(param == "auto") {
return ECSACT_PAR_EXEC_AUTO;
} else if(param == "preferred") {
return ECSACT_PAR_EXEC_PREFERRED;
} else if(param == "deny") {
return ECSACT_PAR_EXEC_DENY;
} else {
return ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE;
}
}
},
*param
);
return result;
}

auto allow_statement_params( //
const ecsact_statement& statement,
const ecsact_statement* context,
Expand Down Expand Up @@ -803,10 +862,17 @@ static ecsact_eval_error eval_system_statement(
ecsact_set_system_lazy_iteration_rate(sys_id, lazy_value);
}

auto parallel = statement_param<bool>(statement, "parallel").value_or(false);
auto parallel = parallel_param(statement);
if(auto err_code = std::get_if<ecsact_eval_error_code>(&parallel)) {
return ecsact_eval_error{
.code = *err_code,
.relevant_content = data.system_name,
};
}

ecsact_set_system_parallel_execution(
ecsact_id_cast<ecsact_system_like_id>(sys_id),
parallel
std::get<ecsact_parallel_execution>(parallel)
);

return {};
Expand Down Expand Up @@ -845,10 +911,17 @@ static ecsact_eval_error eval_action_statement(
data.action_name.length
);

auto parallel = statement_param<bool>(statement, "parallel").value_or(false);
auto parallel = parallel_param(statement);
if(auto err_code = std::get_if<ecsact_eval_error_code>(&parallel)) {
return ecsact_eval_error{
.code = *err_code,
.relevant_content = data.action_name,
};
}

ecsact_set_system_parallel_execution(
ecsact_id_cast<ecsact_system_like_id>(act_id),
parallel
std::get<ecsact_parallel_execution>(parallel)
);

return {};
Expand Down
3 changes: 3 additions & 0 deletions ecsact/interpret/eval_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ typedef enum ecsact_eval_error_code {
/// currently not allowed.
ECSACT_EVAL_ERR_SAME_FIELDS_SYSTEM_ASSOCIATION,

/// Statement supports the given parameter name but the value is invalid
ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,

/// Internal error. Should not happen and is an indiciation of a bug.
ECSACT_EVAL_ERR_INTERNAL = 999,

Expand Down
8 changes: 4 additions & 4 deletions parse-resolver-runtime/parse-resolver-runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct system_like {
/** in execution order */
std::vector<ecsact_system_id> nested_systems;

bool parallel_execution = false;
ecsact_parallel_execution parallel_execution = {};
};

struct action_def : composite, system_like {
Expand Down Expand Up @@ -1073,14 +1073,14 @@ int32_t ecsact_meta_get_lazy_iteration_rate( //
}

void ecsact_set_system_parallel_execution( //
ecsact_system_like_id system_like_id,
bool parallel_execution
ecsact_system_like_id system_like_id,
ecsact_parallel_execution parallel_execution
) {
auto& def = get_system_like(system_like_id);
def.parallel_execution = parallel_execution;
}

bool ecsact_meta_get_system_parallel_execution( //
ecsact_parallel_execution ecsact_meta_get_system_parallel_execution( //
ecsact_system_like_id system_like_id
) {
auto& def = get_system_like(system_like_id);
Expand Down
4 changes: 2 additions & 2 deletions test/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module(name = "ecsact_interpret_test")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "ecsact_parse", version = "0.5.0")
bazel_dep(name = "ecsact_runtime", version = "0.6.4")
bazel_dep(name = "ecsact_parse", version = "0.5.1")
bazel_dep(name = "ecsact_runtime", version = "0.6.5")

bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
Expand Down
3 changes: 2 additions & 1 deletion test/errors/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("@ecsact_interpret//bazel:copts.bzl", "copts")
load("@rules_cc//cc:defs.bzl", "cc_test")

# buildifier: keep sorted
_TESTS = [
Expand All @@ -9,6 +9,7 @@ _TESTS = [
"no_package_statement_first",
"unknown_association_field",
"unknown_component_notify_settings",
"unknown_param_value",
]

[cc_test(
Expand Down
11 changes: 11 additions & 0 deletions test/errors/unknown_param_value.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "gtest/gtest.h"
#include "ecsact/interpret/eval.h"

#include "test_lib.hh"

TEST(UnknownParamValue, UnknownParallelParam) {
auto errs =
ecsact_interpret_test_files({"errors/unknown_param_value.ecsact"});
ASSERT_EQ(errs.size(), 1);
ASSERT_EQ(errs[0].eval_error, ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE);
}
7 changes: 7 additions & 0 deletions test/errors/unknown_param_value.ecsact
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package unknown_param_value;

component A { i32 a; };

system UnknownParamValue(parallel: what) {
readwrite A;
}
16 changes: 13 additions & 3 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ TEST(EcsactParseRuntimeInterop, Simple) {
field_id
);
ASSERT_STREQ(assoc_field_name, "test_entity");

ASSERT_EQ(
ecsact_meta_get_system_parallel_execution(
ecsact_id_cast<ecsact_system_like_id>(sys_id)
),
ECSACT_PAR_EXEC_AUTO
);
}},
{"TestNestedSystem",
[&](ecsact_system_id sys_id) {
Expand All @@ -157,9 +164,12 @@ TEST(EcsactParseRuntimeInterop, Simple) {
);
ASSERT_EQ(sys_caps[0], ECSACT_SYS_CAP_READWRITE);

ASSERT_TRUE(ecsact_meta_get_system_parallel_execution(
ecsact_id_cast<ecsact_system_like_id>(sys_id)
));
ASSERT_EQ(
ecsact_meta_get_system_parallel_execution(
ecsact_id_cast<ecsact_system_like_id>(sys_id)
),
ECSACT_PAR_EXEC_PREFERRED
);
}},
};

Expand Down