diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-03 06:38:50 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-03 06:44:33 -0600 |
commit | bf8b214cdcbf66c63972d133e8ad451848223c7f (patch) | |
tree | a260087f4ed760ba5b8a94c3440d5c396783799f | |
parent | f2a053bba3f033fd76faa35c4eff6c60d914ea1c (diff) |
fixup! libmisc: macro.h: Allow FOREACH macros to nestlukeshu/macros
-rw-r--r-- | libmisc/include/libmisc/macro.h | 6 | ||||
-rw-r--r-- | libmisc/tests/test_macro.c | 12 |
2 files changed, 10 insertions, 8 deletions
diff --git a/libmisc/include/libmisc/macro.h b/libmisc/include/libmisc/macro.h index f155f7d..8793d1e 100644 --- a/libmisc/include/libmisc/macro.h +++ b/libmisc/include/libmisc/macro.h @@ -122,7 +122,9 @@ #define _LM_EVAL__2(...) _LM_EVAL__1(_LM_EVAL__1(__VA_ARGS__)) #define _LM_EVAL__1(...) __VA_ARGS__ +#define _LM_DEFER1(macro) macro LM_EAT() #define _LM_DEFER2(macro) macro LM_EAT LM_EAT()() +#define _LM_DEFER3(macro) macro LM_EAT LM_EAT LM_EAT()()() /** * LM_FOREACH_PARAM(func, (fixedparams), params...) calls @@ -135,7 +137,7 @@ */ #define LM_FOREACH_PARAM LM_CAT2_(_LM_FOREACH_PARAM, _LM_EVAL__16()) #define _LM_FOREACH_PARAM(...) _LM_EVAL__16(_LM_FOREACH_PARAM_impl(__VA_ARGS__)) -#define _LM_FOREACH_PARAM_LM_EVAL__16() _LM_DEFER2(_LM_FOREACH_PARAM_indirect)() +#define _LM_FOREACH_PARAM_LM_EVAL__16() _LM_DEFER1(_LM_FOREACH_PARAM_impl) #define _LM_FOREACH_PARAM_impl(func, fixedparams, ...) _LM_FOREACH_PARAM_ITEM(func, fixedparams, __VA_ARGS__, ()) #define _LM_FOREACH_PARAM_indirect() _LM_FOREACH_PARAM_impl @@ -163,7 +165,7 @@ */ #define LM_FOREACH_TUPLE LM_CAT2_(_LM_FOREACH_TUPLE, _LM_EVAL__16()) #define _LM_FOREACH_TUPLE(...) _LM_EVAL__16(_LM_FOREACH_TUPLE_impl(__VA_ARGS__)) -#define _LM_FOREACH_TUPLE_LM_EVAL__16() _LM_DEFER2(_LM_FOREACH_TUPLE_indirect)() +#define _LM_FOREACH_TUPLE_LM_EVAL__16() _LM_DEFER1(_LM_FOREACH_TUPLE_impl) #define _LM_FOREACH_TUPLE_impl(tuples, func, ...) \ LM_IF(LM_TUPLES_IS_NONEMPTY(tuples))( \ diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c index bc66f48..5648063 100644 --- a/libmisc/tests/test_macro.c +++ b/libmisc/tests/test_macro.c @@ -108,11 +108,11 @@ int main() { /* Recursion. */ { #define FNi(a, b) LM_CAT2(a, b) - #define FNo(a) LM_FOREACH_PARAM(FNi, (a), 4, 5, 6) - const char *str = LM_STR_(LM_FOREACH_PARAM(FNo, (), 1, 2, 3)); + #define FNo(a) LM_FOREACH_PARAM(FNi, (a), 1, 2, 3) + const char *str = LM_STR_(LM_FOREACH_PARAM(FNo, (), a, b, c)); #undef FNo #undef FNi - test_assert(strcmp(str, "14 15 16 24 25 26 34 35 36") == 0); + test_assert(strcmp(str, "a1 a2 a3 b1 b2 b3 c1 c2 c3") == 0); } /* Test that it works with the documented limit of params. */ @@ -159,11 +159,11 @@ int main() { /* Recursion. */ { #define FNi(a, b) LM_CAT2(a, b) - #define FNo(a) LM_FOREACH_TUPLE((4) (5) (6), FNi, a) - const char *str = LM_STR_(LM_FOREACH_TUPLE((1) (2) (3), FNo)); + #define FNo(a) LM_FOREACH_TUPLE((1) (2) (3), FNi, a) + const char *str = LM_STR_(LM_FOREACH_TUPLE((a) (b) (c), FNo)); #undef FNo #undef FNi - test_assert(strcmp(str, "14 15 16 24 25 26 34 35 36") == 0); + test_assert(strcmp(str, "a1 a2 a3 b1 b2 b3 c1 c2 c3") == 0); } /* Test that it works with the documented limit of tuples. */ |