@@ -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 ;
@@ -405,7 +426,7 @@ static te_expr *power(state *s) {
405426 if (sign == 1 ) {
406427 ret = base (s );
407428 } else {
408- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
429+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
409430 ret -> v .f .f1 = negate ;
410431 }
411432
@@ -433,19 +454,19 @@ static te_expr *factor(state *s) {
433454
434455 if (insertion ) {
435456 /* 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 ));
437458 insert -> v .f .f2 = t ;
438459 insertion -> parameters [1 ] = insert ;
439460 insertion = insert ;
440461 } 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 ));
442463 ret -> v .f .f2 = t ;
443464 insertion = ret ;
444465 }
445466 }
446467
447468 if (neg ) {
448- ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
469+ ret = new_expr1 (TE_FUNCTION1 | TE_FLAG_PURE , ret );
449470 ret -> v .f .f1 = negate ;
450471 }
451472
@@ -459,7 +480,7 @@ static te_expr *factor(state *s) {
459480 while (s -> type == TOK_INFIX && (s -> v .f .f2 == pow )) {
460481 te_fun2 t = s -> v .f .f2 ;
461482 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 ));
463484 ret -> v .f .f2 = t ;
464485 }
465486
@@ -476,7 +497,7 @@ static te_expr *term(state *s) {
476497 while (s -> type == TOK_INFIX && (s -> v .f .f2 == mul || s -> v .f .f2 == divide || s -> v .f .f2 == fmod )) {
477498 te_fun2 t = s -> v .f .f2 ;
478499 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 ));
480501 ret -> v .f .f2 = t ;
481502 }
482503
@@ -491,7 +512,7 @@ static te_expr *expr(state *s) {
491512 while (s -> type == TOK_INFIX && (s -> v .f .f2 == add || s -> v .f .f2 == sub )) {
492513 te_fun2 t = s -> v .f .f2 ;
493514 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 ));
495516 ret -> v .f .f2 = t ;
496517 }
497518
@@ -505,7 +526,7 @@ static te_expr *list(state *s) {
505526
506527 while (s -> type == TOK_SEP ) {
507528 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 ));
509530 ret -> v .f .f2 = comma ;
510531 }
511532
0 commit comments