Consider ## operator. It *is* possible. And you don't really want to do
it...
.
Let's *try* to do it:
#define PUT_BYTE_0 sth
#define PUT_BYTE_1 sth_else
#define PUT_BYTE_EXPANDED(__x, __y) \
((PUT_BYTE_ ## __x)(__y))
#define PUT_BYTE(__x, __y) PUT_BYTE_EXPANDED(__x, __y)
Sometimes you can't. Consider function with parameter in it's name (you
can't use parameter, cause you have no register available) configured at
compile time.
M.
PS. I leave discovering PUT_BYTE_EXPANDED usage reasons as an exercise
.
M.
Hi Marcin!
I've tested it with
/*************/
#include <stdio.h>
#define PUT_BYTE_0(val) printf("0: %d\n", val)
#define PUT_BYTE_1(val) printf("1: %d\n", val)
#define PUT_BYTE(__x, __y) (PUT_BYTE_ ## __x(__y))
int main(void)
{
PUT_BYTE(0, 8);
PUT_BYTE(1, 9);
return 0;
}
/*************/
and it works nicely:
(I don't think we need PUT_BYTE_EXPANDED)
But this is just luck, that it's such a simple example!
How would you do the following:
/************** Example: nice to have *****************/
int regA;
#define MEM_MAP_A ®A;
int regB;
#define MEM_MAP_B ®B;
#define GO(val) \
#preprocessor_META_level_ON \
if (!(val % 3)) \
#machine_code_begin \
*((int *) MEM_MAP_A) = val \
#machine_code_end \
else \
#machine_code_begin \
*((int *) MEM_MAP_B) = val \
#machine_code_end \
#preprocessor_META_level_OFF \
/*
if the preprocessor catches a variable in the meta_level e.g.
{
int my_var;
GO(my_var);
}
then a compiler-error (or rather preprocessor-error) should result!
*/
//In the code I'll have:
{
GO(1);
//...
GO(876);
//...
GO(7987);
//...
GO(3);
//...
}
/************** end *****************/
????????????
It can probably be done, but the header file with it's preprocessor
lines will be huge. Probably just as huge as
/************** Example: bitter reality *****************/
//...
#define GO_1 *((int *) MEM_MAP_B) = 1
#define GO_2 *((int *) MEM_MAP_B) = 2
#define GO_3 *((int *) MEM_MAP_A) = 3
#define GO_4 *((int *) MEM_MAP_B) = 4
#define GO_5 *((int *) MEM_MAP_B) = 5
//...
#define GO_7987 *((int *) MEM_MAP_B) = 7987
//many many lines above!
//In the code I'll have:
{
GO_1;
//...
GO_876;
//...
GO_7987;
//...
GO_3;
//...
}
/************** end *****************/
The reality is that the preprocessor is not turing complete if a
program is preprocessed only once. (Ref:
http://www.ioccc.org/2001/herrmann1.hint)
But a Meta-Level would make it turing complete, right? Refer to:
http://groups.google.de/group/comp.lang.c/msg/4d74e79644f7befd?
-Albert