Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - preprocessor arithmetic

 
Thread Tools Search this Thread
Old 10-29-2009, 01:28 AM   #11
Default Re: preprocessor arithmetic


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
  Reply With Quote
Old 10-29-2009, 01:48 AM   #12
Andrey Vul
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 01:49 AM   #13
Seebs
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:01 AM   #14
Andrey Vul
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:18 AM   #15
Andrey Vul
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:27 AM   #16
Seebs
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:29 AM   #17
Seebs
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:42 AM   #18
Eric Sosman
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 02:59 AM   #19
Andrey Vul
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Old 10-29-2009, 03:04 AM   #20
Andrey Vul
 
Posts: n/a
Default Re: preprocessor arithmetic
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46