diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-08-14 11:43:28 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2023-08-14 12:09:57 +0200 |
commit | 726e70cb9bf544ee3164e32405e4dd4f750c0403 (patch) | |
tree | 7e15485967af5f24852e30d54059b62a40e8928d /src | |
parent | 755360ab129879c8981c84262ef1f1d3e697a5e0 (diff) | |
download | justbuild-726e70cb9bf544ee3164e32405e4dd4f750c0403.tar.gz |
expression: add new built in "set"
... to obtain from a list of strings a map with those entries
as keys and true as value. In this way, repeated membership tests
in lists can be implemented more efficiently.
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/buildtool/build_engine/expression/evaluator.cpp b/src/buildtool/build_engine/expression/evaluator.cpp index 6aeea591..431792e8 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -167,6 +167,24 @@ auto Enumerate(ExpressionPtr const& expr) -> ExpressionPtr { return ExpressionPtr{Expression::map_t{result}}; } +auto Set(ExpressionPtr const& expr) -> ExpressionPtr { + if (not expr->IsList()) { + throw Evaluator::EvaluationError{ + fmt::format("set expects list of strings but instead got: {}.", + expr->ToString())}; + } + auto result = Expression::map_t::underlying_map_t{}; + for (auto const& entry : expr->List()) { + if (not entry->IsString()) { + throw Evaluator::EvaluationError{ + fmt::format("set expects list of strings found entry: {}.", + entry->ToString())}; + } + result[entry->String()] = Expression::kTrue; + } + return ExpressionPtr{Expression::map_t{result}}; +} + auto NubRight(ExpressionPtr const& expr) -> ExpressionPtr { if (not expr->IsList()) { throw Evaluator::EvaluationError{fmt::format( @@ -938,6 +956,7 @@ auto const kBuiltInFunctions = {"escape_chars", EscapeCharsExpr}, {"keys", UnaryExpr(Keys)}, {"enumerate", UnaryExpr(Enumerate)}, + {"set", UnaryExpr(Set)}, {"values", UnaryExpr(Values)}, {"lookup", LookupExpr}, {"empty_map", EmptyMapExpr}, |