@@ -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 ;
@@ -405,7 +426,7 @@ static te_expr *power(state *s) {
405
426
if (sign == 1 ) {
406
427
ret = base (s );
407
428
} else {
408
- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
429
+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
409
430
ret -> v .f .f1 = negate ;
410
431
}
411
432
@@ -433,19 +454,19 @@ static te_expr *factor(state *s) {
433
454
434
455
if (insertion ) {
435
456
/* Make exponentiation go right-to-left. */
436
- te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
457
+ te_expr * insert = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
437
458
insert -> v .f .f2 = t ;
438
459
insertion -> parameters [1 ] = insert ;
439
460
insertion = insert ;
440
461
} else {
441
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
462
+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
442
463
ret -> v .f .f2 = t ;
443
464
insertion = ret ;
444
465
}
445
466
}
446
467
447
468
if (neg ) {
448
- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
469
+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , ret );
449
470
ret -> v .f .f1 = negate ;
450
471
}
451
472
@@ -459,7 +480,7 @@ static te_expr *factor(state *s) {
459
480
while (s -> type == TOK_INFIX && (s -> v .f .f2 == pow )) {
460
481
te_fun2 t = s -> v .f .f2 ;
461
482
next_token (s );
462
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
483
+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
463
484
ret -> v .f .f2 = t ;
464
485
}
465
486
@@ -476,7 +497,7 @@ static te_expr *term(state *s) {
476
497
while (s -> type == TOK_INFIX && (s -> v .f .f2 == mul || s -> v .f .f2 == divide || s -> v .f .f2 == fmod )) {
477
498
te_fun2 t = s -> v .f .f2 ;
478
499
next_token (s );
479
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
500
+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
480
501
ret -> v .f .f2 = t ;
481
502
}
482
503
@@ -491,7 +512,7 @@ static te_expr *expr(state *s) {
491
512
while (s -> type == TOK_INFIX && (s -> v .f .f2 == add || s -> v .f .f2 == sub )) {
492
513
te_fun2 t = s -> v .f .f2 ;
493
514
next_token (s );
494
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
515
+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
495
516
ret -> v .f .f2 = t ;
496
517
}
497
518
@@ -505,7 +526,7 @@ static te_expr *list(state *s) {
505
526
506
527
while (s -> type == TOK_SEP ) {
507
528
next_token (s );
508
- ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
529
+ ret = new_expr2 (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
509
530
ret -> v .f .f2 = comma ;
510
531
}
511
532
0 commit comments