Jump to content

Macro (computer science): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
edited intro
Line 1: Line 1:
{{Otheruses}}
{{Otheruses}}


A '''macro''' in [[computer science]] is an [[abstraction]], whereby a certain textual [[pattern]] is replaced according to a defined set of rules. The [[Interpreter (computing)|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|keyboard macros]] and [[#Macro languages|macro languages]]. In most situations, the use of the word "macro" implies expanding a small command or action into a larger set of instructions.
A '''macro''' in [[computer science]] is an [[abstraction]], that defines how a certain input [[pattern]] is replaced by an output pattern according to a defined set of rules. There are three broad categories of macros, and each takes a different kind of input pattern to produce a different kind of output pattern.


'''[[#Programming macros|Programming macros]]''' take a [[computer program]] as input and output a new, expanded program. Hence, this process is called ''macro-expansion'' and the tool that does this is called a ''macro-expander''. For many [[programming languages]], a macro expander is included in the [[Interpreter (computing)|interpreter]] or [[compiler]]. These macros are typically used to enable a higher level of abstraction in programming languages.
The purpose of macros is to either [[automate]] frequently-used [[sequence]]s or enable a more powerful abstraction — but these are often the same thing.

'''[[#Keyboard macros|Keyboard macros]]''' and '''[[#Application macros|application macros]]''' take a simple command as input and then output a new, expanded set of commands or instructions. These macros are typically used to [[automate]] frequently-used [[sequence]]s or [[List (computing)|lists]] of keystrokes or mouse movements.


== Programming macros ==
== Programming macros ==

Revision as of 18:37, 10 July 2006

A macro in computer science is an abstraction, that defines how a certain input pattern is replaced by an output pattern according to a defined set of rules. There are three broad categories of macros, and each takes a different kind of input pattern to produce a different kind of output pattern.

Programming macros take a computer program as input and output a new, expanded program. Hence, this process is called macro-expansion and the tool that does this is called a macro-expander. For many programming languages, a macro expander is included in the interpreter or compiler. These macros are typically used to enable a higher level of abstraction in programming languages.

Keyboard macros and application macros take a simple command as input and then output a new, expanded set of commands or instructions. These macros are typically used to automate frequently-used sequences or lists of keystrokes or mouse movements.

Programming macros

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.

Application macros

Application programs can also use an analogous system of macros, which allow the user to specify a series of (oft-repeated) actions as one new procedure. These application macros are generally recorded by carrying out the sequence once and letting the application "remember" the actions. More advanced users can often make use of a built-in macro programming language which has direct access to the features of the application.

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 or definitions allow short sequences of keystrokes to substitute long sequences of commands, and can automate repetitive tasks. They can be created either by using an application's built-in macro features, or by means of a separate macro program.

During the 1980's, macro programs -- originally SmartKey, then Superkey, KeyWorks, Prokey -- were very popular, first as a means to automatically format screenplays, then for a variety of business tasks. Since the 1990's, keyboard macros are built into most popular applications, such as word processors and spreadsheets, but are more difficult to use than the older macro programs, and require knowledge of programming.

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:

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