Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Macro (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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 tree]]s, and preserve the lexical structure of the original program. The most widely used implementations of syntactic macro systems are found in [[Lisp (programming language)|Lisp]]-like languages. These languages are especially suited for this style of macro due to their uniform, parenthesized syntax (known as [[S-expression]]s). 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]],<ref>{{Cite web|title=Prolog Macros|url=https://www.metalevel.at/prolog/macros|access-date=2021-04-05|website=www.metalevel.at}}</ref> [[Erlang (programming language)|Erlang]],<ref>{{Cite web|title=Erlang -- Preprocessor|url=https://erlang.org/doc/reference_manual/macros.html#defining-and-using-macros|access-date=2021-05-24|website=erlang.org}}</ref> [[Dylan (programming language)|Dylan]],<ref>{{Cite web|title=The Dylan Macro System — Open Dylan|url=https://opendylan.org/articles/macro-system/index.html|access-date=2021-04-05|website=opendylan.org}}</ref> [[Scala (programming language)|Scala]],<ref>{{Cite web|title=Def Macros|url=https://docs.scala-lang.org/overviews/macros/overview.html|access-date=2021-04-05|website=Scala Documentation}}</ref> [[Nemerle]],<ref>{{Cite web|title=About - Nemerle programming language official site|url=http://nemerle.org/About|access-date=2021-04-05|website=nemerle.org}}</ref> [[Rust (programming language)|Rust]],<ref>{{Cite web|title=Macros - The Rust Programming Language|url=https://doc.rust-lang.org/book/ch19-06-macros.html|access-date=2021-04-05|website=doc.rust-lang.org}}</ref> [[Elixir (programming language)|Elixir]],<ref>{{Cite web|title=Macros|url=https://elixir-lang.org/getting-started/meta/macros.html|access-date=2021-04-05|website=elixir-lang.github.com|language=en}}</ref> [[Nim (programming language)|Nim]],<ref>{{Cite web|title=macros|url=https://nim-lang.org/docs/macros.html|access-date=2021-04-05|website=nim-lang.org}}</ref> [[Haxe]],<ref>{{cite web|url=https://haxe.org/manual/macro.html|title=Macros|website=Haxe - The Cross-platform Toolkit}}</ref> and [[Julia (programming language)|Julia]].<ref>{{Cite web|title=Metaprogramming · The Julia Language|url=https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros|access-date=2021-04-05|website=docs.julialang.org}}</ref> They are also available as third-party extensions to [[JavaScript]]<ref>{{cite web|url=https://www.sweetjs.org/|title=Sweet.js - Hygienic Macros for JavaScript|website=www.sweetjs.org}}</ref> and [[C Sharp (programming language)|C#]].<ref>{{cite web|url=http://ecsharp.net/lemp/|title=LeMP Home Page · Enhanced C#|website=ecsharp.net}}</ref> === Early Lisp macros === Before Lisp had macros, it had so-called [[Fexpr|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.<ref>{{cite web|last=Marshall|first=Joe|title=untitled email|url=http://www.brinckerhoff.org/scraps/joe-marshall-on-FEXPRS-and-DEFMACRO.txt|access-date=May 3, 2012}}</ref> In 1963, Timothy Hart proposed adding macros to Lisp 1.5 in [[AI Memo]] 57: MACRO Definitions for LISP.<ref>{{cite periodical |id=AIM-057 |title=MACRO Definitions for LISP |periodical=AI Memos |date=October 1963|hdl=1721.1/6111 |hdl-access=free |last1=Hart|first1=Timothy P.}}</ref> === Anaphoric macros === {{Main article|Anaphoric macro}} 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 === {{Main article|Hygienic macro}} In the mid-eighties, a number of papers<ref>{{cite conference |last1=Kohlbecker |first1=Eugene |last2=Friedman |first2=Daniel |last3=Felleisen |first3=Matthias |last4=Duba |first4=Bruce |book-title=LFP '86: Proceedings of the 1986 ACM conference on LISP and functional programming |title=Hygienic Macro Expansion |year=1986 |pages=151–161 |doi=10.1145/319838.319859 |isbn=0897912004}}</ref><ref>[http://portal.acm.org/citation.cfm?id=99583.99607] Clinger, Rees. "Macros that Work"</ref> introduced the notion of [[hygienic macro]] expansion (<CODE>syntax-rules</CODE>), a pattern-based system where the syntactic environments of the macro definition and the macro use are distinct, allowing macro definers and users not to worry about inadvertent variable capture (cf. [[referential transparency]]). Hygienic macros have been standardized for Scheme in the [[R5RS]], [[R6RS]], and [[R7RS]] standards. A number of competing implementations of hygienic macros exist such as <code>syntax-rules</code>, <code>syntax-case</code>, explicit renaming, and syntactic closures. Both <code>syntax-rules</code> and <code>syntax-case</code> have been standardized in the Scheme standards. Recently, [[Racket (programming language)|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,<ref>{{cite news | last1 = Flatt | first1 = Matthew | title = Composable and compilable macros: you want it when? |url=http://www.cs.utah.edu/plt/publications/macromod.pdf}}</ref> and showed how to apply interleaved expansion and parsing in a non-parenthesized language.<ref>{{cite news | last1 = Rafkind | first1 = Jon | last2 = Flatt | first2 = Matthew | title = Honu: Syntactic Extension for Algebraic Notation through Enforestation |url=http://www.cs.utah.edu/plt/publications/gpce12-rf.pdf}}</ref> A number of languages other than Scheme either implement hygienic macros or implement partially hygienic systems. Examples include [[Scala (programming language)|Scala]], [[Rust (programming language)|Rust]], [[Elixir (programming language)|Elixir]], [[Julia (programming language)|Julia]], [[Dylan (programming language)|Dylan]], [[Nim (programming language)|Nim]], and [[Nemerle]]. === Applications === ; Evaluation order :Macro systems have a range of uses. Being able to choose the order of evaluation (see [[lazy evaluation]] and [[Strict function|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 <CODE>cond</CODE> but lacks <CODE>if</CODE>, it is possible to define the latter in terms of the former using macros. For example, Scheme has both [[continuation]]s and 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. ; 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 machine]]s can be implemented in a way that is both natural and efficient.<ref>{{cite web|url=http://cs.brown.edu/~sk/Publications/Papers/Published/sk-automata-macros/|title=Automata via Macros|website=cs.brown.edu}}</ref> ; Binding constructs :Macros can also be used to introduce new binding constructs. The most well-known example is the transformation of <CODE>let</CODE> into the application of a function to a set of arguments. [[Matthias Felleisen|Felleisen]] conjectures<ref name="three-uses">[http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg01539.html], Matthias Felleisen, LL1 mailing list posting</ref> 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 macro]]s 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, [[Software component|component]]s and [[Modular programming|module]]s are useful for large-scale programming, but the interaction of macros and these other constructs must be defined for their use together. Module and component-systems that can interact with macros have been proposed for Scheme and other languages with macros. For example, the [[Racket (programming language)|Racket]] language extends the notion of a macro system to a syntactic tower, where macros can be written in languages including macros, using hygiene to ensure that syntactic layers are distinct and allowing modules to export macros to other modules.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)