QPMS
Electromagnetic multiple scattering library and toolkit.
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
specexpr.c File Reference

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>
Include dependency graph for specexpr.c:

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 []
 

Detailed Description

Implementation of the small expression evaluator (see specexpr.h).

Macro Definition Documentation

◆ SPECEXPR_FN1

#define SPECEXPR_FN1 (   cname,
  impl 
)
Value:
static _Complex double specexpr_f_##cname(const _Complex double *a, \
size_t na) { (void)na; return impl(a[0]); }

Function Documentation

◆ qpms_specexpr_env_copy()

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.

◆ qpms_specexpr_env_set()

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.

◆ qpms_specexpr_env_set_dyn()

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.

◆ qpms_specexpr_eval()

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.

Parameters
envVariable environment (may be NULL if the expression uses no variables).
exprNUL-terminated expression string.
outResult storage.
errbufBuffer for the error message (may be NULL).
errlenSize of errbuf.
Returns
0 on success; -1 on error, with a human readable message (including the offending position) written to errbuf.

◆ specexpr_parse_args()

static int specexpr_parse_args ( specexpr_ctx_t *  c,
size_t  lparen_pos,
_Complex double *  args 
)
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).

Variable Documentation

◆ specexpr_constants

const specexpr_const_entry_t specexpr_constants[]
static
Initial value:
= {
{ "pi", M_PI },
{ "I", 1.0i },
{ "c", 299792458. },
{ "hbar", 1.054571800e-34 },
{ "h", 2 * M_PI * 1.054571800e-34 },
{ "e", 1.602176487e-19 },
{ "eV", 1.602176487e-19 },
{ "nm", 1e-9 },
{ "um", 1e-6 },
{ "mm", 1e-3 },
{ "SU", 3e14 },
{ "eV2SU", 1.602176487e-19 / 1.054571800e-34 / 3e14 },
{ "SU2eV", 3e14 / (1.602176487e-19 / 1.054571800e-34) },
{ NULL, 0 },
}

◆ specexpr_functions

const specexpr_fn_entry_t specexpr_functions[]
static
Initial value:
= {
{ "exp", 1, 1, specexpr_f_cexp },
{ "log", 1, 1, specexpr_f_clog },
{ "ln", 1, 1, specexpr_f_clog },
{ "sqrt", 1, 1, specexpr_f_csqrt },
{ "sin", 1, 1, specexpr_f_csin },
{ "cos", 1, 1, specexpr_f_ccos },
{ "tan", 1, 1, specexpr_f_ctan },
{ "abs", 1, 1, specexpr_f_cabs },
{ "real", 1, 1, specexpr_f_creal },
{ "imag", 1, 1, specexpr_f_cimag },
{ "conj", 1, 1, specexpr_f_conj },
{ NULL, 0, 0, NULL },
}