![]() |
QPMS
Electromagnetic multiple scattering library and toolkit.
|
Implementation of the small expression evaluator (see specexpr.h). More...
#include "specexpr.h"#include <stdlib.h>#include <string.h>#include <stdio.h>#include <stdarg.h>#include <ctype.h>#include <math.h>#include <complex.h>
Data Structures | |
| struct | qpms_specexpr_env_t |
| struct | specexpr_ctx_t |
| struct | specexpr_fn_entry_t |
| struct | specexpr_const_entry_t |
Macros | |
| #define | SPECEXPR_MAXDEPTH 512 |
| Recursion guard; every parenthesis/function/unary nesting consumes one level. | |
| #define | SPECEXPR_MAXIDENT 64 |
| #define | SPECEXPR_FN1(cname, impl) |
| #define | SPECEXPR_MAXARGS 8 |
| Maximum number of call arguments (dynamic functions included). | |
Typedefs | |
| typedef _Complex double(* | specexpr_fnN_t) (const _Complex double *args, size_t nargs) |
Enumerations | |
| enum | specexpr_tok_t { TOK_END = 0 , TOK_NUM , TOK_IDENT , TOK_PLUS , TOK_MINUS , TOK_STAR , TOK_SLASH , TOK_CARET , TOK_POW , TOK_LPAREN , TOK_RPAREN , TOK_COMMA } |
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_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. | |
| static void | specexpr_fail (specexpr_ctx_t *c, const char *fmt,...) |
| static specexpr_tok_t | specexpr_lex (specexpr_ctx_t *c) |
| static specexpr_tok_t | specexpr_next (specexpr_ctx_t *c) |
| static specexpr_tok_t | specexpr_peek (specexpr_ctx_t *c) |
| int | qpms_specexpr_is_constant (const char *name) |
Whether name is one of the built-in constants (qpms_specexpr_eval()). | |
| static _Complex double | parse_add (specexpr_ctx_t *c) |
| static _Complex double | parse_unary (specexpr_ctx_t *c) |
| static int | specexpr_parse_args (specexpr_ctx_t *c, size_t lparen_pos, _Complex double *args) |
| Parses a parenthesised, comma-separated argument list. | |
| static _Complex double | parse_primary (specexpr_ctx_t *c, specexpr_tok_t tok) |
| static _Complex double | parse_power (specexpr_ctx_t *c, specexpr_tok_t tok) |
| Parses a primary followed, optionally, by the power operator. | |
| static _Complex double | parse_mul (specexpr_ctx_t *c) |
| 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. | |
Variables | |
| static const specexpr_fn_entry_t | specexpr_functions [] |
| static const specexpr_const_entry_t | specexpr_constants [] |
Implementation of the small expression evaluator (see specexpr.h).
| #define SPECEXPR_FN1 | ( | cname, | |
| impl | |||
| ) |
| 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. |
|
static |
Parses a parenthesised, comma-separated argument list.
lparen_pos is the lexer position of the '(' (for error messages). Consumes the closing ')'. Returns the argument count (0 for an empty list) or -1 on a syntax error (already reported).
|
static |
|
static |