Macro
From Free net encyclopedia
- For other uses, see Macro (disambiguation).
A macro in computer science is an abstraction, whereby a certain textual pattern is replaced according to a defined set of rules. The interpreter or compiler automatically replaces the pattern when it is encountered. In compiled languages, macro-expansion always happens at compile-time. The tool which performs the expansion is sometimes called a macro-expander. The term macro is used in many similar contexts which are derived from the concept of macro-expansion, including keyboard macros and macro languages. In most situations, the use of the word "macro" implies expanding a small command or action into a larger set of instructions.
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 the program text itself, 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.
Being able to choose the order of evaluation (see lazy evaluation and non-strict functions) enables the creation of new syntactic constructs (e.g. control structures) 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.
MacroML has typed syntax macros, and one productive way to think of these syntax macros is as a multi-stage computation.
Contents |
Programming macros
When programming macros in an unfamiliar macro language, it may be helpful to record a macro to do what the user wants it to do, and then open the macro file and try to gain an understanding of how the command structure works. It is then possible to modify the commands to fine-tune it. Some macro languages, such as Great Plains accounting software's Dexterity runtime engine, lack the ability to import data from another data source, such as a comma delimited text file. This limitation can be overcome by creating a computer program in a more powerful programming language, such as VBA, to generate a specialized macro in the weaker programming language. For instance, a Microsoft Excel macro can be programmed to read data from a spreadsheet or text file and create a Great Plains .mac file designed to enter that specific data into Great Plains. A new .mac file would be needed to be generated for each new set of data.
Keyboard macros
Keyboard macros and editor macros are used interactively on a graphical user interface and text editor, respectively. These allow short sequences of keystrokes to substitute long sequences of commands, and can provide a simple form of automation for repetitive tasks.
Macro languages
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:
- C preprocessor
- Internet Macros (iOpus)
- M4 (mentioned previously, originally from AT&T, bundled with Unix)
- TRAC
- GPM
- PHP
- SMX
- Stage 2
- Scripting languages such as Tcl and ECMAScript (a.k.a. ActionScript, DMDScript, JavaScript, JScript) have been embedded in applications so that they behave like macro languages.
- TeX
- ml/1
- Visual Basic Scripting (Microsoft)
- Lisp has a powerful system of unhygienic syntax macros.
- Scheme has hygienic syntax macros with a macros by example system, and is probably the most advanced untyped macro system in common use.
- MacroML is an experimental system that seeks to reconcile static typing and macro systems.
- The General Purpose Macro Processor is a contextual pattern matching macro processor, which could be described as a combination of regular expressions, EBNF and awk
- MacroScript (Macro Scheduler)
Visual Basic for Applications and macro viruses
Visual Basic for Applications (VBA) is a programming language included in Microsoft Office and some other applications. It is not, by the above definition, a macro language at all. However, its function has evolved from and replaced the macro languages which were originally included in some of these applications so it is popularly and mistakenly called a macro language.
VBA has access to most operating system functions and supports automatic execution when a document is opened. This makes it relatively easy to write computer viruses in VBA, commonly known as macro viruses. In the mid-to-late 1990s, this became one of the most common types of computer virus. Some applications with macro languages, such as OpenOffice.org, deliberately exclude certain functionality (eg: automatic execution) to avoid such susceptibility. However, such features (in particular, automatic execution) remain popular in those applications that include them.
See also
es:Macro fr:Macro-définition it:Macro nl:Macro (software) ja:マクロ (コンピュータ用語) pl:Makro pt:Macro ru:Макрос sv:Makro uk:Макрос zh:宏