![]() |
|
|
|
#11 |
|
Andrey Vul wrote:
> > Any help with regards to the details of CPP (without getting into > platform-specifics)? I.e. how to structure defines so that #define a b > #define b c doesn't turn to #define a c , etc. ? Your wish is granted: The preprocessor already operates in the way you desire. Despite the second #define, the first remains unchanged: The expansion of `a' is still `b', and not `c'. Here's a snippet that illustrates the point: int a = 1; int b = 2; int c = 3; #define a b #define b c printf ("a = %d\n", a); #undef b #define b 4 printf ("a = %d\n", a); #undef b printf ("a = %d\n", a); #undef a printf ("a = %d\n", a); If after pondering this you still don't understand why it produces the output it does, I *strongly* suggest you not try any fancy tricks with the preprocessor. *Strongly.* -- Eric Sosman lid Eric Sosman |
|
|
|
|
#12 |
|
Posts: n/a
|
On Oct 28, 9:28*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> Andrey Vul wrote: > > > Any help with regards to the details of CPP (without getting into > > platform-specifics)? I.e. how to structure defines so that #define a b > > #define b c doesn't turn to #define a c , etc. ? > > * * *Your wish is granted: The preprocessor already operates > in the way you desire. *Despite the second #define, the first > remains unchanged: The expansion of `a' is still `b', and not > `c'. *Here's a snippet that illustrates the point: > > * * * * int a = 1; > * * * * int b = 2; > * * * * int c = 3; > * * * * #define a b > * * * * #define b c > * * * * printf ("a = %d\n", a); > * * * * #undef b > * * * * #define b 4 > * * * * printf ("a = %d\n", a); > * * * * #undef b > * * * * printf ("a = %d\n", a); > * * * * #undef a > * * * * printf ("a = %d\n", a); > > * * *If after pondering this you still don't understand why it > produces the output it does, I *strongly* suggest you not try > any fancy tricks with the preprocessor. **Strongly.* My compiler begs to differ: int a = 1; int b = 2; int c = 3; #define a b #define b c printf ("a = %d\n", a); #undef b #define b 4 printf ("a = %d\n", a); #undef b printf ("a = %d\n", a); #undef a printf ("a = %d\n", a); Based on the output, it looks like the expansion of b overloads the expansion for a until b is undefined. It looks like cpp uses a list to store define chains: #define a b #define b c => a => (b is expandable ? c : b) I'm asking if there's a way to force cpp to _not_ linklist #define chains. Or is a lookup pseudo-table mandatory to avoid chain and recursion complexities? Andrey Vul |
|
|
|
#13 |
|
Posts: n/a
|
On 2009-10-29, Andrey Vul <> wrote:
> Based on the output, it looks like the expansion of b overloads the > expansion for a until b is undefined. Nope. > It looks like cpp uses a list to store define chains: Nope. > #define a b > #define b c >=> a => (b is expandable ? c : b) Nope. > I'm asking if there's a way to force cpp to _not_ linklist #define > chains. Or is a lookup pseudo-table mandatory to avoid chain and > recursion complexities? It's not a "linklist". It doesn't know about any chains or anything. When expansion completes, the results are expanded. No linked list. And really: If you want C++, you know where to get it. The preprocessor is not the right tool for this. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet- http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Seebs |
|
|
|
#14 |
|
Posts: n/a
|
On Oct 28, 9:49*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-10-29, Andrey Vul <andrey....@gmail.com> wrote: > > > Based on the output, it looks like the expansion of b overloads the > > expansion for a until b is undefined. > > Nope. > > > It looks like cpp uses a list to store define chains: > > Nope. > > > #define a b > > #define b c > >=> a => (b is expandable ? c : b) > > Nope. > > > I'm asking if there's a way to force cpp to _not_ linklist #define > > chains. Or is a lookup pseudo-table mandatory to avoid chain and > > recursion complexities? > > It's not a "linklist". *It doesn't know about any chains or anything. > When expansion completes, the results are expanded. *No linked list. I meant a linked list (in the implementation of cpp) that stores the expansion chain. I should state what I'm trying to do: descend a staircase of macros without having full expansion I.e. #define a a_1 #define a_1 a_2 .... #define a_N a_n without the staircase effect of a=>a_1=>...=>a_N=>a_n . If that's even possible in cpp, unless you have a complex alternate staircase that solves the problem of expansion, which feels like a headache to implement. Andrey Vul |
|
|
|
#15 |
|
Posts: n/a
|
On Oct 28, 5:55*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-10-28, Andrey Vul <andrey....@gmail.com> wrote: > > > Is there a way to do this: > > cat << "EOF" | cpp -Dn=2 - > > #define d define > > d foo#(n-1) (n-1) > > EOF > > > with the output being > > #define foo1 1 > > No. > > But you could easily do this with pure shell, or m4, or even a C program > which prints C code as its output. > There's an easy way to do it, and an interesting way to do it. Guess what I chose Andrey Vul |
|
|
|
#16 |
|
Posts: n/a
|
On 2009-10-29, Andrey Vul <> wrote:
> I meant a linked list (in the implementation of cpp) that stores the > expansion chain. I guessed this. But that's not actually what happens, as I explained. > I should state what I'm trying to do: > descend a staircase of macros without having full expansion > I.e. > #define a a_1 > #define a_1 a_2 > ... > #define a_N a_n > without the staircase effect of a=>a_1=>...=>a_N=>a_n . > If that's even possible in cpp, unless you have a complex alternate > staircase that solves the problem of expansion, which feels like a > headache to implement. No, it's not possible. I can't even imagine why you'd want it. Could you maybe explain what your ultimate goal is? Can you show some kind of actual use case where you want a thing like this and it would be useful? I can't figure out what you'd want with this. What good would it do you to be able to write "a" and get "a_1", but to write "a_1" and get "a_2", but not get "a_2" from a? -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet- http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Seebs |
|
|
|
#17 |
|
Posts: n/a
|
On 2009-10-29, Andrey Vul <> wrote:
> There's an easy way to do it, and an interesting way to do it. Guess > what I chose The one that actually doesn't work? Unless you're just messing around to see what you can do, generally you should do things the easy way. It lets you get on to the interesting stuff you were trying to accomplish instead of leaving you stuck wrestling with using the wrong tool. Under some circumstances, the back end of a claw hammer can be used as a screwdriver for certain types of screws. That's much more INTERESTING than using a plain screwdriver, but it's not a good choice. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet- http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! Seebs |
|
|
|
#18 |
|
Posts: n/a
|
Andrey Vul wrote:
> On Oct 28, 9:28 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: >> Andrey Vul wrote: >> >>> Any help with regards to the details of CPP (without getting into >>> platform-specifics)? I.e. how to structure defines so that #define a b >>> #define b c doesn't turn to #define a c , etc. ? >> Your wish is granted: The preprocessor already operates >> in the way you desire. Despite the second #define, the first >> remains unchanged: The expansion of `a' is still `b', and not >> `c'. Here's a snippet that illustrates the point: >> >> int a = 1; >> int b = 2; >> int c = 3; >> #define a b >> #define b c >> printf ("a = %d\n", a); >> #undef b >> #define b 4 >> printf ("a = %d\n", a); >> #undef b >> printf ("a = %d\n", a); >> #undef a >> printf ("a = %d\n", a); >> >> If after pondering this you still don't understand why it >> produces the output it does, I *strongly* suggest you not try >> any fancy tricks with the preprocessor. *Strongly.* > > My compiler begs to differ: > > int a = 1; > int b = 2; > int c = 3; > #define a b > #define b c > printf ("a = %d\n", a); > #undef b > #define b 4 > printf ("a = %d\n", a); > #undef b > printf ("a = %d\n", a); > #undef a > printf ("a = %d\n", a); > > Based on the output, it looks like the expansion of b overloads the > expansion for a until b is undefined. You've misunderstood. There is no "overloading" going on, and `a' is not #define'd as `c'. I repeat my suggestion that you refrain from attempting fancy tricks with the preprocessor until you understand the tool you are trying to use. At the moment, your mental model of the preprocessor appears to approximate reality somewhat inexactly. > It looks like cpp uses a list to store define chains: > #define a b > #define b c > => a => (b is expandable ? c : b) No. See 6.10.3.4. > I'm asking if there's a way to force cpp to _not_ linklist #define > chains. Yes: Just use it as it stands; there are no chains or lists of definitions. See 6.10.3.4. > Or is a lookup pseudo-table mandatory to avoid chain and > recursion complexities? Can't comment; I don't know what a "pseudo-table" is. -- Eric Sosman lid Eric Sosman |
|
|
|
#19 |
|
Posts: n/a
|
On Oct 28, 10:27*pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-10-29, Andrey Vul <andrey....@gmail.com> wrote: > > > I meant a linked list (in the implementation of cpp) that stores the > > expansion chain. > > I guessed this. *But that's not actually what happens, as I explained. > > > I should state what I'm trying to do: > > descend a staircase of macros without having full expansion > > I.e. > > #define a a_1 > > #define a_1 a_2 > > ... > > #define a_N a_n > > without the staircase effect of a=>a_1=>...=>a_N=>a_n . > > If that's even possible in cpp, unless you have a complex alternate > > staircase that solves the problem of expansion, which feels like a > > headache to implement. > > No, it's not possible. *I can't even imagine why you'd want it. *Could > you maybe explain what your ultimate goal is? *Can you show some kind of > actual use case where you want a thing like this and it would be useful? > I can't figure out what you'd want with this. *What good would it do > you to be able to write "a" and get "a_1", but to write "a_1" and get "a_2", > but not get "a_2" from a? > Use the defined decrement macros in a cpp-fibonacci to decrement the previous macros. Here's my code so far: Note: there a script that generates for n=5,8,11,etc. That output file is specified via -Di=... #ifdef IDEF #define I #ifndef #define D #define #define E #endif #endif #ifdef i #include #i #endif #define _C(x,y) x##y #define C(x,y) _C(x,y) #define F(x) C(F,x) #ifndef R I F(0) I F(1) I F(2) D F(0) 1 D F(1) 1 D F(2) 1 E E E I F(n_2) D F(n_2) (F(n_3)+F(n_4)) E I F(n_1) D F(n_1) (F(n_2)+F(n_3)) E E I F(n) D F(n) (F(n_1)+F(n_2)) E #endif #if (n>=1) F(n) #define R /* How do I decrement n here */ #include __FILE__ #endif Andrey Vul |
|
|
|
#20 |
|
Posts: n/a
|
On Oct 28, 10:42*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> Andrey Vul wrote: > > > It looks like cpp uses a list to store define chains: > > #define a b > > #define b c > > => a => (b is expandable ? c : b) > > * * *No. *See 6.10.3.4. > > > I'm asking if there's a way to force cpp to _not_ linklist #define > > chains. > > * * *Yes: Just use it as it stands; there are no chains or lists > of definitions. *See 6.10.3.4. Multi-pass somehow registers with me as linked lists. My bad. But 6.10.3.4 is what I'm trying to subvert. Andrey Vul |
|