44 double (*
const *
funcs1)(
void *,
double a);
46 double (*
const *
funcs2)(
void *,
double a,
double b);
87 if (next[0] ==
'd' && next[1] ==
'B') {
91 }
else if (*next >=
'E' && *next <=
'z') {
116 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_') 121 for (i=0; prefix[i]; i++) {
122 if (prefix[i] != s[i])
return 0;
132 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
134 e_last, e_st,
e_while, e_floor, e_ceil, e_trunc,
140 double (*func0)(double);
141 double (*func1)(
void *, double);
142 double (*func2)(
void *, double, double);
175 case e_mod:
return e->
value * (d - floor(d/d2)*d2);
176 case e_max:
return e->
value * (d > d2 ? d : d2);
177 case e_min:
return e->
value * (d < d2 ? d : d2);
178 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
179 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
180 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
206 char *next = p->
s, *s0 = p->
s;
233 p->
s= strchr(p->
s,
'(');
245 if (p->
s[0] !=
')') {
262 if (p->
s[0] !=
')') {
344 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
356 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
366 int sign, sign2, ret;
368 if ((ret =
parse_dB(&e0, p, &sign)) < 0)
373 if ((ret =
parse_dB(&e2, p, &sign2)) < 0) {
385 if (e0) e0->
value *= (sign|1);
397 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
421 while (*p->
s ==
'+' || *p->
s ==
'-') {
449 while (*p->
s ==
';') {
495 const char *
const *
func2_names,
double (*
const *
funcs2)(
void *,
double,
double),
552 const char *
const *const_names,
const double *
const_values,
553 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
554 const char *
const *func2_names,
double (*
const *
funcs2)(
void *,
double,
double),
572 static const double const_values[] = {
578 static const char *
const const_names[] = {
584 int main(
int argc,
char **argv)
588 const char *
const *expr;
589 static const char *
const exprs[] = {
595 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
616 "bar + PI + E + 100f*2 + foo",
617 "13k + 12f - foo(1, 2)",
631 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
633 "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)",
635 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
659 for (expr = exprs; *expr; expr++) {
660 printf(
"Evaluating '%s'\n", *expr);
662 const_names, const_values,
665 printf(
"'%s' -> nan\n\n", *expr);
667 printf(
"'%s' -> %f\n\n", *expr, d);
671 const_names, const_values,
673 printf(
"%f == 12.7\n", d);
675 const_names, const_values,
677 printf(
"%f == 0.931322575\n", d);
679 if (argc > 1 && !strcmp(argv[1],
"-t")) {
680 for (i = 0; i < 1050; i++) {
683 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.
Macro definitions for various function/variable attributes.
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 AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
double strtod(const char *, char **)
double(* func2)(void *, double, double)
const char *const * const_names
static int parse_pow(AVExpr **e, Parser *p, int *sign)
static int parse_factor(AVExpr **e, Parser *p)
high precision timer, useful to profile code
static int parse_subexpr(AVExpr **e, Parser *p)
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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)
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
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(*const funcs1)(void *, double a)
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(* func1)(void *, double)
const char *const * func2_names
common internal and external API header
static const int8_t si_prefixes['z' - 'E'+1]
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)
double(*const funcs2)(void *, double a, double b)
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