m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: very basic question incr


From: Eric Blake
Subject: Re: very basic question incr
Date: Wed, 11 Jan 2012 09:58:06 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111222 Thunderbird/9.0

On 01/11/2012 09:36 AM, jfw wrote:
> 
> I've been programming for about 45 years.  I've been programming about 2 days
> with M4.
> 
> I want to increment or double a "variable" every time I call a macro.
> A paper "Exploiting the m4 Macro Language" by Kenneth J. Turner available on
> the web suggests:
> 
> 
> define(‘Count’,‘incr(Count)’)

That looks like the paper you are reading has used a different
typography than what m4 expects for its default quoting characters.  Are
you sure you are using backtick/apostrophe (ASCII values 0x60/0x39) and
not the Unicode single-quote characters in your actual script?  Or are
you using changequote() to select alternate quotes?

But given your description of your stack overflow, I assume you are
using the correct quoting in your script.  Rather, your problem is one
of usage.
> 
> then invoking it with
> Count

It sounds like you _wanted_ to re-define the variable Count to be the
value obtained by incrementing the previous contents of Count.  But that
is written:

define(`Count', incr(Count))

Notice that the second argument is _not_ quoted - I want incr(Count) to
be expanded and its _result_ used as an argument to define().  But in
what you wrote, you ended up defining Count to the literal string
"incr(Count)" rather than a numeric value; therefore, when you later
typed Count to cause it to be macro expanded, you triggered an infinite
expansion: Count expands to incr(Count) expands to incr(incr(Count)),
etc., until you have stack overflow.

> 
> produces m4: stack overflow
> 
> using:
> 
> define(‘Count’,incr(`Count'))
> Count produces m4: non-numeric to builtin`incr'

Now you are really worrying me - you've mixed two different quoting
styles in the same define() line, which makes me suspect that you really
aren't using the right bytes consistently.  But the warning is correct:
here, you are attempting to call incr with a single argument of the
literal string "Count", when you really meant to be calling it with a
single argument which is the number produced by the expansion of the
pre-existing macro Count.  That is, you want incr(Count) and not
incr(`Count').

> 
> using:
> define(`count',0)
> define(`count',`incr(count)')
> count
> also produces stack overflow
> 
> I would appreciate being told the simple idiom I am trying to write.  Once I
> understand incr() I assume I could use eval(x *2) to produce doubling

Indeed:
define(`count', `0')
define(`count', expr(count * 2))

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]