Re: very basic question incr

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat
Message-ID <[email protected]>
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   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc (application/pgp-signature, 620 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBCAAGBQJPDb+eAAoJEKeha0olJ0NqLdkH/0IoPXjCl+bjaFRHwDsH9IGo
EIe8bgWL5edDSvkAzRn1zvsaUchF5p7QJIapbL+YifwoIsAQrEJ/MSI1U7sAHzXC
dnSAX4whj1LREnCPjw6gEF2f7Uks9VI7/7UzlmEf3V9H4YUds8MA/gjbbuzmJizl
om90i9UZGZUmlT44SnOXIQp9onjm7qRQmsUMrjB0lY/3wjwRhEXnWAMa0hNkbsaP
ONKSjA1C/5eZQJBbgInTl4paonobGdr5pF4D4d1uetgcNJpyS4BD717EFv2Ig5kA
OhVFvRWrfpmDGgn/hBDXRK2Bw8rAXWaofOXnNXbMxtsaWU3h3CiF6mku+cn0d2A=
=hBqr
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.