Macro (computer science)
In
Macros are used to make a sequence of computing instructions available to the programmer as a single
Keyboard and mouse macros
Keyboard macros and mouse macros allow short sequences of keystrokes and mouse actions to transform into other, usually more time-consuming, sequences of keystrokes and mouse actions. In this way, frequently used or repetitive sequences of keystrokes and mouse movements can be
During the 1980s, macro programs – originally
Keyboard macros can be used in
Application macros and scripting
Keyboard and mouse macros that are created using an application's built-in macro features are sometimes called application macros. They are created by carrying out the sequence once and letting the application record the actions. An underlying macro programming language, most commonly a scripting language, with direct access to the features of the application may also exist.
The programmers' text editor,
Another programmers' text editor,
Visual Basic for Applications (VBA) is a programming language included in Microsoft Office from Office 97 through Office 2019 (although it was available in some components of Office prior to Office 97). However, its function has evolved from and replaced the macro languages that were originally included in some of these applications.
Macro virus
Parameterized and parameterless macro
A parameterized macro is a macro that is able to insert given objects into its expansion. This gives the macro some of the power of a
As a simple example, in the C programming language, this is a typical macro that is not a parameterized macro, i.e., a parameterless macro:
#define PI 3.14159
This causes PI
to always be replaced with 3.14159
wherever it occurs. An example of a parameterized macro, on the other hand, is this:
#define pred(x) ((x)-1)
What this macro expands to depends on what
pred(2) → ((2) -1) pred(y+2) → ((y+2) -1) pred(f(5)) → ((f(5))-1)
Parameterized macros are a useful source-level mechanism for performing in-line expansion, but in languages such as C where they use simple textual substitution, they have a number of severe disadvantages over other mechanisms for performing in-line expansion, such as inline functions.
The parameterized macros used in languages such as
Text-substitution macros
Languages such as
A classic use of macros is in the computer typesetting system TeX and its derivatives, where most of the functionality is based on macros.
Other examples:
- m4 is a sophisticated stand-alone macro processor.
- TRAC
- Macro Extension TAL, accompanying Template Attribute Language
- SMX: for web pages
- ML/1 (Macro Language One)
- The EBNF and AWK[citation needed]
- troff and nroff: for typesetting and formatting Unix manpages.
- CMS EXEC: for command-line macros and application macros
- EXEC 2 in Conversational Monitor System (CMS): for command-line macros and application macros
- CLIST in IBM's Time Sharing Option (TSO): for command-line macros and application macros
- , TSO
- SCRIPT: for formatting documents
- Various shells for, e.g., Linux
Some major applications have been written as text macro invoked by other applications, e.g., by XEDIT in CMS.
Embeddable languages
Some languages, such as PHP, can be embedded in free-format text, or the source code of other languages. The mechanism by which the code fragments are recognised (for instance, being bracketed by <?php
and ?>
) is similar to a textual macro language, but they are much more powerful, fully featured languages.
Procedural macros
Macros in the
Most assembly languages have less powerful procedural macro facilities, for example allowing a block of code to be repeated N times for
Syntactic macros
Macro systems—such as the C preprocessor described earlier—that work at the level of lexical tokens cannot preserve the lexical structure reliably. Syntactic macro systems work instead at the level of abstract syntax trees, and preserve the lexical structure of the original program. The most widely used implementations of syntactic macro systems are found in Lisp-like languages. These languages are especially suited for this style of macro due to their uniform, parenthesized syntax (known as S-expressions). In particular, uniform syntax makes it easier to determine the invocations of macros. Lisp macros transform the program structure itself, with the full language available to express such transformations. While syntactic macros are often found in Lisp-like languages, they are also available in other languages such as Prolog,[6] Erlang,[7] Dylan,[8] Scala,[9] Nemerle,[10] Rust,[11] Elixir,[12] Nim,[13] Haxe,[14] and Julia.[15] They are also available as third-party extensions to JavaScript[16] and C#.[17]
Early Lisp macros
Before Lisp had macros, it had so-called FEXPRs, function-like operators whose inputs were not the values computed by the arguments but rather the syntactic forms of the arguments, and whose output were values to be used in the computation. In other words, FEXPRs were implemented at the same level as EVAL, and provided a window into the meta-evaluation layer. This was generally found to be a difficult model to reason about effectively.[18]
In 1963, Timothy Hart proposed adding macros to Lisp 1.5 in AI Memo 57: MACRO Definitions for LISP.[19]
Anaphoric macros
An anaphoric macro is a type of programming macro that deliberately captures some form supplied to the macro which may be referred to by an anaphor (an expression referring to another). Anaphoric macros first appeared in Paul Graham's On Lisp and their name is a reference to linguistic anaphora—the use of words as a substitute for preceding words.
Hygienic macros
In the mid-eighties, a number of paperssyntax-rules
, syntax-case
, explicit renaming, and syntactic closures. Both syntax-rules
and syntax-case
have been standardized in the Scheme standards.
Recently, Racket has combined the notions of hygienic macros with a "tower of evaluators", so that the syntactic expansion time of one macro system is the ordinary runtime of another block of code,[22] and showed how to apply interleaved expansion and parsing in a non-parenthesized language.[23]
A number of languages other than Scheme either implement hygienic macros or implement partially hygienic systems. Examples include Scala, Rust, Elixir, Julia, Dylan, Nim, and Nemerle.
Applications
- Evaluation order
- Macro systems have a range of uses. Being able to choose the order of evaluation (see control structures) indistinguishable from those built into the language. For instance, in a Lisp dialect that hasand hygienic macros, which enables a programmer to design their own control abstractions, such as looping and early exit constructs, without the need to build them into the language.
cond
but lacksif
, it is possible to define the latter in terms of the former using macros. For example, Scheme has both continuations - Data sub-languages and domain-specific languages
- Next, macros make it possible to define data languages that are immediately compiled into code, which means that constructs such as state machines can be implemented in a way that is both natural and efficient.[24]
- Binding constructs
- Macros can also be used to introduce new binding constructs. The most well-known example is the transformation of
let
into the application of a function to a set of arguments.
Felleisen conjectures[25] that these three categories make up the primary legitimate uses of macros in such a system. Others have proposed alternative uses of macros, such as anaphoric macros in macro systems that are unhygienic or allow selective unhygienic transformation.
The interaction of macros and other language features has been a productive area of research. For example,
Macros for machine-independent software
Macros are normally used to map a short string (macro invocation) to a longer sequence of instructions. Another, less common, use of macros is to do the reverse: to map a sequence of instructions to a macro string. This was the approach taken by the
Assembly language
While macro instructions can be defined by a programmer for any set of native assembler program instructions, typically macros are associated with macro libraries delivered with the operating system allowing access to operating system functions such as
- peripheral access by access methods(including macros such as OPEN, CLOSE, READ and WRITE)
- operating system functions such as ATTACH, WAIT and POST for subtask creation and synchronization.[28] Typically such macros expand into executable code, e.g., for the EXIT macroinstruction,
- a list of define constant instructions, e.g., for the DCB macro—DTF (Define The File) for DOS[29]—or a combination of code and constants, with the details of the expansion depending on the parameters of the macro instruction (such as a reference to a file and a data area for a READ instruction);
- the executable code often terminated in either a branch and link register instruction to call a routine, or a supervisor callinstruction to call an operating system function directly.
- Generating a Stage 2 job stream for OS/360. Unlike typical macros, sysgen stage 1 macros do not generate data or code to be loaded into storage, but rather use the PUNCH statement to output JCL and associated data.
In older operating systems such as those used on IBM mainframes, full operating system functionality was only available to assembler language programs, not to high level language programs (unless assembly language subroutines were used, of course), as the standard macro instructions did not always have counterparts in routines available to high-level languages.
History
In the mid-1950s, when
By the late 1950s the macro language was followed by the
In 1959, Douglas E. Eastwood and Douglas McIlroy of Bell Labs introduced conditional and recursive macros into the popular SAP assembler,[32] creating what is known as Macro SAP.[33] McIlroy's 1960 paper was seminal in the area of extending any (including high-level) programming languages through macro processors.[34][32]
Macro Assemblers allowed assembly language programmers to implement their own macro-language and allowed limited portability of code between two machines running the same CPU but different operating systems, for example, early versions of MS-DOS and CP/M-86. The macro library would need to be written for each target machine but not the overall assembly language program. Note that more powerful macro assemblers allowed use of conditional assembly constructs in macro instructions that could generate different code on different machines or different operating systems, reducing the need for multiple libraries.[citation needed]
In the 1980s and early 1990s, desktop PCs were only running at a few MHz and assembly language routines were commonly used to speed up programs written in C, Fortran, Pascal and others. These languages, at the time, used different calling conventions. Macros could be used to interface routines written in assembly language to the front end of applications written in almost any language. Again, the basic assembly language code remained the same, only the macro libraries needed to be written for each target language.[citation needed]
In modern operating systems such as Unix and its derivatives, operating system access is provided through subroutines, usually provided by dynamic libraries. High-level languages such as C offer comprehensive access to operating system functions, obviating the need for assembler language programs for such functionality.[citation needed]
See also
- Anaphoric macros
- Assembly language § Macros (the origin of the concept of macros)
- Compound operator
- Extensible programming – programming mechanisms for extending the language, compiler and runtime environment
- Fused operation
- Hygienic macros
- Macro and security
- Programming by demonstration – Technique for teaching a computer or a robot new behaviors
- String interpolation – Replacing placeholders in a string with values
References
- ^ Oxford English Dictionary, s.v. macro, macro-instruction, and macro-
- S2CID 27424222.
One of the important uses of programmer macros is to save time and clerical-type errors in writing sequence of instructions which are often repeated in the course of a program.
- .
- ^ "Runescape: The Massive Online Adventure Game by Jagex Ltd". Retrieved 2008-04-03.
- ^ "scripts: vim online". www.vim.org.
- ^ "Prolog Macros". www.metalevel.at. Retrieved 2021-04-05.
- ^ "Erlang -- Preprocessor". erlang.org. Retrieved 2021-05-24.
- ^ "The Dylan Macro System — Open Dylan". opendylan.org. Retrieved 2021-04-05.
- ^ "Def Macros". Scala Documentation. Retrieved 2021-04-05.
- ^ "About - Nemerle programming language official site". nemerle.org. Retrieved 2021-04-05.
- ^ "Macros - The Rust Programming Language". doc.rust-lang.org. Retrieved 2021-04-05.
- ^ "Macros". elixir-lang.github.com. Retrieved 2021-04-05.
- ^ "macros". nim-lang.org. Retrieved 2021-04-05.
- ^ "Macros". Haxe - The Cross-platform Toolkit.
- ^ "Metaprogramming · The Julia Language". docs.julialang.org. Retrieved 2021-04-05.
- ^ "Sweet.js - Hygienic Macros for JavaScript". www.sweetjs.org.
- ^ "LeMP Home Page · Enhanced C#". ecsharp.net.
- ^ Marshall, Joe. "untitled email". Retrieved May 3, 2012.
- hdl:1721.1/6111. AIM-057.
- ISBN 0897912004.
- ^ [1] Clinger, Rees. "Macros that Work"
- ^ Flatt, Matthew. "Composable and compilable macros: you want it when?" (PDF).
- ^ Rafkind, Jon; Flatt, Matthew. "Honu: Syntactic Extension for Algebraic Notation through Enforestation" (PDF).
- ^ "Automata via Macros". cs.brown.edu.
- ^ [2], Matthias Felleisen, LL1 mailing list posting
- S2CID 8164996.
- S2CID 11733598.
- ^ "University of North Florida" (PDF).
- ^ "DTF (DOS/VSE)". IBM.
- ^ "IBM Knowledge Center". IBM Knowledge Center. 16 August 2013.
- ^ a b "Assembler Language Macro Instructions". Cisco.
- ^ a b Holbrook, Bernard D.; Brown, W. Stanley. "Computing Science Technical Report No. 99 – A History of Computing Research at Bell Laboratories (1937–1975)". Bell Labs. Archived from the original on September 2, 2014. Retrieved February 2, 2020.
- ^ "Macro SAP – Macro compiler modification of SAP". HOPL: Online Historical Encyclopaedia of Programming Languages. Archived from the original on August 13, 2008.
- .