@@ -65,7 +65,7 @@ typedef struct state {
65
65
const char * start ;
66
66
const char * next ;
67
67
int type ;
68
- union { double value ; const double * bound ; const void * function ;} ;
68
+ union value v ;
69
69
void * context ;
70
70
71
71
const te_variable * lookup ;
@@ -91,7 +91,7 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
91
91
memcpy (ret -> parameters , parameters , psize );
92
92
}
93
93
ret -> type = type ;
94
- ret -> bound = 0 ;
94
+ ret -> v . bound = 0 ;
95
95
return ret ;
96
96
}
97
97
@@ -238,7 +238,7 @@ void next_token(state *s) {
238
238
239
239
/* Try reading a number. */
240
240
if ((s -> next [0 ] >= '0' && s -> next [0 ] <= '9' ) || s -> next [0 ] == '.' ) {
241
- s -> value = strtod (s -> next , (char * * )& s -> next );
241
+ s -> v . value = strtod (s -> next , (char * * )& s -> next );
242
242
s -> type = TOK_NUMBER ;
243
243
} else {
244
244
/* Look for a variable or builtin function call. */
@@ -257,7 +257,7 @@ void next_token(state *s) {
257
257
{
258
258
case TE_VARIABLE :
259
259
s -> type = TOK_VARIABLE ;
260
- s -> bound = var -> address ;
260
+ s -> v . bound = var -> address ;
261
261
break ;
262
262
263
263
case TE_CLOSURE0 : case TE_CLOSURE1 : case TE_CLOSURE2 : case TE_CLOSURE3 : /* Falls through. */
@@ -267,20 +267,20 @@ void next_token(state *s) {
267
267
case TE_FUNCTION0 : case TE_FUNCTION1 : case TE_FUNCTION2 : case TE_FUNCTION3 : /* Falls through. */
268
268
case TE_FUNCTION4 : case TE_FUNCTION5 : case TE_FUNCTION6 : case TE_FUNCTION7 : /* Falls through. */
269
269
s -> type = var -> type ;
270
- s -> function = var -> address ;
270
+ s -> v . function = var -> address ;
271
271
break ;
272
272
}
273
273
}
274
274
275
275
} else {
276
276
/* Look for an operator or special character. */
277
277
switch (s -> next ++ [0 ]) {
278
- case '+' : s -> type = TOK_INFIX ; s -> function = add ; break ;
279
- case '-' : s -> type = TOK_INFIX ; s -> function = sub ; break ;
280
- case '*' : s -> type = TOK_INFIX ; s -> function = mul ; break ;
281
- case '/' : s -> type = TOK_INFIX ; s -> function = divide ; break ;
282
- case '^' : s -> type = TOK_INFIX ; s -> function = pow ; break ;
283
- case '%' : s -> type = TOK_INFIX ; s -> function = fmod ; break ;
278
+ case '+' : s -> type = TOK_INFIX ; s -> v . function = add ; break ;
279
+ case '-' : s -> type = TOK_INFIX ; s -> v . function = sub ; break ;
280
+ case '*' : s -> type = TOK_INFIX ; s -> v . function = mul ; break ;
281
+ case '/' : s -> type = TOK_INFIX ; s -> v . function = divide ; break ;
282
+ case '^' : s -> type = TOK_INFIX ; s -> v . function = pow ; break ;
283
+ case '%' : s -> type = TOK_INFIX ; s -> v . function = fmod ; break ;
284
284
case '(' : s -> type = TOK_OPEN ; break ;
285
285
case ')' : s -> type = TOK_CLOSE ; break ;
286
286
case ',' : s -> type = TOK_SEP ; break ;
@@ -305,20 +305,20 @@ static te_expr *base(state *s) {
305
305
switch (TYPE_MASK (s -> type )) {
306
306
case TOK_NUMBER :
307
307
ret = new_expr (TE_CONSTANT , 0 );
308
- ret -> value = s -> value ;
308
+ ret -> v . value = s -> v . value ;
309
309
next_token (s );
310
310
break ;
311
311
312
312
case TOK_VARIABLE :
313
313
ret = new_expr (TE_VARIABLE , 0 );
314
- ret -> bound = s -> bound ;
314
+ ret -> v . bound = s -> v . bound ;
315
315
next_token (s );
316
316
break ;
317
317
318
318
case TE_FUNCTION0 :
319
319
case TE_CLOSURE0 :
320
320
ret = new_expr (s -> type , 0 );
321
- ret -> function = s -> function ;
321
+ ret -> v . function = s -> v . function ;
322
322
if (IS_CLOSURE (s -> type )) ret -> parameters [0 ] = s -> context ;
323
323
next_token (s );
324
324
if (s -> type == TOK_OPEN ) {
@@ -334,7 +334,7 @@ static te_expr *base(state *s) {
334
334
case TE_FUNCTION1 :
335
335
case TE_CLOSURE1 :
336
336
ret = new_expr (s -> type , 0 );
337
- ret -> function = s -> function ;
337
+ ret -> v . function = s -> v . function ;
338
338
if (IS_CLOSURE (s -> type )) ret -> parameters [1 ] = s -> context ;
339
339
next_token (s );
340
340
ret -> parameters [0 ] = power (s );
@@ -347,7 +347,7 @@ static te_expr *base(state *s) {
347
347
arity = ARITY (s -> type );
348
348
349
349
ret = new_expr (s -> type , 0 );
350
- ret -> function = s -> function ;
350
+ ret -> v . function = s -> v . function ;
351
351
if (IS_CLOSURE (s -> type )) ret -> parameters [arity ] = s -> context ;
352
352
next_token (s );
353
353
@@ -384,7 +384,7 @@ static te_expr *base(state *s) {
384
384
default :
385
385
ret = new_expr (0 , 0 );
386
386
s -> type = TOK_ERROR ;
387
- ret -> value = NAN ;
387
+ ret -> v . value = NAN ;
388
388
break ;
389
389
}
390
390
@@ -395,8 +395,8 @@ static te_expr *base(state *s) {
395
395
static te_expr * power (state * s ) {
396
396
/* <power> = {("-" | "+")} <base> */
397
397
int sign = 1 ;
398
- while (s -> type == TOK_INFIX && (s -> function == add || s -> function == sub )) {
399
- if (s -> function == sub ) sign = - sign ;
398
+ while (s -> type == TOK_INFIX && (s -> v . function == add || s -> v . function == sub )) {
399
+ if (s -> v . function == sub ) sign = - sign ;
400
400
next_token (s );
401
401
}
402
402
@@ -406,7 +406,7 @@ static te_expr *power(state *s) {
406
406
ret = base (s );
407
407
} else {
408
408
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , base (s ));
409
- ret -> function = negate ;
409
+ ret -> v . function = negate ;
410
410
}
411
411
412
412
return ret ;
@@ -420,33 +420,33 @@ static te_expr *factor(state *s) {
420
420
int neg = 0 ;
421
421
te_expr * insertion = 0 ;
422
422
423
- if (ret -> type == (TE_FUNCTION1 | TE_FLAG_PURE ) && ret -> function == negate ) {
423
+ if (ret -> type == (TE_FUNCTION1 | TE_FLAG_PURE ) && ret -> v . function == negate ) {
424
424
te_expr * se = ret -> parameters [0 ];
425
425
free (ret );
426
426
ret = se ;
427
427
neg = 1 ;
428
428
}
429
429
430
- while (s -> type == TOK_INFIX && (s -> function == pow )) {
431
- te_fun2 t = s -> function ;
430
+ while (s -> type == TOK_INFIX && (s -> v . function == pow )) {
431
+ te_fun2 t = s -> v . function ;
432
432
next_token (s );
433
433
434
434
if (insertion ) {
435
435
/* Make exponentiation go right-to-left. */
436
436
te_expr * insert = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , insertion -> parameters [1 ], power (s ));
437
- insert -> function = t ;
437
+ insert -> v . function = t ;
438
438
insertion -> parameters [1 ] = insert ;
439
439
insertion = insert ;
440
440
} else {
441
441
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
442
- ret -> function = t ;
442
+ ret -> v . function = t ;
443
443
insertion = ret ;
444
444
}
445
445
}
446
446
447
447
if (neg ) {
448
448
ret = NEW_EXPR (TE_FUNCTION1 | TE_FLAG_PURE , ret );
449
- ret -> function = negate ;
449
+ ret -> v . function = negate ;
450
450
}
451
451
452
452
return ret ;
@@ -456,11 +456,11 @@ static te_expr *factor(state *s) {
456
456
/* <factor> = <power> {"^" <power>} */
457
457
te_expr * ret = power (s );
458
458
459
- while (s -> type == TOK_INFIX && (s -> function == pow )) {
460
- te_fun2 t = s -> function ;
459
+ while (s -> type == TOK_INFIX && (s -> v . function == pow )) {
460
+ te_fun2 t = s -> v . function ;
461
461
next_token (s );
462
462
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , power (s ));
463
- ret -> function = t ;
463
+ ret -> v . function = t ;
464
464
}
465
465
466
466
return ret ;
@@ -473,11 +473,11 @@ static te_expr *term(state *s) {
473
473
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
474
474
te_expr * ret = factor (s );
475
475
476
- while (s -> type == TOK_INFIX && (s -> function == mul || s -> function == divide || s -> function == fmod )) {
477
- te_fun2 t = s -> function ;
476
+ while (s -> type == TOK_INFIX && (s -> v . function == mul || s -> v . function == divide || s -> v . function == fmod )) {
477
+ te_fun2 t = s -> v . function ;
478
478
next_token (s );
479
479
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , factor (s ));
480
- ret -> function = t ;
480
+ ret -> v . function = t ;
481
481
}
482
482
483
483
return ret ;
@@ -488,11 +488,11 @@ static te_expr *expr(state *s) {
488
488
/* <expr> = <term> {("+" | "-") <term>} */
489
489
te_expr * ret = term (s );
490
490
491
- while (s -> type == TOK_INFIX && (s -> function == add || s -> function == sub )) {
492
- te_fun2 t = s -> function ;
491
+ while (s -> type == TOK_INFIX && (s -> v . function == add || s -> v . function == sub )) {
492
+ te_fun2 t = s -> v . function ;
493
493
next_token (s );
494
494
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , term (s ));
495
- ret -> function = t ;
495
+ ret -> v . function = t ;
496
496
}
497
497
498
498
return ret ;
@@ -506,23 +506,23 @@ static te_expr *list(state *s) {
506
506
while (s -> type == TOK_SEP ) {
507
507
next_token (s );
508
508
ret = NEW_EXPR (TE_FUNCTION2 | TE_FLAG_PURE , ret , expr (s ));
509
- ret -> function = comma ;
509
+ ret -> v . function = comma ;
510
510
}
511
511
512
512
return ret ;
513
513
}
514
514
515
515
516
- #define TE_FUN (...) ((double(*)(__VA_ARGS__))n->function)
516
+ #define TE_FUN (...) ((double(*)(__VA_ARGS__))n->v. function)
517
517
#define M (e ) te_eval(n->parameters[e])
518
518
519
519
520
520
double te_eval (const te_expr * n ) {
521
521
if (!n ) return NAN ;
522
522
523
523
switch (TYPE_MASK (n -> type )) {
524
- case TE_CONSTANT : return n -> value ;
525
- case TE_VARIABLE : return * n -> bound ;
524
+ case TE_CONSTANT : return n -> v . value ;
525
+ case TE_VARIABLE : return * n -> v . bound ;
526
526
527
527
case TE_FUNCTION0 : case TE_FUNCTION1 : case TE_FUNCTION2 : case TE_FUNCTION3 :
528
528
case TE_FUNCTION4 : case TE_FUNCTION5 : case TE_FUNCTION6 : case TE_FUNCTION7 :
@@ -580,7 +580,7 @@ static void optimize(te_expr *n) {
580
580
const double value = te_eval (n );
581
581
te_free_parameters (n );
582
582
n -> type = TE_CONSTANT ;
583
- n -> value = value ;
583
+ n -> v . value = value ;
584
584
}
585
585
}
586
586
}
@@ -627,8 +627,8 @@ static void pn (const te_expr *n, int depth) {
627
627
printf ("%*s" , depth , "" );
628
628
629
629
switch (TYPE_MASK (n -> type )) {
630
- case TE_CONSTANT : printf ("%f\n" , n -> value ); break ;
631
- case TE_VARIABLE : printf ("bound %p\n" , n -> bound ); break ;
630
+ case TE_CONSTANT : printf ("%f\n" , n -> v . value ); break ;
631
+ case TE_VARIABLE : printf ("bound %p\n" , n -> v . bound ); break ;
632
632
633
633
case TE_FUNCTION0 : case TE_FUNCTION1 : case TE_FUNCTION2 : case TE_FUNCTION3 :
634
634
case TE_FUNCTION4 : case TE_FUNCTION5 : case TE_FUNCTION6 : case TE_FUNCTION7 :
0 commit comments