1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <datatype99.h>
#include <stdio.h>
// Does not work properly on TCC for some reason.
#ifndef __TINYC__
// clang-format off
datatype(
Token,
(Ident, const char *),
(Int, int),
(LParen),
(RParen),
(Plus)
);
// clang-format on
void print_token(Token token) {
match(token) {
of(Ident, ident) printf("%s", *ident);
of(Int, x) printf("%d", *x);
of(LParen) printf("(");
of(RParen) printf(")");
of(Plus) printf(" + ");
}
}
int main(void) {
Token tokens[] = {
LParen(),
Ident("x"),
Plus(),
Int(123),
RParen(),
};
/*
* Output:
* (x + 123)
*/
for (size_t i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
print_token(tokens[i]);
}
puts("");
return 0;
}
#else
int main(void) {
return 0;
}
#endif // __TINYC__
|