mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Refactoring.
This commit is contained in:
		
							
								
								
									
										31
									
								
								test.c
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								test.c
									
									
									
									
									
								
							| @@ -35,12 +35,7 @@ typedef struct { | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| void test_results() { | ||||
|  | ||||
|     test_case cases[] = { | ||||
|         {"1", 1}, | ||||
|         {"(1)", 1}, | ||||
| @@ -117,6 +112,7 @@ void test_results() { | ||||
|  | ||||
|         {"1,2", 2}, | ||||
|         {"1,2+1", 3}, | ||||
|         {"1+1,2+2,2+1", 3}, | ||||
|         {"1,2,3", 3}, | ||||
|         {"(1,2),3", 3}, | ||||
|         {"1,(2,3)", 3}, | ||||
| @@ -155,8 +151,6 @@ void test_results() { | ||||
|  | ||||
|  | ||||
| void test_syntax() { | ||||
|  | ||||
|  | ||||
|     test_case errors[] = { | ||||
|         {"", 1}, | ||||
|         {"1+", 2}, | ||||
| @@ -293,7 +287,7 @@ void test_variables() { | ||||
|  | ||||
|  | ||||
| #define cross_check(a, b) do {\ | ||||
|     expr = te_compile((a), &lookup, 1, &err);\ | ||||
|     expr = te_compile((a), lookup, 2, &err);\ | ||||
|     lfequal(te_eval(expr), (b));\ | ||||
|     lok(!err);\ | ||||
|     te_free(expr);\ | ||||
| @@ -301,8 +295,8 @@ void test_variables() { | ||||
|  | ||||
| void test_functions() { | ||||
|  | ||||
|     double x; | ||||
|     te_variable lookup = {"x", &x}; | ||||
|     double x, y; | ||||
|     te_variable lookup[] = {{"x", &x}, {"y", &y}}; | ||||
|  | ||||
|     int err; | ||||
|     te_expr *expr; | ||||
| @@ -324,6 +318,12 @@ void test_functions() { | ||||
|         cross_check("sqrt x", sqrt(x)); | ||||
|         cross_check("tan x", tan(x)); | ||||
|         cross_check("tanh x", tanh(x)); | ||||
|  | ||||
|         for (y = -2; y < 2; y += .2) { | ||||
|             if (fabs(x) < 0.01) break; | ||||
|             cross_check("atan2(x,y)", atan2(x, y)); | ||||
|             cross_check("pow(x,y)", pow(x, y)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -351,10 +351,10 @@ void test_dynamic() { | ||||
|     te_variable lookup[] = { | ||||
|         {"x", &x}, | ||||
|         {"f", &f}, | ||||
|         {"test0", test0, TE_FUN | 0}, | ||||
|         {"test1", test1, TE_FUN | 1}, | ||||
|         {"test2", test2, TE_FUN | 2}, | ||||
|         {"test3", test3, TE_FUN | 3}, | ||||
|         {"test0", test0, TE_FUNCTION0}, | ||||
|         {"test1", test1, TE_FUNCTION1}, | ||||
|         {"test2", test2, TE_FUNCTION2}, | ||||
|         {"test3", test3, TE_FUNCTION3}, | ||||
|     }; | ||||
|  | ||||
|     test_case cases[] = { | ||||
| @@ -366,7 +366,10 @@ void test_dynamic() { | ||||
|         {"f+test0", 11}, | ||||
|         {"test0+test0", 12}, | ||||
|         {"test0()+test0", 12}, | ||||
|         {"test0+test0()", 12}, | ||||
|         {"test0()+(0)+test0()", 12}, | ||||
|         {"test1 test0", 12}, | ||||
|         {"test1(test0)", 12}, | ||||
|         {"test1 f", 10}, | ||||
|         {"test1 x", 4}, | ||||
|         {"test2 (test0, x)", 8}, | ||||
|   | ||||
							
								
								
									
										147
									
								
								tinyexpr.c
									
									
									
									
									
								
							
							
						
						
									
										147
									
								
								tinyexpr.c
									
									
									
									
									
								
							| @@ -29,12 +29,17 @@ | ||||
| #include <stdio.h> | ||||
|  | ||||
|  | ||||
| enum {TOK_NULL, TOK_END, TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_INFIX, | ||||
|     TOK_VARIABLE, TOK_SEP, TOK_ERROR, TOK_FUNCTION0, TOK_FUNCTION1, | ||||
|     TOK_FUNCTION2, TOK_FUNCTION3, TOK_FUNCTION4, TOK_FUNCTION5, TOK_FUNCTION6, | ||||
|     TOK_FUNCTION7 | ||||
| #define TE_MASK_ARIT 0x00000007 /* Three bits, Arity, max is 8 */ | ||||
| #define TE_FLAG_TYPE 0x00000018 /* Two bits, 1 = constant, 2 = variable, 3 = function */ | ||||
|  | ||||
| enum { | ||||
|     TOK_NULL, TOK_ERROR, TOK_END, TOK_SEP, | ||||
|     TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_VARIABLE, TOK_INFIX, | ||||
|     TOK_FUNCTION0, TOK_FUNCTION1, TOK_FUNCTION2, TOK_FUNCTION3, | ||||
|     TOK_FUNCTION4, TOK_FUNCTION5, TOK_FUNCTION6, TOK_FUNCTION7 | ||||
| }; | ||||
|  | ||||
| enum { TE_CONSTANT = TE_FUNCTION7+1}; | ||||
|  | ||||
|  | ||||
| typedef struct state { | ||||
| @@ -48,26 +53,14 @@ typedef struct state { | ||||
| } state; | ||||
|  | ||||
|  | ||||
| static int te_get_type(const int type) { | ||||
|     if(type == 0) return TE_VAR; | ||||
|     return type & TE_FLAG_TYPE; | ||||
| } | ||||
|  | ||||
|  | ||||
| static int te_get_arity(const int type) { | ||||
|     return type & TE_MASK_ARIT; | ||||
| } | ||||
|  | ||||
| #define ARITY(TYPE) (((TYPE) < TE_FUNCTION0 || (TYPE) > TE_FUNCTION7) ? 0 : ((TYPE)-TE_FUNCTION0)) | ||||
|  | ||||
| static te_expr *new_expr(const int type, const te_expr *members[]) { | ||||
|     int member_count = te_get_arity(type); | ||||
|     size_t member_size = sizeof(te_expr*) * member_count; | ||||
|     te_expr *ret = malloc(sizeof(te_expr) + member_size); | ||||
|     ret->member_count = member_count; | ||||
|     if(!members) { | ||||
|         memset(ret->members, 0, member_size); | ||||
|     } else { | ||||
|         memcpy(ret->members, members, member_size); | ||||
|     const int arity = ARITY(type); | ||||
|     te_expr *ret = malloc(sizeof(te_expr) + sizeof(te_expr*) * arity); | ||||
|     if (arity && members) { | ||||
|         memcpy(ret->members, members, sizeof(te_expr*) * arity); | ||||
|     } | ||||
|     ret->type = type; | ||||
|     ret->bound = 0; | ||||
| @@ -76,9 +69,16 @@ static te_expr *new_expr(const int type, const te_expr *members[]) { | ||||
|  | ||||
|  | ||||
| void te_free(te_expr *n) { | ||||
|     int i; | ||||
|     if (!n) return; | ||||
|     for(i = n->member_count - 1; i >= 0; i--) te_free(n->members[i]); | ||||
|     switch (n->type) { | ||||
|         case TE_FUNCTION7: te_free(n->members[6]); | ||||
|         case TE_FUNCTION6: te_free(n->members[5]); | ||||
|         case TE_FUNCTION5: te_free(n->members[4]); | ||||
|         case TE_FUNCTION4: te_free(n->members[3]); | ||||
|         case TE_FUNCTION3: te_free(n->members[2]); | ||||
|         case TE_FUNCTION2: te_free(n->members[1]); | ||||
|         case TE_FUNCTION1: te_free(n->members[0]); | ||||
|     } | ||||
|     free(n); | ||||
| } | ||||
|  | ||||
| @@ -88,26 +88,26 @@ static const double e  = 2.71828182845904523536; | ||||
|  | ||||
| static const te_variable functions[] = { | ||||
|     /* must be in alphabetical order */ | ||||
|     {"abs", fabs,     TE_FUN | 1}, | ||||
|     {"acos", acos,    TE_FUN | 1}, | ||||
|     {"asin", asin,    TE_FUN | 1}, | ||||
|     {"atan", atan,    TE_FUN | 1}, | ||||
|     {"atan2", atan2,  TE_FUN | 2}, | ||||
|     {"ceil", ceil,    TE_FUN | 1}, | ||||
|     {"cos", cos,      TE_FUN | 1}, | ||||
|     {"cosh", cosh,    TE_FUN | 1}, | ||||
|     {"e", &e,         TE_VAR}, | ||||
|     {"exp", exp,      TE_FUN | 1}, | ||||
|     {"floor", floor,  TE_FUN | 1}, | ||||
|     {"ln", log,       TE_FUN | 1}, | ||||
|     {"log", log10,    TE_FUN | 1}, | ||||
|     {"pi", &pi,       TE_VAR}, | ||||
|     {"pow", pow,      TE_FUN | 2}, | ||||
|     {"sin", sin,      TE_FUN | 1}, | ||||
|     {"sinh", sinh,    TE_FUN | 1}, | ||||
|     {"sqrt", sqrt,    TE_FUN | 1}, | ||||
|     {"tan", tan,      TE_FUN | 1}, | ||||
|     {"tanh", tanh,    TE_FUN | 1}, | ||||
|     {"abs", fabs,     TE_FUNCTION1}, | ||||
|     {"acos", acos,    TE_FUNCTION1}, | ||||
|     {"asin", asin,    TE_FUNCTION1}, | ||||
|     {"atan", atan,    TE_FUNCTION1}, | ||||
|     {"atan2", atan2,  TE_FUNCTION2}, | ||||
|     {"ceil", ceil,    TE_FUNCTION1}, | ||||
|     {"cos", cos,      TE_FUNCTION1}, | ||||
|     {"cosh", cosh,    TE_FUNCTION1}, | ||||
|     {"e", &e,         TE_VARIABLE}, | ||||
|     {"exp", exp,      TE_FUNCTION1}, | ||||
|     {"floor", floor,  TE_FUNCTION1}, | ||||
|     {"ln", log,       TE_FUNCTION1}, | ||||
|     {"log", log10,    TE_FUNCTION1}, | ||||
|     {"pi", &pi,       TE_VARIABLE}, | ||||
|     {"pow", pow,      TE_FUNCTION2}, | ||||
|     {"sin", sin,      TE_FUNCTION1}, | ||||
|     {"sinh", sinh,    TE_FUNCTION1}, | ||||
|     {"sqrt", sqrt,    TE_FUNCTION1}, | ||||
|     {"tan", tan,      TE_FUNCTION1}, | ||||
|     {"tanh", tanh,    TE_FUNCTION1}, | ||||
|     {0} | ||||
| }; | ||||
|  | ||||
| @@ -132,7 +132,6 @@ static const te_variable *find_builtin(const char *name, int len) { | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static const te_variable *find_lookup(const state *s, const char *name, int len) { | ||||
|     int i; | ||||
|     if (!s->lookup) return 0; | ||||
| @@ -171,7 +170,6 @@ void next_token(state *s) { | ||||
|         } else { | ||||
|             /* Look for a variable or builtin function call. */ | ||||
|             if (s->next[0] >= 'a' && s->next[0] <= 'z') { | ||||
|                 int arity, type; | ||||
|                 const char *start; | ||||
|                 start = s->next; | ||||
|                 while ((s->next[0] >= 'a' && s->next[0] <= 'z') || (s->next[0] >= '0' && s->next[0] <= '9')) s->next++; | ||||
| @@ -182,16 +180,15 @@ void next_token(state *s) { | ||||
|                 if (!var) { | ||||
|                     s->type = TOK_ERROR; | ||||
|                 } else { | ||||
|                     type = te_get_type(var->type); | ||||
|                     arity = te_get_arity(var->type); | ||||
|                     switch(type) | ||||
|                     switch(var->type) | ||||
|                     { | ||||
|                         case TE_VAR: | ||||
|                         case TE_VARIABLE: | ||||
|                             s->type = TOK_VARIABLE; | ||||
|                             s->bound = var->value; break; | ||||
|                         case TE_FUN: | ||||
|                             s->type = TOK_FUNCTION0 + arity; | ||||
|                             s->fun.f0 = (void*)var->value; | ||||
|                             s->bound = var->address; break; | ||||
|                         case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: | ||||
|                         case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: | ||||
|                             s->type = TOK_FUNCTION0 + ARITY(var->type); | ||||
|                             s->fun.f0 = (void*)var->address; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
| @@ -227,19 +224,19 @@ static te_expr *base(state *s) { | ||||
|  | ||||
|     switch (s->type) { | ||||
|         case TOK_NUMBER: | ||||
|             ret = new_expr(TE_CONST, 0); | ||||
|             ret = new_expr(TE_CONSTANT, 0); | ||||
|             ret->value = s->value; | ||||
|             next_token(s); | ||||
|             break; | ||||
|  | ||||
|         case TOK_VARIABLE: | ||||
|             ret = new_expr(TE_VAR, 0); | ||||
|             ret = new_expr(TE_VARIABLE, 0); | ||||
|             ret->bound = s->bound; | ||||
|             next_token(s); | ||||
|             break; | ||||
|  | ||||
|         case TOK_FUNCTION0: | ||||
|             ret = new_expr(TE_FUN, 0); | ||||
|             ret = new_expr(TE_FUNCTION0, 0); | ||||
|             ret->fun.f0 = s->fun.f0; | ||||
|             next_token(s); | ||||
|             if (s->type == TOK_OPEN) { | ||||
| @@ -253,7 +250,7 @@ static te_expr *base(state *s) { | ||||
|             break; | ||||
|  | ||||
|         case TOK_FUNCTION1: | ||||
|             ret = new_expr(TE_FUN | 1, 0); | ||||
|             ret = new_expr(TE_FUNCTION1, 0); | ||||
|             ret->fun.f0 = s->fun.f0; | ||||
|  | ||||
|             next_token(s); | ||||
| @@ -264,7 +261,7 @@ static te_expr *base(state *s) { | ||||
|         case TOK_FUNCTION5: case TOK_FUNCTION6: case TOK_FUNCTION7: | ||||
|             arity = s->type - TOK_FUNCTION0; | ||||
|  | ||||
|             ret = new_expr(TE_FUN | arity, 0); | ||||
|             ret = new_expr(TE_FUNCTION0 + arity, 0); | ||||
|             ret->fun.f0 = s->fun.f0; | ||||
|             next_token(s); | ||||
|  | ||||
| @@ -322,7 +319,7 @@ static te_expr *power(state *s) { | ||||
|     if (sign == 1) { | ||||
|         ret = base(s); | ||||
|     } else { | ||||
|         ret = new_expr(TE_FUN | 1, (const te_expr*[]){base(s)}); | ||||
|         ret = new_expr(TE_FUNCTION1, (const te_expr*[]){base(s)}); | ||||
|         ret->fun.f1 = negate; | ||||
|     } | ||||
|  | ||||
| @@ -337,7 +334,7 @@ static te_expr *factor(state *s) { | ||||
|     while (s->type == TOK_INFIX && (s->fun.f2 == pow)) { | ||||
|         te_fun2 t = s->fun.f2; | ||||
|         next_token(s); | ||||
|         ret = new_expr(TE_FUN | 2, (const te_expr*[]){ret, power(s)}); | ||||
|         ret = new_expr(TE_FUNCTION2, (const te_expr*[]){ret, power(s)}); | ||||
|         ret->fun.f2 = t; | ||||
|     } | ||||
|  | ||||
| @@ -352,7 +349,7 @@ static te_expr *term(state *s) { | ||||
|     while (s->type == TOK_INFIX && (s->fun.f2 == mul || s->fun.f2 == divide || s->fun.f2 == fmod)) { | ||||
|         te_fun2 t = s->fun.f2; | ||||
|         next_token(s); | ||||
|         ret = new_expr(TE_FUN | 2, (const te_expr*[]){ret, factor(s)}); | ||||
|         ret = new_expr(TE_FUNCTION2, (const te_expr*[]){ret, factor(s)}); | ||||
|         ret->fun.f2 = t; | ||||
|     } | ||||
|  | ||||
| @@ -367,7 +364,7 @@ static te_expr *expr(state *s) { | ||||
|     while (s->type == TOK_INFIX && (s->fun.f2 == add || s->fun.f2 == sub)) { | ||||
|         te_fun2 t = s->fun.f2; | ||||
|         next_token(s); | ||||
|         ret = new_expr(TE_FUN | 2, (const te_expr*[]){ret, term(s)}); | ||||
|         ret = new_expr(TE_FUNCTION2, (const te_expr*[]){ret, term(s)}); | ||||
|         ret->fun.f2 = t; | ||||
|     } | ||||
|  | ||||
| @@ -381,7 +378,7 @@ static te_expr *list(state *s) { | ||||
|  | ||||
|     while (s->type == TOK_SEP) { | ||||
|         next_token(s); | ||||
|         ret = new_expr(TE_FUN | 2, (const te_expr*[]){ret, expr(s)}); | ||||
|         ret = new_expr(TE_FUNCTION2, (const te_expr*[]){ret, expr(s)}); | ||||
|         ret->fun.f2 = comma; | ||||
|     } | ||||
|  | ||||
| @@ -390,11 +387,14 @@ static te_expr *list(state *s) { | ||||
|  | ||||
|  | ||||
| double te_eval(const te_expr *n) { | ||||
|     switch(te_get_type(n->type)) { | ||||
|         case TE_CONST: return n->value; | ||||
|         case TE_VAR: return *n->bound; | ||||
|         case TE_FUN: | ||||
|         switch(te_get_arity(n->type)) { | ||||
|     if (!n) return 0.0/0.0; | ||||
|  | ||||
|     switch(n->type) { | ||||
|         case TE_CONSTANT: return n->value; | ||||
|         case TE_VARIABLE: return *n->bound; | ||||
|         case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: | ||||
|         case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: | ||||
|         switch(ARITY(n->type)) { | ||||
|             #define m(e) te_eval(n->members[e]) | ||||
|             case 0: return n->fun.f0(); | ||||
|             case 1: return n->fun.f1(m(0)); | ||||
| @@ -482,11 +482,12 @@ static void pn (const te_expr *n, int depth) { | ||||
|     int i, arity; | ||||
|     printf("%*s", depth, ""); | ||||
|  | ||||
|     switch(te_get_type(n->type)) { | ||||
|     case TE_CONST: printf("%f\n", n->value); break; | ||||
|     case TE_VAR: printf("bound %p\n", n->bound); break; | ||||
|     case TE_FUN: | ||||
|          arity = te_get_arity(n->type); | ||||
|     switch(n->type) { | ||||
|     case TE_CONSTANT: printf("%f\n", n->value); break; | ||||
|     case TE_VARIABLE: printf("bound %p\n", n->bound); break; | ||||
|     case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: | ||||
|     case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: | ||||
|          arity = ARITY(n->type); | ||||
|          printf("f%d", arity); | ||||
|          for(i = 0; i < arity; i++) { | ||||
|              printf(" %p", n->members[i]); | ||||
|   | ||||
							
								
								
									
										14
									
								
								tinyexpr.h
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								tinyexpr.h
									
									
									
									
									
								
							| @@ -47,20 +47,20 @@ typedef union | ||||
|  | ||||
| typedef struct te_expr { | ||||
|     int type; | ||||
|     union {double value; const double *bound; te_fun fun; }; | ||||
|     int member_count; | ||||
|     union {double value; const double *bound; te_fun fun;}; | ||||
|     struct te_expr *members[]; | ||||
| } te_expr; | ||||
|  | ||||
|  | ||||
| #define TE_MASK_ARIT 0x00000007 /* Three bits, Arity, max is 8 */ | ||||
| #define TE_FLAG_TYPE 0x00000018 /* Two bits, 1 = constant, 2 = variable, 3 = function */ | ||||
|  | ||||
| enum { TE_CONST = 1 << 3, TE_VAR = 2 << 3, TE_FUN = 3 << 3}; | ||||
| enum { | ||||
|     TE_VARIABLE = 0, | ||||
|     TE_FUNCTION0, TE_FUNCTION1, TE_FUNCTION2, TE_FUNCTION3, | ||||
|     TE_FUNCTION4, TE_FUNCTION5, TE_FUNCTION6, TE_FUNCTION7 | ||||
| }; | ||||
|  | ||||
| typedef struct te_variable { | ||||
|     const char *name; | ||||
|     const void *value; | ||||
|     const void *address; | ||||
|     int type; | ||||
| } te_variable; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user