diff options
author | Ambroz Bizjak <ambrop7@gmail.com> | 2014-12-10 01:16:14 +0300 |
---|---|---|
committer | Ambroz Bizjak <ambrop7@gmail.com> | 2014-12-10 01:48:08 +0300 |
commit | d220d45708440cb570c6e5660a5f24c32fa7dba4 (patch) | |
tree | e3589874563760ad586cc25e624cf0912b266409 | |
parent | 86f709fa7be75514ab8383ca4de8b56b010f2f72 (diff) |
ncd: Implement ifel function to support future lazy If.
-rw-r--r-- | ncd/modules/basic_functions.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/ncd/modules/basic_functions.c b/ncd/modules/basic_functions.c index 5af3396..fd31ae3 100644 --- a/ncd/modules/basic_functions.c +++ b/ncd/modules/basic_functions.c @@ -75,6 +75,40 @@ static void if_eval (NCDCall call) NCDCall_SetResult(&call, NCDCall_EvalArg(&call, eval_arg, NCDCall_ResMem(&call))); } +static void ifel_eval (NCDCall call) +{ + size_t count = NCDCall_ArgCount(&call); + if (count % 2 == 0) { + return FunctionLog(&call, BLOG_ERROR, "ifel: need an odd number of arguments"); + } + NCDValRef value; + size_t j = 0; + while (1) { + NCDValRef arg = NCDCall_EvalArg(&call, j, NCDCall_ResMem(&call)); + if (NCDVal_IsInvalid(arg)) { + return; + } + if (j == count - 1) { + value = arg; + break; + } + NCDValRef arg2 = NCDCall_EvalArg(&call, j + 1, NCDCall_ResMem(&call)); + if (NCDVal_IsInvalid(arg2)) { + return; + } + int arg_val; + if (!ncd_read_boolean(arg, &arg_val)) { + return FunctionLog(&call, BLOG_ERROR, "ifel: bad condition"); + } + if (arg_val) { + value = arg2; + break; + } + j += 2; + } + NCDCall_SetResult(&call, value); +} + static void bool_not_eval (NCDCall call, int negate, char const *name) { if (NCDCall_ArgCount(&call) != 1) { @@ -438,6 +472,9 @@ static struct NCDModuleFunction const functions[] = { .func_name = "if", .func_eval = if_eval }, { + .func_name = "ifel", + .func_eval = ifel_eval + }, { .func_name = "bool", .func_eval = bool_eval }, { |