/*
- * simple arithmetic expression evaluator
- *
* Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
* Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
*
* see http://joe.hotchkiss.com/programming/eval/eval.html
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-
-#include "libavutil/mathematics.h"
-#include "avcodec.h"
+#include "libavutil/avutil.h"
#include "eval.h"
typedef struct Parser{
char *s;
const double *const_value;
const char * const *const_name; // NULL terminated
- double (**func1)(void *, double a); // NULL terminated
- const char **func1_name; // NULL terminated
- double (**func2)(void *, double a, double b); // NULL terminated
- const char **func2_name; // NULL terminated
+ double (* const *func1)(void *, double a); // NULL terminated
+ const char * const *func1_name; // NULL terminated
+ double (* const *func2)(void *, double a, double b); // NULL terminated
+ const char * const *func2_name; // NULL terminated
void *opaque;
const char **error;
#define VARS 10
return 1;
}
-struct ff_expr_s {
+struct AVExpr {
enum {
e_value, e_const, e_func0, e_func1, e_func2,
e_squish, e_gauss, e_ld,
double (*func1)(void *, double);
double (*func2)(void *, double, double);
} a;
- AVExpr *param[2];
+ struct AVExpr *param[2];
};
static double eval_expr(Parser * p, AVExpr * e) {
static AVExpr * parse_expr(Parser *p);
-void ff_eval_free(AVExpr * e) {
+void ff_free_expr(AVExpr * e) {
if (!e) return;
- ff_eval_free(e->param[0]);
- ff_eval_free(e->param[1]);
+ ff_free_expr(e->param[0]);
+ ff_free_expr(e->param[1]);
av_freep(&e);
}
if(p->s==NULL){
*p->error = "undefined constant or missing (";
p->s= next;
- ff_eval_free(d);
+ ff_free_expr(d);
return NULL;
}
p->s++; // "("
d = parse_expr(p);
if(p->s[0] != ')'){
*p->error = "missing )";
- ff_eval_free(d);
+ ff_free_expr(d);
return NULL;
}
p->s++; // ")"
}
if(p->s[0] != ')'){
*p->error = "missing )";
- ff_eval_free(d);
+ ff_free_expr(d);
return NULL;
}
p->s++; // ")"
}
*p->error = "unknown function";
- ff_eval_free(d);
+ ff_free_expr(d);
return NULL;
}
}
}
-AVExpr * ff_parse(const char *s, const char * const *const_name,
- double (**func1)(void *, double), const char **func1_name,
- double (**func2)(void *, double, double), const char **func2_name,
+AVExpr *ff_parse_expr(const char *s, const char * const *const_name,
+ double (* const *func1)(void *, double), const char * const *func1_name,
+ double (* const *func2)(void *, double, double), const char * const *func2_name,
const char **error){
Parser p;
AVExpr *e = NULL;
e = parse_expr(&p);
if (!verify_expr(e)) {
- ff_eval_free(e);
+ ff_free_expr(e);
e = NULL;
}
end:
return e;
}
-double ff_parse_eval(AVExpr * e, const double *const_value, void *opaque) {
+double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
Parser p;
p.const_value= const_value;
return eval_expr(&p, e);
}
-double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
- double (**func1)(void *, double), const char **func1_name,
- double (**func2)(void *, double, double), const char **func2_name,
+double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name,
+ double (* const *func1)(void *, double), const char * const *func1_name,
+ double (* const *func2)(void *, double, double), const char * const *func2_name,
void *opaque, const char **error){
- AVExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
+ AVExpr * e = ff_parse_expr(s, const_name, func1, func1_name, func2, func2_name, error);
double d;
if (!e) return NAN;
- d = ff_parse_eval(e, const_value, opaque);
- ff_eval_free(e);
+ d = ff_eval_expr(e, const_value, opaque);
+ ff_free_expr(e);
return d;
}
};
int main(void){
int i;
- printf("%f == 12.7\n", ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
- printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
+ printf("%f == 12.7\n", ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
+ printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
for(i=0; i<1050; i++){
START_TIMER
- ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
- STOP_TIMER("ff_eval2")
+ ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
+ STOP_TIMER("ff_parse_and_eval_expr")
}
return 0;
}