mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Added option TE_POW_FROM_RIGHT
This commit is contained in:
		
							
								
								
									
										51
									
								
								tinyexpr.c
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								tinyexpr.c
									
									
									
									
									
								
							| @@ -22,6 +22,14 @@ | ||||
|  * 3. This notice may not be removed or altered from any source distribution. | ||||
|  */ | ||||
|  | ||||
| /* COMPILE TIME OPTIONS */ | ||||
|  | ||||
| /* Exponentiation associativity: | ||||
| For a^b^c = (a^b)^c and -a^b = (-a)^b do nothing. | ||||
| For a^b^c = a^(b^c) and -a^b = -(a^b) uncomment the next line.*/ | ||||
| /* #define TE_POW_FROM_RIGHT */ | ||||
|  | ||||
|  | ||||
| #include "tinyexpr.h" | ||||
| #include <stdlib.h> | ||||
| #include <math.h> | ||||
| @@ -357,7 +365,48 @@ static te_expr *power(state *s) { | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| #ifdef TE_POW_FROM_RIGHT | ||||
| static te_expr *factor(state *s) { | ||||
|     /* <factor>    =    <power> {"^" <power>} */ | ||||
|     te_expr *ret = power(s); | ||||
|  | ||||
|     int neg = 0; | ||||
|     te_expr *insertion = 0; | ||||
|  | ||||
|     if (ret->type == (TE_FUNCTION1 | TE_FLAG_PURE) && ret->function == negate) { | ||||
|         te_expr *se = ret->parameters[0]; | ||||
|         if (se->type != TE_CONSTANT) { | ||||
|             free(ret); | ||||
|             ret = se; | ||||
|             neg = 1; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     while (s->type == TOK_INFIX && (s->function == pow)) { | ||||
|         te_fun2 t = s->function; | ||||
|         next_token(s); | ||||
|  | ||||
|         if (insertion) { | ||||
|             /* Make exponentiation go right-to-left. */ | ||||
|             te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s)); | ||||
|             insert->function = t; | ||||
|             insertion->parameters[1] = insert; | ||||
|             insertion = insert; | ||||
|         } else { | ||||
|             ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s)); | ||||
|             ret->function = t; | ||||
|             insertion = ret; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (neg) { | ||||
|         ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret); | ||||
|         ret->function = negate; | ||||
|     } | ||||
|  | ||||
|     return ret; | ||||
| } | ||||
| #else | ||||
| static te_expr *factor(state *s) { | ||||
|     /* <factor>    =    <power> {"^" <power>} */ | ||||
|     te_expr *ret = power(s); | ||||
| @@ -371,6 +420,8 @@ static te_expr *factor(state *s) { | ||||
|  | ||||
|     return ret; | ||||
| } | ||||
| #endif | ||||
|  | ||||
|  | ||||
|  | ||||
| static te_expr *term(state *s) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user