Category:M4: Difference between revisions

From Rosetta Code
Content added Content deleted
(m4 ; should a GNU m4 page be created for the implementation?)
 
m (correction, changequote() for the quote chars, not changecom())
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{language|M4}}{{codepad}}
{{language
|exec=interpreted
`m4' is a macro processor, in the sense that it copies its input to the
|safety=safe
output, expanding macros as it goes. Macros are either builtin or
|untyped=yes
user-defined, and can take any number of arguments. Besides just doing
|tag=m4
macro expansion, `m4' has builtin functions for including named files,
}}
running shell commands, doing integer arithmetic, manipulating text in
'''m4''' reads text input, expands macros in the text, and prints the result. It can be used as a front-end to a compiler or assembler, or for general purpose expanding etc.
various ways, performing recursion, etc.... `m4' can be used either as
a front-end to a compiler, or as a macro processor in its own right.


Various builtin macros can do integer arithmetic, run shell commands, access files, divert output to temporary buffers for re-ordering, etc. <code>define</code> creates new macros.
(Excerpt from the GNU info page of m4)

<lang m4>define(`foo', `blah blah: $1')
foo(123)
=>
blah blah: 123</lang>

Control flow is limited to <code>ifelse</code>, but it's easy to construct loops by recursion. GNU m4 includes examples of macros implementing various general-purpose loops.

Quoting data values against premature or unwanted expansion can be a little tricky. The default quote characters are <code>`</code> and <code>'</code>. If they would occur in text too often then <code>changequote()</code> can set something else. Autoconf changes to <code>[</code> and <code>]</code> since <code>`</code> and <code>'</code> occur often in its [[Bourne Shell]] output.

When a macro expands, its value (with <code>$1</code> etc parameters substituted) is re-read as input. This is how macro definitions can contain further macros to expand.

<lang m4>define(`foo', `bar(`$1',x,`$2')')</lang>

Various m4 implementations, including BSD, have a fixed limit on the amount "push-back" text to be re-read. GNU m4 has no limit except available memory. A limit restricts the size of macro values and the data they might operate on. Cutting data into pieces can keep expansions to a reasonable size.

The simple text re-reading means that macro calls are "properly tail recursive". If an expansion ends with another macro call then that call can re-expand recursively or by co-routining endlessly. But a tail call must be the very last thing, no newline or other fixed text after. See [[Factorial#M4|Factorial]] for an example of such recursion.


One implementation of this Unix macro processor m4 is the GNU m4
One implementation of this Unix macro processor m4 is the GNU m4


* [[wp:m4 (computer language)|m4 on wikipedia]]
* [http://www.gnu.org/software/m4/ GNU m4]
* [http://www.gnu.org/software/m4/ GNU m4]

Latest revision as of 00:42, 30 September 2013

Language
M4
This programming language may be used to instruct a computer to perform a task.
Execution method: Interpreted
Type safety: Safe
Typing: Untyped
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using M4.

m4 reads text input, expands macros in the text, and prints the result. It can be used as a front-end to a compiler or assembler, or for general purpose expanding etc.

Various builtin macros can do integer arithmetic, run shell commands, access files, divert output to temporary buffers for re-ordering, etc. define creates new macros.

<lang m4>define(`foo', `blah blah: $1') foo(123) => blah blah: 123</lang>

Control flow is limited to ifelse, but it's easy to construct loops by recursion. GNU m4 includes examples of macros implementing various general-purpose loops.

Quoting data values against premature or unwanted expansion can be a little tricky. The default quote characters are ` and '. If they would occur in text too often then changequote() can set something else. Autoconf changes to [ and ] since ` and ' occur often in its Bourne Shell output.

When a macro expands, its value (with $1 etc parameters substituted) is re-read as input. This is how macro definitions can contain further macros to expand.

<lang m4>define(`foo', `bar(`$1',x,`$2')')</lang>

Various m4 implementations, including BSD, have a fixed limit on the amount "push-back" text to be re-read. GNU m4 has no limit except available memory. A limit restricts the size of macro values and the data they might operate on. Cutting data into pieces can keep expansions to a reasonable size.

The simple text re-reading means that macro calls are "properly tail recursive". If an expansion ends with another macro call then that call can re-expand recursively or by co-routining endlessly. But a tail call must be the very last thing, no newline or other fixed text after. See Factorial for an example of such recursion.

One implementation of this Unix macro processor m4 is the GNU m4

Subcategories

This category has the following 4 subcategories, out of 4 total.

Pages in category "M4"

The following 92 pages are in this category, out of 92 total.