The purpose of macros is to either automate frequently-used sequences or enable a more powerful abstraction -- but these are often the same thing.
Languages such as C and assembly language have simple macro systems, implemented as preprocessors to the compiler or assembler. C preprocessor macros work by simple textual search-and-replace. More elaborate macros are available to C programmers by using an additional text-processing language such as M4.
Lisp languages such as Common Lisp and Scheme have more elaborate macro systems: In Lisp, macros behave like functions transforming program text, with the full language available to express such transformations.
Whereas a C macro may define a simple replacement of one piece of syntax with another, a Lisp macro can control the evaluation of sections of code, creating new syntactic constructs indistinguishable from those built into the language. For instance, in a Lisp dialect that has cond
but lacks if
, it is possible to define the latter in terms of the former using macros. Entire major extensions to Lisp syntax, such as the CLOS system for object-oriented programming, have been defined as macros.
Table of contents |
2 Macro languages 3 Microsoft Word and macro viruses |
Keyboard macros and editor macros are used interactively on a graphical user interface and text editor, respectively. These allow short sequences of keystrokes substitute for long sequences of commands, and can provide a simple form of automation for repetitive tasks.
The programmers' text editor Emacs (short for "editing macros") follows this idea to a conclusion. In effect, most of the editor is made of macros. Emacs was originally devised as a set of macros in the editing language TECO; it was later ported to dialects of Lisp.
A macro language is a programming language in which all or most computation is done by expanding macros. Macro languages are not widely used for general-purpose programming, but are common in text processing applications. Examples include M4 (mentioned previously) and TeX.
The macro programming language in Microsoft Word has access to many operating system functions, and supports automatic execution of macros when a document is opened. It is possible to write computer viruses in this language. In the mid-to-late 1990s so-called macro viruses became one of the most common types of computer virus.Keyboard macros
Macro languages
Microsoft Word and macro viruses