Skip to content

Commit e88ca22

Browse files
committed
Get rid of unnamed unions to allow c99 build.
1 parent 03dfe50 commit e88ca22

File tree

4 files changed

+50
-45
lines changed

4 files changed

+50
-45
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CCFLAGS = -ansi -Wall -Wshadow -O2
1+
CCFLAGS = -ansi -Wall -Wshadow -O2 -std=c89 #-pedantic
22
LFLAGS = -lm
33

44
.PHONY = all clean

test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ void test_optimize() {
564564

565565
/* The answer should be know without
566566
* even running eval. */
567-
lfequal(ex->value, answer);
567+
lfequal(ex->v.value, answer);
568568
lfequal(te_eval(ex), answer);
569569

570570
te_free(ex);

tinyexpr.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typedef struct state {
6565
const char *start;
6666
const char *next;
6767
int type;
68-
union {double value; const double *bound; const void *function;};
68+
union value v;
6969
void *context;
7070

7171
const te_variable *lookup;
@@ -91,7 +91,7 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
9191
memcpy(ret->parameters, parameters, psize);
9292
}
9393
ret->type = type;
94-
ret->bound = 0;
94+
ret->v.bound = 0;
9595
return ret;
9696
}
9797

@@ -238,7 +238,7 @@ void next_token(state *s) {
238238

239239
/* Try reading a number. */
240240
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);
242242
s->type = TOK_NUMBER;
243243
} else {
244244
/* Look for a variable or builtin function call. */
@@ -257,7 +257,7 @@ void next_token(state *s) {
257257
{
258258
case TE_VARIABLE:
259259
s->type = TOK_VARIABLE;
260-
s->bound = var->address;
260+
s->v.bound = var->address;
261261
break;
262262

263263
case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3: /* Falls through. */
@@ -267,20 +267,20 @@ void next_token(state *s) {
267267
case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: /* Falls through. */
268268
case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: /* Falls through. */
269269
s->type = var->type;
270-
s->function = var->address;
270+
s->v.function = var->address;
271271
break;
272272
}
273273
}
274274

275275
} else {
276276
/* Look for an operator or special character. */
277277
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;
284284
case '(': s->type = TOK_OPEN; break;
285285
case ')': s->type = TOK_CLOSE; break;
286286
case ',': s->type = TOK_SEP; break;
@@ -305,20 +305,20 @@ static te_expr *base(state *s) {
305305
switch (TYPE_MASK(s->type)) {
306306
case TOK_NUMBER:
307307
ret = new_expr(TE_CONSTANT, 0);
308-
ret->value = s->value;
308+
ret->v.value = s->v.value;
309309
next_token(s);
310310
break;
311311

312312
case TOK_VARIABLE:
313313
ret = new_expr(TE_VARIABLE, 0);
314-
ret->bound = s->bound;
314+
ret->v.bound = s->v.bound;
315315
next_token(s);
316316
break;
317317

318318
case TE_FUNCTION0:
319319
case TE_CLOSURE0:
320320
ret = new_expr(s->type, 0);
321-
ret->function = s->function;
321+
ret->v.function = s->v.function;
322322
if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context;
323323
next_token(s);
324324
if (s->type == TOK_OPEN) {
@@ -334,7 +334,7 @@ static te_expr *base(state *s) {
334334
case TE_FUNCTION1:
335335
case TE_CLOSURE1:
336336
ret = new_expr(s->type, 0);
337-
ret->function = s->function;
337+
ret->v.function = s->v.function;
338338
if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context;
339339
next_token(s);
340340
ret->parameters[0] = power(s);
@@ -347,7 +347,7 @@ static te_expr *base(state *s) {
347347
arity = ARITY(s->type);
348348

349349
ret = new_expr(s->type, 0);
350-
ret->function = s->function;
350+
ret->v.function = s->v.function;
351351
if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context;
352352
next_token(s);
353353

@@ -384,7 +384,7 @@ static te_expr *base(state *s) {
384384
default:
385385
ret = new_expr(0, 0);
386386
s->type = TOK_ERROR;
387-
ret->value = NAN;
387+
ret->v.value = NAN;
388388
break;
389389
}
390390

@@ -395,8 +395,8 @@ static te_expr *base(state *s) {
395395
static te_expr *power(state *s) {
396396
/* <power> = {("-" | "+")} <base> */
397397
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;
400400
next_token(s);
401401
}
402402

@@ -406,7 +406,7 @@ static te_expr *power(state *s) {
406406
ret = base(s);
407407
} else {
408408
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
409-
ret->function = negate;
409+
ret->v.function = negate;
410410
}
411411

412412
return ret;
@@ -420,33 +420,33 @@ static te_expr *factor(state *s) {
420420
int neg = 0;
421421
te_expr *insertion = 0;
422422

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) {
424424
te_expr *se = ret->parameters[0];
425425
free(ret);
426426
ret = se;
427427
neg = 1;
428428
}
429429

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;
432432
next_token(s);
433433

434434
if (insertion) {
435435
/* Make exponentiation go right-to-left. */
436436
te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
437-
insert->function = t;
437+
insert->v.function = t;
438438
insertion->parameters[1] = insert;
439439
insertion = insert;
440440
} else {
441441
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
442-
ret->function = t;
442+
ret->v.function = t;
443443
insertion = ret;
444444
}
445445
}
446446

447447
if (neg) {
448448
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
449-
ret->function = negate;
449+
ret->v.function = negate;
450450
}
451451

452452
return ret;
@@ -456,11 +456,11 @@ static te_expr *factor(state *s) {
456456
/* <factor> = <power> {"^" <power>} */
457457
te_expr *ret = power(s);
458458

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;
461461
next_token(s);
462462
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
463-
ret->function = t;
463+
ret->v.function = t;
464464
}
465465

466466
return ret;
@@ -473,11 +473,11 @@ static te_expr *term(state *s) {
473473
/* <term> = <factor> {("*" | "/" | "%") <factor>} */
474474
te_expr *ret = factor(s);
475475

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;
478478
next_token(s);
479479
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
480-
ret->function = t;
480+
ret->v.function = t;
481481
}
482482

483483
return ret;
@@ -488,11 +488,11 @@ static te_expr *expr(state *s) {
488488
/* <expr> = <term> {("+" | "-") <term>} */
489489
te_expr *ret = term(s);
490490

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;
493493
next_token(s);
494494
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
495-
ret->function = t;
495+
ret->v.function = t;
496496
}
497497

498498
return ret;
@@ -506,23 +506,23 @@ static te_expr *list(state *s) {
506506
while (s->type == TOK_SEP) {
507507
next_token(s);
508508
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
509-
ret->function = comma;
509+
ret->v.function = comma;
510510
}
511511

512512
return ret;
513513
}
514514

515515

516-
#define TE_FUN(...) ((double(*)(__VA_ARGS__))n->function)
516+
#define TE_FUN(...) ((double(*)(__VA_ARGS__))n->v.function)
517517
#define M(e) te_eval(n->parameters[e])
518518

519519

520520
double te_eval(const te_expr *n) {
521521
if (!n) return NAN;
522522

523523
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;
526526

527527
case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
528528
case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
@@ -580,7 +580,7 @@ static void optimize(te_expr *n) {
580580
const double value = te_eval(n);
581581
te_free_parameters(n);
582582
n->type = TE_CONSTANT;
583-
n->value = value;
583+
n->v.value = value;
584584
}
585585
}
586586
}
@@ -627,8 +627,8 @@ static void pn (const te_expr *n, int depth) {
627627
printf("%*s", depth, "");
628628

629629
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;
632632

633633
case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
634634
case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:

tinyexpr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ extern "C" {
3131
#endif
3232

3333

34+
union value {
35+
double value;
36+
const double *bound;
37+
const void *function;
38+
};
3439

3540
typedef struct te_expr {
3641
int type;
37-
union {double value; const double *bound; const void *function;};
42+
union value v;
3843
void *parameters[1];
3944
} te_expr;
4045

0 commit comments

Comments
 (0)