@@ -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 ;
@@ -411,9 +397,7 @@ static te_expr *base(state *s) {
411
397
case TOK_OPEN :
412
398
next_token (s );
413
399
ret = list (s );
414
- if (ret == NULL ) {
415
- return NULL ;
416
- }
400
+ CHECK_NULL (ret );
417
401
418
402
if (s -> type != TOK_CLOSE ) {
419
403
s -> type = TOK_ERROR ;
@@ -424,9 +408,7 @@ static te_expr *base(state *s) {
424
408
425
409
default :
426
410
ret = new_expr (0 , 0 );
427
- if (ret == NULL ) {
428
- return NULL ;
429
- }
411
+ CHECK_NULL (ret );
430
412
431
413
s -> type = TOK_ERROR ;
432
414
ret -> value = NAN ;
@@ -451,15 +433,10 @@ static te_expr *power(state *s) {
451
433
ret = base (s );
452
434
} else {
453
435
te_expr * b = base (s );
454
- if (b == NULL ) {
455
- return NULL ;
456
- }
436
+ CHECK_NULL (b );
457
437
458
438
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , b );
459
- if (ret == NULL ) {
460
- te_free (b );
461
- return NULL ;
462
- }
439
+ CHECK_NULL (ret , te_free (b ));
463
440
464
441
ret -> function = negate ;
465
442
}
@@ -471,9 +448,7 @@ static te_expr *power(state *s) {
471
448
static te_expr * factor (state * s ) {
472
449
/* <factor> = <power> {"^" <power>} */
473
450
te_expr * ret = power (s );
474
- if (ret == NULL ) {
475
- return NULL ;
476
- }
451
+ CHECK_NULL (ret );
477
452
478
453
int neg = 0 ;
479
454
@@ -493,35 +468,21 @@ static te_expr *factor(state *s) {
493
468
if (insertion ) {
494
469
/* Make exponentiation go right-to-left. */
495
470
te_expr * p = power (s );
496
- if (p == NULL ) {
497
- te_free (ret );
498
- return NULL ;
499
- }
471
+ CHECK_NULL (p , te_free (ret ));
500
472
501
473
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
- }
474
+ CHECK_NULL (insert , te_free (p ), te_free (ret ));
507
475
508
476
insert -> function = t ;
509
477
insertion -> parameters [1 ] = insert ;
510
478
insertion = insert ;
511
479
} else {
512
480
te_expr * p = power (s );
513
- if (p == NULL ) {
514
- te_free (ret );
515
- return NULL ;
516
- }
481
+ CHECK_NULL (p , te_free (ret ));
517
482
518
483
te_expr * prev = ret ;
519
484
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
- }
485
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
525
486
526
487
ret -> function = t ;
527
488
insertion = ret ;
@@ -531,10 +492,7 @@ static te_expr *factor(state *s) {
531
492
if (neg ) {
532
493
te_expr * prev = ret ;
533
494
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
534
- if (ret == NULL ) {
535
- te_free (prev );
536
- return NULL ;
537
- }
495
+ CHECK_NULL (ret , te_free (prev ));
538
496
539
497
ret -> function = negate ;
540
498
}
@@ -545,26 +503,17 @@ static te_expr *factor(state *s) {
545
503
static te_expr * factor (state * s ) {
546
504
/* <factor> = <power> {"^" <power>} */
547
505
te_expr * ret = power (s );
548
- if (ret == NULL ) {
549
- return NULL ;
550
- }
506
+ CHECK_NULL (ret );
551
507
552
508
while (s -> type == TOK_INFIX && (s -> function == pow )) {
553
509
te_fun2 t = s -> function ;
554
510
next_token (s );
555
511
te_expr * p = power (s );
556
- if (p == NULL ) {
557
- te_free (ret );
558
- return NULL ;
559
- }
512
+ CHECK_NULL (p , te_free (ret ));
560
513
561
514
te_expr * prev = ret ;
562
515
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
- }
516
+ CHECK_NULL (ret , te_free (p ), te_free (prev ));
568
517
569
518
ret -> function = t ;
570
519
}
@@ -578,26 +527,17 @@ static te_expr *factor(state *s) {
578
527
static te_expr * term (state * s ) {
579
528
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
580
529
te_expr * ret = factor (s );
581
- if (ret == NULL ) {
582
- return NULL ;
583
- }
530
+ CHECK_NULL (ret );
584
531
585
532
while (s -> type == TOK_INFIX && (s -> function == mul || s -> function == divide || s -> function == fmod )) {
586
533
te_fun2 t = s -> function ;
587
534
next_token (s );
588
535
te_expr * f = factor (s );
589
- if (f == NULL ) {
590
- te_free (ret );
591
- return NULL ;
592
- }
536
+ CHECK_NULL (f , te_free (ret ));
593
537
594
538
te_expr * prev = ret ;
595
539
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
- }
540
+ CHECK_NULL (ret , te_free (f ), te_free (prev ));
601
541
602
542
ret -> function = t ;
603
543
}
@@ -609,26 +549,17 @@ static te_expr *term(state *s) {
609
549
static te_expr * expr (state * s ) {
610
550
/* <expr> = <term> {("+" | "-") <term>} */
611
551
te_expr * ret = term (s );
612
- if (ret == NULL ) {
613
- return NULL ;
614
- }
552
+ CHECK_NULL (ret );
615
553
616
554
while (s -> type == TOK_INFIX && (s -> function == add || s -> function == sub )) {
617
555
te_fun2 t = s -> function ;
618
556
next_token (s );
619
557
te_expr * te = term (s );
620
- if (te == NULL ) {
621
- te_free (ret );
622
- return NULL ;
623
- }
558
+ CHECK_NULL (te , te_free (ret ));
624
559
625
560
te_expr * prev = ret ;
626
561
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
- }
562
+ CHECK_NULL (ret , te_free (te ), te_free (prev ));
632
563
633
564
ret -> function = t ;
634
565
}
@@ -640,25 +571,16 @@ static te_expr *expr(state *s) {
640
571
static te_expr * list (state * s ) {
641
572
/* <list> = <expr> {"," <expr>} */
642
573
te_expr * ret = expr (s );
643
- if (ret == NULL ) {
644
- return NULL ;
645
- }
574
+ CHECK_NULL (ret );
646
575
647
576
while (s -> type == TOK_SEP ) {
648
577
next_token (s );
649
578
te_expr * e = expr (s );
650
- if (e == NULL ) {
651
- te_free (ret );
652
- return NULL ;
653
- }
579
+ CHECK_NULL (e , te_free (ret ));
654
580
655
581
te_expr * prev = ret ;
656
582
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
- }
583
+ CHECK_NULL (ret , te_free (e ), te_free (prev ));
662
584
663
585
ret -> function = comma ;
664
586
}
0 commit comments