@@ -40,6 +40,7 @@ For log = natural log uncomment the next line. */
40
40
#include <string.h>
41
41
#include <stdio.h>
42
42
#include <limits.h>
43
+ #include <assert.h>
43
44
44
45
#ifndef NAN
45
46
#define NAN (0.0/0.0)
@@ -95,6 +96,26 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
95
96
return ret ;
96
97
}
97
98
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
+ }
98
119
99
120
static void te_free_parameters (te_expr * n ) {
100
121
if (!n ) return ;
@@ -149,20 +170,24 @@ static double ncr(double n, double r) {
149
170
}
150
171
static double npr (double n , double r ) {return ncr (n , r ) * fac (r );}
151
172
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
+
152
177
static const te_variable functions [] = {
153
178
/* must be in alphabetical order */
154
179
{"abs" , {.f1 = fabs }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
155
180
{"acos" , {.f1 = acos }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
156
181
{"asin" , {.f1 = asin }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
157
182
{"atan" , {.f1 = atan }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
158
183
{"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 },
160
185
{"cos" , {.f1 = cos }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
161
186
{"cosh" , {.f1 = cosh }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
162
187
{"e" , {.f0 = e }, TE_FUNCTION0 | TE_FLAG_PURE , 0 },
163
188
{"exp" , {.f1 = exp }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
164
189
{"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 },
166
191
{"ln" , {.f1 = log }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
167
192
#ifdef TE_NAT_LOG
168
193
{"log" , {.f1 = log }, TE_FUNCTION1 | TE_FLAG_PURE , 0 },
@@ -405,7 +430,7 @@ static te_expr *power(state *s) {
405
430
if (sign == 1 ) {
406
431
ret = base (s );
407
432
} else {
408
- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
433
+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
409
434
ret -> v .f .f1 = negate ;
410
435
}
411
436
@@ -433,19 +458,19 @@ static te_expr *factor(state *s) {
433
458
434
459
if (insertion ) {
435
460
/* 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 ));
437
462
insert -> v .f .f2 = t ;
438
463
insertion -> parameters [1 ] = insert ;
439
464
insertion = insert ;
440
465
} 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 ));
442
467
ret -> v .f .f2 = t ;
443
468
insertion = ret ;
444
469
}
445
470
}
446
471
447
472
if (neg ) {
448
- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
473
+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , ret );
449
474
ret -> v .f .f1 = negate ;
450
475
}
451
476
@@ -459,7 +484,7 @@ static te_expr *factor(state *s) {
459
484
while (s -> type == TOK_INFIX && (s -> v .f .f2 == pow )) {
460
485
te_fun2 t = s -> v .f .f2 ;
461
486
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 ));
463
488
ret -> v .f .f2 = t ;
464
489
}
465
490
@@ -476,7 +501,7 @@ static te_expr *term(state *s) {
476
501
while (s -> type == TOK_INFIX && (s -> v .f .f2 == mul || s -> v .f .f2 == divide || s -> v .f .f2 == fmod )) {
477
502
te_fun2 t = s -> v .f .f2 ;
478
503
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 ));
480
505
ret -> v .f .f2 = t ;
481
506
}
482
507
@@ -491,7 +516,7 @@ static te_expr *expr(state *s) {
491
516
while (s -> type == TOK_INFIX && (s -> v .f .f2 == add || s -> v .f .f2 == sub )) {
492
517
te_fun2 t = s -> v .f .f2 ;
493
518
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 ));
495
520
ret -> v .f .f2 = t ;
496
521
}
497
522
@@ -505,7 +530,7 @@ static te_expr *list(state *s) {
505
530
506
531
while (s -> type == TOK_SEP ) {
507
532
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 ));
509
534
ret -> v .f .f2 = comma ;
510
535
}
511
536
0 commit comments