diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-23 12:35:39 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-24 12:30:09 +0200 |
commit | eb085c33aba12a332c3ee8e73d26a241a6996c41 (patch) | |
tree | f0bf5cffd3fd04c64d26c5c4d821deb0bde59114 | |
parent | 14b93071eca21c69b635bd77eb68057b2906ddab (diff) | |
download | justbuild-eb085c33aba12a332c3ee8e73d26a241a6996c41.tar.gz |
expressions: for "if" expressions, make both branches optional
... using, also for the "then" branch, the empty list as default.
In this way, this statement not only more symmetric, but also
allows shorter representations of some typical expressions.
-rw-r--r-- | doc/concepts/expressions.md | 12 | ||||
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 2 | ||||
-rw-r--r-- | test/buildtool/build_engine/expression/expression.test.cpp | 13 |
3 files changed, 20 insertions, 7 deletions
diff --git a/doc/concepts/expressions.md b/doc/concepts/expressions.md index a14cd60f..b3dbd82f 100644 --- a/doc/concepts/expressions.md +++ b/doc/concepts/expressions.md @@ -107,12 +107,12 @@ expression power. It is equivalent but lot shorter to multiple ###### Binary conditional: `"if"` -First the key `"cond"` is evaluated. If it evaluates to a -value that is logically true, then the key `"then"` is -evaluated and its value is the result of the evaluation. -Otherwise, the key `"else"` (if present, otherwise `[]` is -taken as default) is evaluated and the obtained value is the -result of the evaluation. +First the key `"cond"` is evaluated. If it evaluates to a value that +is logically true, then the key `"then"` (if present, otherwise `[]` +is taken as default) is evaluated and its value is the result of +the evaluation. Otherwise, the key `"else"` (if present, otherwise +`[]` is taken as default) is evaluated and the obtained value is +the result of the evaluation. ###### Sequential conditional: `"cond"` diff --git a/src/buildtool/build_engine/expression/evaluator.cpp b/src/buildtool/build_engine/expression/evaluator.cpp index 11299bb5..d53259ba 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -495,7 +495,7 @@ auto IfExpr(SubExprEvaluator&& eval, ExpressionPtr const& expr, Configuration const& env) -> ExpressionPtr { if (ValueIsTrue(EvalArgument(expr, "cond", eval, env))) { - return EvalArgument(expr, "then", eval, env); + return eval(expr->Get("then", list_t{}), env); } return eval(expr->Get("else", list_t{}), env); } diff --git a/test/buildtool/build_engine/expression/expression.test.cpp b/test/buildtool/build_engine/expression/expression.test.cpp index 83550ebd..1d7bde3e 100644 --- a/test/buildtool/build_engine/expression/expression.test.cpp +++ b/test/buildtool/build_engine/expression/expression.test.cpp @@ -479,6 +479,19 @@ TEST_CASE("Expression Evaluation", "[expression]") { // NOLINT CHECK(failure == Expression::FromJson(R"("failure")"_json)); } } + + SECTION("if defaults") { + auto expr = Expression::FromJson(R"( + { "type": "if" + , "cond": {"type": "var", "name": "x"} + } + )"_json); + CHECK(expr.Evaluate(env.Update("x", true), fcts) == + Expression::FromJson(R"([])"_json)); + CHECK(expr.Evaluate(env.Update("x", false), fcts) == + Expression::FromJson(R"([])"_json)); + } + SECTION("cond expression") { auto expr = Expression::FromJson(R"( { "type": "cond" |