include/sig/value/sig_expr.h
typedef struct sig_expr sig_expr;
A small condition language evaluated against a sig_value. input % 2 == 0 input.reading.temp > 21.5 && input.status != "off" input.subvalue ~ "failure" input.items[0] >= 10 The one variable is `input`, the value being tested. Literals are integers, decimals, "double" or 'single' quoted strings (escapes \\ \" \' \n \t), and true / false. Operators, loosest binding first: || && == != < <= > >= ~ (a ~ b: string a contains string b) + - * / % ! - (unary) .key [n] ["key"] (postfix: map member, list element, map member) Integers and decimals mix freely (the operation is done in decimals if either side is one); integer arithmetic wraps; % is integers only. Strings compare by bytes. An expression that cannot be evaluated against a particular input -- a missing key, an index out of range, operands of the wrong type, division by zero -- has no value. "No value" spreads through an operator, except that a false operand of && (or a true operand of ||) decides the result whatever the other side is. sig_expr_eval_bool() reports it as SIG_ERR_TYPE_MISMATCH so a caller can tell "false" from "could not be decided". A compiled sig_expr is immutable; evaluating it allocates nothing and is safe from several threads at once.
sig_status_t sig_expr_compile(const char * src, sig_expr ** out, char * err, size_t err_cap)
| Parameter | Type |
|---|---|
src | const char * |
out | sig_expr ** |
err | char * |
err_cap | size_t |
Compiles src. On failure returns SIG_ERR_PARSE (or SIG_ERR_ALLOC / SIG_ERR_INVALID_ARG) and, when err is non-NULL, copies a message such as "col 7: expected ')'" (truncated) into err[0..err_cap).
void sig_expr_destroy(sig_expr * expr)
| Parameter | Type |
|---|---|
expr | sig_expr * |
Compiles src. On failure returns SIG_ERR_PARSE (or SIG_ERR_ALLOC / SIG_ERR_INVALID_ARG) and, when err is non-NULL, copies a message such as "col 7: expected ')'" (truncated) into err[0..err_cap).
sig_status_t sig_expr_eval_bool(const sig_expr * expr, const sig_value * input, bool * result)
| Parameter | Type |
|---|---|
expr | const sig_expr * |
input | const sig_value * |
result | bool * |
SIG_OK with *result set, or SIG_ERR_TYPE_MISMATCH when the expression has no boolean value for this input (including a final value that is not a boolean). *result is false in that case.