From 8dafe3b7a6a6122f845e8ae9249a7ff85b8fe17c Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 4 Jul 2018 17:39:03 +0200 Subject: [PATCH] Add macro M_GET_AT. --- README.md | 14 ++++++++++++++ m-core.h | 9 +++++++++ tests/test-mcore.c | 3 +++ 3 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 1f5df4f3..6fc9d93e 100644 --- a/README.md +++ b/README.md @@ -3920,6 +3920,20 @@ Keep the medium arguments of the argument list, starting from the 'first'-th one and up to 'len' arguments. first and len can be within [0..52]. + +##### M\_GET\_AT(array, index) + +Return the index 'index' of the "array" 'array'. +array in this context is a list of arguments encapsulated with parenthesis, +and is not a true C array. +Return the pre-processing token corresponding to this value (meaning it is evaluated +at macro processing stage, not at compiler stage). + +Example: + + M_GET_AT((f_0,f_1,f_2),1) + + ##### M\_BOOL(cond) Convert an integer or a symbol into 0 (if 0) or 1 (if not 0 or symbol unknown). diff --git a/m-core.h b/m-core.h index b5a5b954..b38c3331 100644 --- a/m-core.h +++ b/m-core.h @@ -622,6 +622,15 @@ #define M_MID(first, len, ...) M_MIDI(M_KEEP,len, M_SKIP(first, __VA_ARGS__)) #define M_MIDI(f, ...) f(__VA_ARGS__) + +/* Return the value of the "array" associated to the given index. + EXAMPLE: M_GET_AT((f_0,f_1,f_2),1) returns f_1 + Can be chained with NARGS(). + */ +#define M_GET_AT(array, index) M_GETI_AT0(M_C(M_RET_ARG,M_INC(index)), array) +#define M_GETI_AT0(func, array) func ( M_ID array , dummy ) + + /* Convert an integer or a symbol into 0 (if 0) or 1 (if not 0). 1 if symbol unknown */ #define M_TOBOOLI_0 1, 0, diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 170066d6..0c63aeae 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -120,6 +120,9 @@ static void test_return(void) assert (M_RET_ARG2(0, 1, 0)); assert (M_RET_ARG3(0, 0, 1, 0)); assert (M_RET_ARG27(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)); + assert (M_GET_AT((0,1,0),1)); + assert (M_GET_AT((1,0,0),0)); + assert (M_GET_AT((0,0,0,1),3)); } static void test_invert(void)