![]() |
QPMS
Electromagnetic multiple scattering library and toolkit.
|
Small arithmetic expression evaluator for the scatspec YAML files. More...
#include <stddef.h>#include <complex.h>

Go to the source code of this file.
Data Structures | |
| struct | qpms_specexpr_dyn_t |
| Dynamic resolution callbacks of an environment (all optional). More... | |
Typedefs | |
| typedef struct qpms_specexpr_env_t | qpms_specexpr_env_t |
| Variable binding environment for qpms_specexpr_eval(). | |
Functions | |
| qpms_specexpr_env_t * | qpms_specexpr_env_create (void) |
| Creates a new, empty variable environment. Returns NULL on memory error. | |
| qpms_specexpr_env_t * | qpms_specexpr_env_copy (const qpms_specexpr_env_t *src) |
| void | qpms_specexpr_env_free (qpms_specexpr_env_t *env) |
| Frees a variable environment. NULL is allowed. | |
| int | qpms_specexpr_env_set (qpms_specexpr_env_t *env, const char *name, _Complex double value) |
| Sets (adds or overrides) a variable binding; name is copied. | |
| int | qpms_specexpr_is_constant (const char *name) |
Whether name is one of the built-in constants (qpms_specexpr_eval()). | |
| int | qpms_specexpr_env_set_dyn (qpms_specexpr_env_t *env, const qpms_specexpr_dyn_t *dyn) |
| Installs (or clears, with NULL) the dynamic resolution callbacks. | |
| int | qpms_specexpr_env_get (const qpms_specexpr_env_t *env, const char *name, _Complex double *out) |
| Looks up a variable binding. Returns 1 and stores the value in out if found. | |
| int | qpms_specexpr_eval (const qpms_specexpr_env_t *env, const char *expr, _Complex double *out, char *errbuf, size_t errlen) |
| Evaluates an arithmetic expression over _Complex double. | |
Small arithmetic expression evaluator for the scatspec YAML files.
The scatspec format (see scatspec.h and scatspec.md) allows most numeric fields to be given either as plain numbers (interpreted in SI units) or as expression strings like "0.4 * pitch" or "2.11*eV/hbar", where identifiers resolve to variables defined in the vars section of the spec file (or overrides set at load time) and to a small set of built-in constants and functions.
The syntax is a deliberately small subset of Python-like arithmetic:
375, 0.4, 1.5e-9);+ - * /, unary + -, parentheses;** (also ^), right-associative, binding tighter than unary minus (i.e. -2**2 is -4);All arithmetic is done over _Complex double. There is no implicit multiplication; juxtaposition like "375nm" is a syntax error.
Built-in functions: exp, log (alias ln), sqrt, sin, cos, tan, abs (modulus), real, imag, conj.
Built-in constants (SI, matching qpms/constants.py): pi, I (imaginary unit), c, hbar, h, e, eV, nm, um, mm, SU (scuff frequency unit), and the conversion factors eV2SU, SU2eV.
| qpms_specexpr_env_t * qpms_specexpr_env_copy | ( | const qpms_specexpr_env_t * | src | ) |
Deep-copies an environment (variables and callbacks; the callback userdata pointers are borrowed). Returns NULL on memory error.
| int qpms_specexpr_env_set | ( | qpms_specexpr_env_t * | env, |
| const char * | name, | ||
| _Complex double | value | ||
| ) |
Sets (adds or overrides) a variable binding; name is copied.
Returns 0 on success, -1 on memory error or invalid (empty) name, and -2 when name is a built-in constant: the constants are resolved before the variables, so such a variable could never be referenced (every expression would evaluate the constant) and is refused.
| int qpms_specexpr_env_set_dyn | ( | qpms_specexpr_env_t * | env, |
| const qpms_specexpr_dyn_t * | dyn | ||
| ) |
Installs (or clears, with NULL) the dynamic resolution callbacks.
The callbacks are consulted for identifiers that are not built-in functions/constants and not user variables. The dyn structure and its userdata are borrowed; they must stay valid as long as the environment is used for evaluation. Returns 0 on success, -1 if clearing a NULL environment.
| int qpms_specexpr_eval | ( | const qpms_specexpr_env_t * | env, |
| const char * | expr, | ||
| _Complex double * | out, | ||
| char * | errbuf, | ||
| size_t | errlen | ||
| ) |
Evaluates an arithmetic expression over _Complex double.
| env | Variable environment (may be NULL if the expression uses no variables). |
| expr | NUL-terminated expression string. |
| out | Result storage. |
| errbuf | Buffer for the error message (may be NULL). |
| errlen | Size of errbuf. |