Re: asterisks, dollar signs and percent signs!

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Organization Red Hat
Message-ID <[email protected]>
On 05/01/2012 01:57 PM, Anonymous wrote:
> Hello, I am trying to use m4 to write assembly language macros.
> 
> My first effort is almost working but I can't get the last part to work.

Hello, and thanks for the question.

> 
> Here is what I have so far.
> 
> define(entry,`

Unrelated to your question, but it's generally a good habit to get into
quoting the name of the macro you are defining (so that if you hit the
define() a second time, you redefine the intended macro name, rather
than defining a completely unrelated macro from the expansion of the old
name):

define(`entry',` ...

> 
> 	entry
> 
> 	call printf
> 
> 	exit
> 
> This works as desired except I want to change the macro names to $entry and
> $exit.

By default, m4 only recognizes the pattern '[a-zA-Z_][a-zA-Z_0-9]*' as a
macro name to check for expansion.  If your version of m4 was compiled
with the changeword() builtin (a conscious decision at compile time),
then you can specify a different regex that includes a leading $ as a
candidate macro name.  But be aware that a non-default regex introduces
a speed penalty to everything m4 does.

The eventual m4 2.0 release will solve this problem differently with a
new changesyntax() builtin that lets you choose which syntax category
each character belongs to (so you could set $ into the alphanumeric
category), and doesn't have the speed penalty of m4 1.4.x's
changeword(), but development on m4 has not been very fast.

> I can't figure out how to quote the names or what I am doing wrong,
> it produces no output or produces wrong output and loops depending on
> what I try. I would like to be able to create a $main macro but main: is a
> text token in the definition and I also have problems with that. I looked
> over the manual and I obviously missed something. Thanks for any help and
> sorry if this is a stupid question.

It is also possible to invoke a macro via the indir() builtin, without
having to use changeword() (on its way out) or changesyntax() (not yet
released):

indir(`$entry')

call printf

indir(`$exit')

but that's not the clean syntax you were looking for.  Hope that helps.

-- 
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.12 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBCAAGBQJPoFTQAAoJEKeha0olJ0Nq9PEH/R5j3VyuGUIBl3VUla7nDaCX
W1OEjESIhproFSfJ8bB4bbo4R34lOgIF65SrWCK7ofLl/tc9F+m1d7v0aDYa47YJ
p0VvTpP2lZSsDuR8COp5YfDjAajylM47aexSLoKbSIXTUaOC9EptQO/bQf/GN4YH
Wc86oFrhnAZQuIYIuyGQTJ9hsMp7EpddESZXZZi8UPNj2PEhZChlr/ZY1sWhYjGn
s1s14OWRDTDqhWdto18PLQHSh+xldwhjzwrXKcUGf/F9zu/3odJgr9UUYrQfCVdN
T+8SWh2u/wM6yeTNnnVVigBfuptfOG9ADrFCWdPicg9LRfAiLZgjQNviRrAE1Mk=
=S8fO
-----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.