41 double (*
const *
funcs1)(
void *,
double a);
43 double (*
const *
funcs2)(
void *,
double a,
double b);
84 if (next[0] ==
'd' && next[1] ==
'B') {
88 }
else if (*next >=
'E' && *next <=
'z') {
113 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
115 static int strmatch(
const char *s,
const char *prefix)
118 for (i=0; prefix[i]; i++) {
119 if (prefix[i] != s[i])
return 0;
139 double (*
func2)(
void *, double, double);
172 case e_mod:
return e->
value * (d - floor(d/d2)*d2);
173 case e_max:
return e->
value * (d > d2 ? d : d2);
174 case e_min:
return e->
value * (d < d2 ? d : d2);
175 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
176 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
177 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
203 char *next = p->
s, *
s0 = p->
s;
230 p->
s= strchr(p->
s,
'(');
242 if (p->
s[0] !=
')') {
259 if (p->
s[0] !=
')') {
341 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
353 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
363 int sign, sign2, ret;
365 if ((ret =
parse_dB(&e0, p, &sign)) < 0)
370 if ((ret =
parse_dB(&e2, p, &sign2)) < 0) {
382 if (e0) e0->
value *= (sign|1);
394 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
418 while (*p->
s ==
'+' || *p->
s ==
'-') {
446 while (*p->
s ==
';') {
491 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
492 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
493 int log_offset,
void *log_ctx)
506 if (!isspace(*s++)) *wp++ = s[-1];
549 const char *
const *const_names,
const double *
const_values,
550 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
551 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
552 void *opaque,
int log_offset,
void *log_ctx)
555 int ret =
av_expr_parse(&e, s, const_names, func1_names,
funcs1, func2_names, funcs2, log_offset, log_ctx);
569 static const double const_values[] = {
575 static const char *
const const_names[] = {
581 int main(
int argc,
char **argv)
585 const char *
const *expr;
586 static const char *
const exprs[] = {
592 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
613 "bar + PI + E + 100f*2 + foo",
614 "13k + 12f - foo(1, 2)",
628 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
630 "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
632 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
656 for (expr = exprs; *expr; expr++) {
657 printf(
"Evaluating '%s'\n", *expr);
659 const_names, const_values,
662 printf(
"'%s' -> nan\n\n", *expr);
664 printf(
"'%s' -> %f\n\n", *expr, d);
668 const_names, const_values,
670 printf(
"%f == 12.7\n", d);
672 const_names, const_values,
674 printf(
"%f == 0.931322575\n", d);
676 if (argc > 1 && !strcmp(argv[1],
"-t")) {
677 for (i = 0; i < 1050; i++) {
680 const_names, const_values,
static int verify_expr(AVExpr *e)
const char *const * func1_names
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int parse_dB(AVExpr **e, Parser *p, int *sign)
static av_always_inline av_const int isnan(float x)
static int parse_expr(AVExpr **e, Parser *p)
static double(*const funcs1[])(void *, double)
static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
double strtod(const char *, char **)
const char *const * const_names
static const double const_values[]
double(* func1)(void *, double)
static int parse_pow(AVExpr **e, Parser *p, int *sign)
static int parse_factor(AVExpr **e, Parser *p)
static int parse_subexpr(AVExpr **e, Parser *p)
double(* func2)(void *, double, double)
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
const double * const_values
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define IS_IDENTIFIER_CHAR(c)
static int parse_term(AVExpr **e, Parser *p)
void av_log(void *avcl, int level, const char *fmt,...)
int main(int argc, char **argv)
static const char *const const_names[]
static const int8_t si_prefixes['z'- 'E'+1]
static av_always_inline av_const double trunc(double x)
static int strmatch(const char *s, const char *prefix)
static int parse_primary(AVExpr **e, Parser *p)
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
static double eval_expr(Parser *p, AVExpr *e)
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Describe the class of an AVClass context structure.
double(*const funcs1)(void *, double a)
const char *const * func2_names
common internal and external API header
double(*const funcs2)(void *, double a, double b)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
static av_always_inline av_const int isinf(float x)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
simple arithmetic expression evaluator