@@ -40,6 +40,7 @@ For log = natural log uncomment the next line. */
4040#include <string.h>
4141#include <stdio.h>
4242#include <limits.h>
43+ #include <assert.h>
4344
4445#ifndef NAN
4546#define NAN (0.0/0.0)
@@ -95,6 +96,26 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
9596 return ret ;
9697}
9798
99+ static te_expr * new_expr1 (const int type , te_expr * p1 ) {
100+ const size_t size = sizeof (te_expr ) + (IS_CLOSURE (type ) ? sizeof (void * ) : 0 );
101+ assert (p1 && ARITY (type ) == 1 );
102+ te_expr * ret = malloc (size );
103+ ret -> type = type ;
104+ ret -> v .bound = 0 ;
105+ ret -> parameters [0 ] = p1 ;
106+ return ret ;
107+ }
108+
109+ static te_expr * new_expr2 (const int type , te_expr * p1 , te_expr * p2 ) {
110+ const size_t size = sizeof (te_expr ) + sizeof (void * ) + (IS_CLOSURE (type ) ? sizeof (void * ) : 0 );
111+ assert (p1 && p2 && ARITY (type ) == 2 );
112+ te_expr * ret = malloc (size );
113+ ret -> type = type ;
114+ ret -> v .bound = 0 ;
115+ ret -> parameters [0 ] = p1 ;
116+ ret -> parameters [1 ] = p2 ;
117+ return ret ;
118+ }
98119
99120static void te_free_parameters (te_expr * n ) {
100121 if (!n ) return ;
@@ -149,20 +170,24 @@ static double ncr(double n, double r) {
149170}
150171static double npr (double n , double r ) {return ncr (n , r ) * fac (r );}
151172
173+ /* Workaround for a VC 2017 problem */
174+ static double ceil_ (double x ) { return ceil (x ); }
175+ static double floor_ (double x ) { return floor (x ); }
176+
152177static const te_variable functions [] = {
153178 /* must be in alphabetical order */
154179 {"abs" , {.f1 = fabs }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
155180 {"acos" , {.f1 = acos }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
156181 {"asin" , {.f1 = asin }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
157182 {"atan" , {.f1 = atan }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
158183 {"atan2" , {.f2 = atan2 }, TE_FUNCTION2 | TE_FLAG_PURE , 0 },
159- {"ceil" , {.f1 = ceil }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
184+ {"ceil" , {.f1 = ceil_ }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
160185 {"cos" , {.f1 = cos }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
161186 {"cosh" , {.f1 = cosh }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
162187 {"e" , {.f0 = e }, TE_FUNCTION0 | TE_FLAG_PURE , 0 },
163188 {"exp" , {.f1 = exp }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
164189 {"fac" , {.f1 = fac }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
165- {"floor" , {.f1 = floor }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
190+ {"floor" , {.f1 = floor_ }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
166191 {"ln" , {.f1 = log }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
167192#ifdef TE_NAT_LOG
168193 {"log" , {.f1 = log }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
@@ -405,7 +430,7 @@ static te_expr *power(state *s) {
405430 if (sign == 1 ) {
406431 ret = base (s );
407432 } else {
408- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
433+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
409434 ret -> v .f .f1 = negate ;
410435 }
411436
@@ -433,19 +458,19 @@ static te_expr *factor(state *s) {
433458
434459 if (insertion ) {
435460 /* Make exponentiation go right-to-left. */
436- te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
461+ te_expr * insert = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
437462 insert -> v .f .f2 = t ;
438463 insertion -> parameters [1 ] = insert ;
439464 insertion = insert ;
440465 } else {
441- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
466+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
442467 ret -> v .f .f2 = t ;
443468 insertion = ret ;
444469 }
445470 }
446471
447472 if (neg ) {
448- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
473+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , ret );
449474 ret -> v .f .f1 = negate ;
450475 }
451476
@@ -459,7 +484,7 @@ static te_expr *factor(state *s) {
459484 while (s -> type == TOK_INFIX && (s -> v .f .f2 == pow )) {
460485 te_fun2 t = s -> v .f .f2 ;
461486 next_token (s );
462- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
487+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
463488 ret -> v .f .f2 = t ;
464489 }
465490
@@ -476,7 +501,7 @@ static te_expr *term(state *s) {
476501 while (s -> type == TOK_INFIX && (s -> v .f .f2 == mul || s -> v .f .f2 == divide || s -> v .f .f2 == fmod )) {
477502 te_fun2 t = s -> v .f .f2 ;
478503 next_token (s );
479- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
504+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
480505 ret -> v .f .f2 = t ;
481506 }
482507
@@ -491,7 +516,7 @@ static te_expr *expr(state *s) {
491516 while (s -> type == TOK_INFIX && (s -> v .f .f2 == add || s -> v .f .f2 == sub )) {
492517 te_fun2 t = s -> v .f .f2 ;
493518 next_token (s );
494- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
519+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
495520 ret -> v .f .f2 = t ;
496521 }
497522
@@ -505,7 +530,7 @@ static te_expr *list(state *s) {
505530
506531 while (s -> type == TOK_SEP ) {
507532 next_token (s );
508- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
533+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
509534 ret -> v .f .f2 = comma ;
510535 }
511536
0 commit comments