39 double (*
const *
funcs1)(
void *,
double a);
41 double (*
const *
funcs2)(
void *,
double a,
double b);
79 d = strtod(numstr, &next);
82 if (*next >=
'E' && *next <=
'z') {
107 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
109 static int strmatch(
const char *s,
const char *prefix)
112 for (i=0; prefix[i]; i++) {
113 if (prefix[i] != s[i])
return 0;
133 double (*
func2)(
void *, double, double);
165 case e_mod:
return e->
value * (d - floor(d/d2)*d2);
166 case e_max:
return e->
value * (d > d2 ? d : d2);
167 case e_min:
return e->
value * (d < d2 ? d : d2);
168 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
169 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
170 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
196 char *next = p->
s, *
s0 = p->
s;
223 p->
s= strchr(p->
s,
'(');
235 if (p->
s[0] !=
')') {
252 if (p->
s[0] !=
')') {
333 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
340 int sign, sign2, ret;
342 if ((ret =
parse_pow(&e0, p, &sign)) < 0)
347 if ((ret =
parse_pow(&e2, p, &sign2)) < 0) {
359 if (e0) e0->
value *= (sign|1);
371 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
395 while (*p->
s ==
'+' || *p->
s ==
'-') {
423 while (*p->
s ==
';') {
467 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
468 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
469 int log_offset,
void *log_ctx)
482 if (!isspace(*s++)) *wp++ = s[-1];
525 const char *
const *const_names,
const double *
const_values,
526 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
527 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
528 void *opaque,
int log_offset,
void *log_ctx)
531 int ret =
av_expr_parse(&e, s, const_names, func1_names,
funcs1, func2_names, funcs2, log_offset, log_ctx);
539 return isnan(*d) ?
AVERROR(EINVAL) : 0;
546 static double const_values[] = {
552 static const char *const_names[] = {
558 int main(
int argc,
char **argv)
562 const char **expr, *exprs[] = {
568 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
589 "bar + PI + E + 100f*2 + foo",
590 "13k + 12f - foo(1, 2)",
596 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
598 "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)",
600 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
618 for (expr = exprs; *expr; expr++) {
619 printf(
"Evaluating '%s'\n", *expr);
621 const_names, const_values,
623 printf(
"'%s' -> %f\n\n", *expr, d);
627 const_names, const_values,
629 printf(
"%f == 12.7\n", d);
631 const_names, const_values,
633 printf(
"%f == 0.931322575\n", d);
635 if (argc > 1 && !strcmp(argv[1],
"-t")) {
636 for (i = 0; i < 1050; i++) {
639 const_names, const_values,
static int verify_expr(AVExpr *e)
const char *const * func1_names
void * av_mallocz(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_expr(AVExpr **e, Parser *p)
static double(*const funcs1[])(void *, double)
static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
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().
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Describe the class of an AVClass context structure.
double(*const funcs1)(void *, double a)
const char *const * func2_names
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.
simple arithmetic expression evaluator