Nestable strings
Written , a minute read
m4 macro processor has a nifty little feature of nestable strings.
It doesn't use the traditional "
character to create string literals - instead it uses a pair of `
and '
.
C, Python | "hello, world" |
---|---|
m4 | `hello, world' |
This allows for easy nesting of string literals - instead of "Program prints \"hello, world\""
in Python, in m4 you write `Program prints `hello, world''
.
In m4 this is particularly useful to prevent macros referencing themselves:
dnl references itself and doesn't terminate:
define(`hello', `hello, world')
dnl terminates thanks to nested quotation:
define(`hello', ``hello', world')
(dnl
is a syntax for line comments in m4)
Of course this „feature” is a present in natural languages.
As seen in Quotation mark article all natural languages follow the convention of different opening and closing quotation marks.
One could argue that we moved from ASCII a long time ago, however we can always use LaTeX convention of ,,
and ''
for ASCII compatible syntax.
Other nice m4 features
Math with eval
can be done in almost arbitrary bases: 0r[base]:[number]
allow for numbers in base from 1 to 36 and second argument of eval
allows choosing the base of the output.
del some hex math
eval(`0xfa + 1', `16')
del and using the arbitrary base syntax:
eval(`0r16:fa + 1', `16')
m4 doesn't support loops so iteration is commonly expressed via recursion:
del macro turns arguments into Haskell list
define(`list', `ifelse(`$#', `0', `[]', `$1', `', `[]', ``$1':list(shift($@))')')
list(1,2,3)
1:2:3:[]
list(1)
1:[]
list()
[]
I recommend this excellent article from Michael Breen on m4 if you are interested in learning more. It's a simple Unix-style program so there isn't much to learn about it's workings.