@@ -80,15 +80,14 @@ typedef struct state {
80
80
#define IS_CLOSURE (TYPE ) (((TYPE) & TE_CLOSURE0) != 0)
81
81
#define ARITY (TYPE ) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
82
82
#define NEW_EXPR (type , ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
83
+ #define CHECK_NULL (ptr , ...) if ((ptr) == NULL) { __VA_ARGS__; return NULL; }
83
84
84
85
static te_expr * new_expr (const int type , const te_expr * parameters []) {
85
86
const int arity = ARITY (type );
86
87
const int psize = sizeof (void * ) * arity ;
87
88
const int size = (sizeof (te_expr ) - sizeof (void * )) + psize + (IS_CLOSURE (type ) ? sizeof (void * ) : 0 );
88
89
te_expr * ret = malloc (size );
89
- if (ret == NULL ) {
90
- return NULL ;
91
- }
90
+ CHECK_NULL (ret );
92
91
93
92
memset (ret , 0 , size );
94
93
if (arity && parameters ) {
@@ -309,19 +308,15 @@ static te_expr *base(state *s) {
309
308
switch (TYPE_MASK (s -> type )) {
310
309
case TOK_NUMBER :
311
310
ret = new_expr (TE_CONSTANT , 0 );
312
- if (ret == NULL ) {
313
- return NULL ;
314
- }
311
+ CHECK_NULL (ret );
315
312
316
313
ret -> value = s -> value ;
317
314
next_token (s );
318
315
break ;
319
316
320
317
case TOK_VARIABLE :
321
318
ret = new_expr (TE_VARIABLE , 0 );
322
- if (ret == NULL ) {
323
- return NULL ;
324
- }
319
+ CHECK_NULL (ret );
325
320
326
321
ret -> bound = s -> bound ;
327
322
next_token (s );
@@ -330,9 +325,7 @@ static te_expr *base(state *s) {
330
325
case TE_FUNCTION0 :
331
326
case TE_CLOSURE0 :
332
327
ret = new_expr (s -> type , 0 );
333
- if (ret == NULL ) {
334
- return NULL ;
335
- }
328
+ CHECK_NULL (ret );
336
329
337
330
ret -> function = s -> function ;
338
331
if (IS_CLOSURE (s -> type )) ret -> parameters [0 ] = s -> context ;
@@ -350,18 +343,13 @@ static te_expr *base(state *s) {
350
343
case TE_FUNCTION1 :
351
344
case TE_CLOSURE1 :
352
345
ret = new_expr (s -> type , 0 );
353
- if (ret == NULL ) {
354
- return NULL ;
355
- }
346
+ CHECK_NULL (ret );
356
347
357
348
ret -> function = s -> function ;
358
349
if (IS_CLOSURE (s -> type )) ret -> parameters [1 ] = s -> context ;
359
350
next_token (s );
360
351
ret -> parameters [0 ] = power (s );
361
- if (ret -> parameters [0 ] == NULL ) {
362
- free (ret );
363
- return NULL ;
364
- }
352
+ CHECK_NULL (ret -> parameters [0 ], te_free (ret ));
365
353
break ;
366
354
367
355
case TE_FUNCTION2 : case TE_FUNCTION3 : case TE_FUNCTION4 :
@@ -371,9 +359,7 @@ static te_expr *base(state *s) {
371
359
arity = ARITY (s -> type );
372
360
373
361
ret = new_expr (s -> type , 0 );
374
- if (ret == NULL ) {
375
- return NULL ;
376
- }
362
+ CHECK_NULL (ret );
377
363
378
364
ret -> function = s -> function ;
379
365
if (IS_CLOSURE (s -> type )) ret -> parameters [arity ] = s -> context ;
@@ -386,14 +372,7 @@ static te_expr *base(state *s) {
386
372
for (i = 0 ; i < arity ; i ++ ) {
387
373
next_token (s );
388
374
ret -> parameters [i ] = expr (s );
389
- if (ret -> parameters [i ] == NULL ) {
390
- int j ;
391
- for (j = 0 ; j < i ; ++ j ) {
392
- te_free (ret -> parameters [j ]);
393
- }
394
- free (ret );
395
- return NULL ;
396
- }
375
+ CHECK_NULL (ret -> parameters [i ], te_free (ret ));
397
376
398
377
if (s -> type != TOK_SEP ) {
399
378
break ;
@@ -411,9 +390,7 @@ static te_expr *base(state *s) {
411
390
case TOK_OPEN :
412
391
next_token (s );
413
392
ret = list (s );
414
- if (ret == NULL ) {
415
- return NULL ;
416
- }
393
+ CHECK_NULL (ret );
417
394
418
395
if (s -> type != TOK_CLOSE ) {
419
396
s -> type = TOK_ERROR ;
@@ -424,9 +401,7 @@ static te_expr *base(state *s) {
424
401
425
402
default :
426
403
ret = new_expr (0 , 0 );
427
- if (ret == NULL ) {
428
- return NULL ;
429
- }
404
+ CHECK_NULL (ret );
430
405
431
406
s -> type = TOK_ERROR ;
432
407
ret -> value = NAN ;
@@ -451,15 +426,10 @@ static te_expr *power(state *s) {
451
426
ret = base (s );
452
427
} else {
453
428
te_expr * b = base (s );
454
- if (b == NULL ) {
455
- return NULL ;
456
- }
429
+ CHECK_NULL (b );
457
430
458
431
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , b );
459
- if (ret == NULL ) {
460
- te_free (b );
461
- return NULL ;
462
- }
432
+ CHECK_NULL (ret , te_free (b ));
463
433
464
434
ret -> function = negate ;
465
435
}
@@ -471,9 +441,7 @@ static te_expr *power(state *s) {
471
441
static te_expr * factor (state * s ) {
472
442
/* <factor> = <power> {"^" <power>} */
473
443
te_expr * ret = power (s );
474
- if (ret == NULL ) {
475
- return NULL ;
476
- }
444
+ CHECK_NULL (ret );
477
445
478
446
int neg = 0 ;
479
447
@@ -493,35 +461,21 @@ static te_expr *factor(state *s) {
493
461
if (insertion ) {
494
462
/* Make exponentiation go right-to-left. */
495
463
te_expr * p = power (s );
496
- if (p == NULL ) {
497
- te_free (ret );
498
- return NULL ;
499
- }
464
+ CHECK_NULL (p , te_free (ret ));
500
465
501
466
te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], p );
502
- if (insert == NULL ) {
503
- te_free (p );
504
- te_free (ret );
505
- return NULL ;
506
- }
467
+ CHECK_NULL (insert , te_free (p ), te_free (ret ));
507
468
508
469
insert -> function = t ;
509
470
insertion -> parameters [1 ] = insert ;
510
471
insertion = insert ;
511
472
} else {
512
473
te_expr * p = power (s );
513
- if (p == NULL ) {
514
- te_free (ret );
515
- return NULL ;
516
- }
474
+ CHECK_NULL (p , te_free (ret ));
517
475
518
476
te_expr * prev = ret ;
519
477
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , p );
520
- if (ret == NULL ) {
521
- te_free (p );
522
- te_free (prev );
523
- return NULL ;
524
- }
478
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
525
479
526
480
ret -> function = t ;
527
481
insertion = ret ;
@@ -531,10 +485,7 @@ static te_expr *factor(state *s) {
531
485
if (neg ) {
532
486
te_expr * prev = ret ;
533
487
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
534
- if (ret == NULL ) {
535
- te_free (prev );
536
- return NULL ;
537
- }
488
+ CHECK_NULL (ret , te_free (prev ));
538
489
539
490
ret -> function = negate ;
540
491
}
@@ -545,26 +496,17 @@ static te_expr *factor(state *s) {
545
496
static te_expr * factor (state * s ) {
546
497
/* <factor> = <power> {"^" <power>} */
547
498
te_expr * ret = power (s );
548
- if (ret == NULL ) {
549
- return NULL ;
550
- }
499
+ CHECK_NULL (ret );
551
500
552
501
while (s -> type == TOK_INFIX && (s -> function == pow )) {
553
502
te_fun2 t = s -> function ;
554
503
next_token (s );
555
504
te_expr * p = power (s );
556
- if (p == NULL ) {
557
- te_free (ret );
558
- return NULL ;
559
- }
505
+ CHECK_NULL (p , te_free (ret ));
560
506
561
507
te_expr * prev = ret ;
562
508
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , p );
563
- if (ret == NULL ) {
564
- te_free (p );
565
- te_free (prev );
566
- return NULL ;
567
- }
509
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
568
510
569
511
ret -> function = t ;
570
512
}
@@ -578,26 +520,17 @@ static te_expr *factor(state *s) {
578
520
static te_expr * term (state * s ) {
579
521
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
580
522
te_expr * ret = factor (s );
581
- if (ret == NULL ) {
582
- return NULL ;
583
- }
523
+ CHECK_NULL (ret );
584
524
585
525
while (s -> type == TOK_INFIX && (s -> function == mul || s -> function == divide || s -> function == fmod )) {
586
526
te_fun2 t = s -> function ;
587
527
next_token (s );
588
528
te_expr * f = factor (s );
589
- if (f == NULL ) {
590
- te_free (ret );
591
- return NULL ;
592
- }
529
+ CHECK_NULL (f , te_free (ret ));
593
530
594
531
te_expr * prev = ret ;
595
532
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , f );
596
- if (ret == NULL ) {
597
- te_free (f );
598
- te_free (prev );
599
- return NULL ;
600
- }
533
+ CHECK_NULL (ret , te_free (f ), te_free (prev ));
601
534
602
535
ret -> function = t ;
603
536
}
@@ -609,26 +542,17 @@ static te_expr *term(state *s) {
609
542
static te_expr * expr (state * s ) {
610
543
/* <expr> = <term> {("+" | "-") <term>} */
611
544
te_expr * ret = term (s );
612
- if (ret == NULL ) {
613
- return NULL ;
614
- }
545
+ CHECK_NULL (ret );
615
546
616
547
while (s -> type == TOK_INFIX && (s -> function == add || s -> function == sub )) {
617
548
te_fun2 t = s -> function ;
618
549
next_token (s );
619
550
te_expr * te = term (s );
620
- if (te == NULL ) {
621
- te_free (ret );
622
- return NULL ;
623
- }
551
+ CHECK_NULL (te , te_free (ret ));
624
552
625
553
te_expr * prev = ret ;
626
554
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , te );
627
- if (ret == NULL ) {
628
- te_free (te );
629
- te_free (prev );
630
- return NULL ;
631
- }
555
+ CHECK_NULL (ret , te_free (te ), te_free (prev ));
632
556
633
557
ret -> function = t ;
634
558
}
@@ -640,25 +564,16 @@ static te_expr *expr(state *s) {
640
564
static te_expr * list (state * s ) {
641
565
/* <list> = <expr> {"," <expr>} */
642
566
te_expr * ret = expr (s );
643
- if (ret == NULL ) {
644
- return NULL ;
645
- }
567
+ CHECK_NULL (ret );
646
568
647
569
while (s -> type == TOK_SEP ) {
648
570
next_token (s );
649
571
te_expr * e = expr (s );
650
- if (e == NULL ) {
651
- te_free (ret );
652
- return NULL ;
653
- }
572
+ CHECK_NULL (e , te_free (ret ));
654
573
655
574
te_expr * prev = ret ;
656
575
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , e );
657
- if (ret == NULL ) {
658
- te_free (e );
659
- te_free (prev );
660
- return NULL ;
661
- }
576
+ CHECK_NULL (ret , te_free (e ), te_free (prev ));
662
577
663
578
ret -> function = comma ;
664
579
}
0 commit comments