sig_expr.h

include/sig/value/sig_expr.h

sig_expr

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_expr_compile

sig_status_t sig_expr_compile(const char * src, sig_expr ** out, char * err, size_t err_cap)
ParameterType
srcconst char *
outsig_expr **
errchar *
err_capsize_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).

sig_expr_destroy

void sig_expr_destroy(sig_expr * expr)
ParameterType
exprsig_expr *

shared with sig_expr_compile

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_expr_eval_bool

sig_status_t sig_expr_eval_bool(const sig_expr * expr, const sig_value * input, bool * result)
ParameterType
exprconst sig_expr *
inputconst sig_value *
resultbool *

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.