Skip to content

Instantly share code, notes, and snippets.

@dleslie
Last active Nov 21, 2020
Embed
What would you like to do?
How to make C enum/char*[] pairs with a single line
#include <stdio.h>
#define _COUNT_ARGS(X100, X99, X98, X97, X96, X95, X94, X93, X92, X91, X90, X89, X88, X87, X86, X85, X84, X83, X82, X81, X80, X79, X78, X77, X76, X75, X74, X73, X72, X71, X70, X69, X68, X67, X66, X65, X64, X63, X62, X61, X60, X59, X58, X57, X56, X55, X54, X53, X52, X51, X50, X49, X48, X47, X46, X45, X44, X43, X42, X41, X40, X39, X38, X37, X36, X35, X34, X33, X32, X31, X30, X29, X28, X27, X26, X25, X24, X23, X22, X21, X20, X19, X18, X17, X16, X15, X14, X13, X12, X11, X10, X9, X8, X7, X6, X5, X4, X3, X2, X1, N, ...) N
#define COUNT_ARGS(...) _COUNT_ARGS(__VA_ARGS__, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define EXPAND(X) X
#define CAR(X, ...) (X)
#define CDR(X, ...) (__VA_ARGS__)
#define MAP(MACRO, LST) MAP_(COUNT_ARGS LST, MACRO, LST)
#define MAP_(N, M, LST) MAP__(N, M, LST)
#define MAP__(N, M, LST) MAP_##N(M, LST)
#define MAP_1(M, LST) M LST
#define MAP_2(M, LST) EXPAND(M CAR LST) MAP_1(M, CDR LST)
#define MAP_3(M, LST) EXPAND(M CAR LST) MAP_2(M, CDR LST)
#define MAP_4(M, LST) EXPAND(M CAR LST) MAP_3(M, CDR LST)
#define MAP_5(M, LST) EXPAND(M CAR LST) MAP_4(M, CDR LST)
#define MAP_6(M, LST) EXPAND(M CAR LST) MAP_5(M, CDR LST)
#define MAP_7(M, LST) EXPAND(M CAR LST) MAP_6(M, CDR LST)
#define MAP_8(M, LST) EXPAND(M CAR LST) MAP_7(M, CDR LST)
#define MAP_9(M, LST) EXPAND(M CAR LST) MAP_8(M, CDR LST)
#define MAP_10(M, LST) EXPAND(M CAR LST) MAP_9(M, CDR LST)
#define MAP_11(M, LST) EXPAND(M CAR LST) MAP_10(M, CDR LST)
#define MAP_12(M, LST) EXPAND(M CAR LST) MAP_11(M, CDR LST)
#define MAP_13(M, LST) EXPAND(M CAR LST) MAP_12(M, CDR LST)
#define MAP_14(M, LST) EXPAND(M CAR LST) MAP_13(M, CDR LST)
#define MAP_15(M, LST) EXPAND(M CAR LST) MAP_14(M, CDR LST)
#define MAP_16(M, LST) EXPAND(M CAR LST) MAP_15(M, CDR LST)
#define MAP_17(M, LST) EXPAND(M CAR LST) MAP_16(M, CDR LST)
#define MAP_18(M, LST) EXPAND(M CAR LST) MAP_17(M, CDR LST)
#define MAP_19(M, LST) EXPAND(M CAR LST) MAP_18(M, CDR LST)
#define MAP_20(M, LST) EXPAND(M CAR LST) MAP_19(M, CDR LST)
#define MAP_21(M, LST) EXPAND(M CAR LST) MAP_20(M, CDR LST)
#define MAP_22(M, LST) EXPAND(M CAR LST) MAP_21(M, CDR LST)
#define MAP_23(M, LST) EXPAND(M CAR LST) MAP_22(M, CDR LST)
#define MAP_24(M, LST) EXPAND(M CAR LST) MAP_23(M, CDR LST)
#define MAP_25(M, LST) EXPAND(M CAR LST) MAP_24(M, CDR LST)
#define MAP_26(M, LST) EXPAND(M CAR LST) MAP_25(M, CDR LST)
#define MAP_27(M, LST) EXPAND(M CAR LST) MAP_26(M, CDR LST)
#define MAP_28(M, LST) EXPAND(M CAR LST) MAP_27(M, CDR LST)
#define MAP_29(M, LST) EXPAND(M CAR LST) MAP_28(M, CDR LST)
#define MAP_30(M, LST) EXPAND(M CAR LST) MAP_29(M, CDR LST)
#define MAP_31(M, LST) EXPAND(M CAR LST) MAP_30(M, CDR LST)
#define MAP_32(M, LST) EXPAND(M CAR LST) MAP_31(M, CDR LST)
#define MAP_33(M, LST) EXPAND(M CAR LST) MAP_32(M, CDR LST)
#define MAP_34(M, LST) EXPAND(M CAR LST) MAP_33(M, CDR LST)
#define MAP_35(M, LST) EXPAND(M CAR LST) MAP_34(M, CDR LST)
#define MAP_36(M, LST) EXPAND(M CAR LST) MAP_35(M, CDR LST)
#define MAP_37(M, LST) EXPAND(M CAR LST) MAP_36(M, CDR LST)
#define MAP_38(M, LST) EXPAND(M CAR LST) MAP_37(M, CDR LST)
#define MAP_39(M, LST) EXPAND(M CAR LST) MAP_38(M, CDR LST)
#define MAP_40(M, LST) EXPAND(M CAR LST) MAP_39(M, CDR LST)
#define MAP_41(M, LST) EXPAND(M CAR LST) MAP_40(M, CDR LST)
#define MAP_42(M, LST) EXPAND(M CAR LST) MAP_41(M, CDR LST)
#define MAP_43(M, LST) EXPAND(M CAR LST) MAP_42(M, CDR LST)
#define MAP_44(M, LST) EXPAND(M CAR LST) MAP_43(M, CDR LST)
#define MAP_45(M, LST) EXPAND(M CAR LST) MAP_44(M, CDR LST)
#define MAP_46(M, LST) EXPAND(M CAR LST) MAP_45(M, CDR LST)
#define MAP_47(M, LST) EXPAND(M CAR LST) MAP_46(M, CDR LST)
#define MAP_48(M, LST) EXPAND(M CAR LST) MAP_47(M, CDR LST)
#define MAP_49(M, LST) EXPAND(M CAR LST) MAP_48(M, CDR LST)
#define MAP_50(M, LST) EXPAND(M CAR LST) MAP_49(M, CDR LST)
#define MAP_51(M, LST) EXPAND(M CAR LST) MAP_50(M, CDR LST)
#define MAP_52(M, LST) EXPAND(M CAR LST) MAP_51(M, CDR LST)
#define MAP_53(M, LST) EXPAND(M CAR LST) MAP_52(M, CDR LST)
#define MAP_54(M, LST) EXPAND(M CAR LST) MAP_53(M, CDR LST)
#define MAP_55(M, LST) EXPAND(M CAR LST) MAP_54(M, CDR LST)
#define MAP_56(M, LST) EXPAND(M CAR LST) MAP_55(M, CDR LST)
#define MAP_57(M, LST) EXPAND(M CAR LST) MAP_56(M, CDR LST)
#define MAP_58(M, LST) EXPAND(M CAR LST) MAP_57(M, CDR LST)
#define MAP_59(M, LST) EXPAND(M CAR LST) MAP_58(M, CDR LST)
#define MAP_60(M, LST) EXPAND(M CAR LST) MAP_59(M, CDR LST)
#define MAP_61(M, LST) EXPAND(M CAR LST) MAP_60(M, CDR LST)
#define MAP_62(M, LST) EXPAND(M CAR LST) MAP_61(M, CDR LST)
#define MAP_63(M, LST) EXPAND(M CAR LST) MAP_62(M, CDR LST)
#define MAP_64(M, LST) EXPAND(M CAR LST) MAP_63(M, CDR LST)
#define MAP_65(M, LST) EXPAND(M CAR LST) MAP_64(M, CDR LST)
#define MAP_66(M, LST) EXPAND(M CAR LST) MAP_65(M, CDR LST)
#define MAP_67(M, LST) EXPAND(M CAR LST) MAP_66(M, CDR LST)
#define MAP_68(M, LST) EXPAND(M CAR LST) MAP_67(M, CDR LST)
#define MAP_69(M, LST) EXPAND(M CAR LST) MAP_68(M, CDR LST)
#define MAP_70(M, LST) EXPAND(M CAR LST) MAP_69(M, CDR LST)
#define MAP_71(M, LST) EXPAND(M CAR LST) MAP_70(M, CDR LST)
#define MAP_72(M, LST) EXPAND(M CAR LST) MAP_71(M, CDR LST)
#define MAP_73(M, LST) EXPAND(M CAR LST) MAP_72(M, CDR LST)
#define MAP_74(M, LST) EXPAND(M CAR LST) MAP_73(M, CDR LST)
#define MAP_75(M, LST) EXPAND(M CAR LST) MAP_74(M, CDR LST)
#define MAP_76(M, LST) EXPAND(M CAR LST) MAP_75(M, CDR LST)
#define MAP_77(M, LST) EXPAND(M CAR LST) MAP_76(M, CDR LST)
#define MAP_78(M, LST) EXPAND(M CAR LST) MAP_77(M, CDR LST)
#define MAP_79(M, LST) EXPAND(M CAR LST) MAP_78(M, CDR LST)
#define MAP_80(M, LST) EXPAND(M CAR LST) MAP_79(M, CDR LST)
#define MAP_81(M, LST) EXPAND(M CAR LST) MAP_80(M, CDR LST)
#define MAP_82(M, LST) EXPAND(M CAR LST) MAP_81(M, CDR LST)
#define MAP_83(M, LST) EXPAND(M CAR LST) MAP_82(M, CDR LST)
#define MAP_84(M, LST) EXPAND(M CAR LST) MAP_83(M, CDR LST)
#define MAP_85(M, LST) EXPAND(M CAR LST) MAP_84(M, CDR LST)
#define MAP_86(M, LST) EXPAND(M CAR LST) MAP_85(M, CDR LST)
#define MAP_87(M, LST) EXPAND(M CAR LST) MAP_86(M, CDR LST)
#define MAP_88(M, LST) EXPAND(M CAR LST) MAP_87(M, CDR LST)
#define MAP_89(M, LST) EXPAND(M CAR LST) MAP_88(M, CDR LST)
#define MAP_90(M, LST) EXPAND(M CAR LST) MAP_89(M, CDR LST)
#define MAP_91(M, LST) EXPAND(M CAR LST) MAP_90(M, CDR LST)
#define MAP_92(M, LST) EXPAND(M CAR LST) MAP_91(M, CDR LST)
#define MAP_93(M, LST) EXPAND(M CAR LST) MAP_92(M, CDR LST)
#define MAP_94(M, LST) EXPAND(M CAR LST) MAP_93(M, CDR LST)
#define MAP_95(M, LST) EXPAND(M CAR LST) MAP_94(M, CDR LST)
#define MAP_96(M, LST) EXPAND(M CAR LST) MAP_95(M, CDR LST)
#define MAP_97(M, LST) EXPAND(M CAR LST) MAP_96(M, CDR LST)
#define MAP_98(M, LST) EXPAND(M CAR LST) MAP_97(M, CDR LST)
#define MAP_99(M, LST) EXPAND(M CAR LST) MAP_98(M, CDR LST)
#define STRINGIFY(X) #X
#define COMMA_STRING(X) STRINGIFY(X),
#define DEF_ENUM(name, ...) \
enum name##_enum { __VA_ARGS__, name##_COUNT };\
const char *name##_strings[] = { MAP(COMMA_STRING, (__VA_ARGS__)) ""};
DEF_ENUM(test, A, B, C, D, E, F, G)
int main() {
printf("%s\n", test_strings[F]);
return 0;
}
@dleslie

This comment has been minimized.

Copy link
Owner Author

@dleslie dleslie commented Nov 20, 2020

Code generated with:

(with-output-to-temp-buffer
  (dotimes (x 100)
    (insert (format "#define MAP_%s(M, LST)  EXPAND(M CAR LST) MAP_%s(M, CDR LST)\n" x (- x 1)))))
@dleslie

This comment has been minimized.

Copy link
Owner Author

@dleslie dleslie commented Nov 20, 2020

Bits cribbed from various docs and forum posts.

@dleslie

This comment has been minimized.

Copy link
Owner Author

@dleslie dleslie commented Nov 21, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment