Generated by DeepSeek V3.2| Macro (computer science) | |
|---|---|
| Name | Macro |
| Paradigm | Metaprogramming |
| Influenced by | Assembly language, Lisp (programming language) |
| Influenced | C (programming language), C++, Rust (programming language), TeX |
Macro (computer science). In computer science, a macro is a rule or pattern that specifies how a certain input sequence should be mapped to a replacement output sequence according to a defined procedure. This process, known as macro expansion, is a fundamental form of metaprogramming that allows programmers to extend the syntax of a programming language or automate repetitive coding tasks. Macros are processed by a macro processor, which is often a component of an assembler, compiler, or text editor.
A macro is essentially a fragment of code that has been given a name; whenever that name is used in a program, it is replaced by the contents of the macro. This concept is central to many programming paradigms and is implemented across various levels of software development, from low-level system software to high-level application software. The processing can occur during different phases, such as compile time in languages like C (programming language) or during a distinct preprocessing stage in assembly language programming. The American National Standards Institute has standardized macro processing in contexts like the C preprocessor.
Macros are broadly categorized by their functionality and the time at which they are expanded. Object-like macros, commonly found in the C preprocessor, perform simple textual substitution, replacing an identifier with a defined code block or value. Function-like macros take arguments and can generate code that varies based on input, mimicking the behavior of functions but through textual replacement. More advanced systems, such as those in Lisp (programming language) dialects like Common Lisp and Scheme (programming language), feature hygienic macros and syntactic macros that operate on the abstract syntax tree, allowing for powerful and safe language extensions. Template metaprogramming in C++ also represents a macro-like system for generic programming.
Macro expansion is typically handled by a dedicated macro processor. In languages like C (programming language), the GNU Compiler Collection uses the C preprocessor (cpp) to handle directives like `#define` before the compiler proper analyzes the code. In assembly language, an assembler like Microsoft Macro Assembler (MASM) expands macros during assembly. The expansion algorithm often involves multiple passes to resolve nested or recursive macro calls. For Lisp (programming language) macros, the process is integrated into the interpreter (computing) or compiler's evaluation phase, transforming S-expressions before execution.
Macros are extensively used for code generation, allowing developers to avoid boilerplate code and enforce coding conventions. In systems programming, they are crucial for creating hardware abstraction layers and defining memory-mapped I/O addresses. Domain-specific languages often rely on macro systems for embedding within general-purpose languages, as seen with Racket (programming language). Historically, macros were vital in operating system development, such as in the Multics project and early Unix kernels. They also play a key role in text formatting systems like TeX and GNU troff.
The primary advantage of macros is their ability to provide abstraction (computer science) and reduce source code duplication, which can minimize errors and improve software maintenance. They can also be used to implement features not natively supported by a language, effectively creating language extensions. However, macros can lead to obfuscated code and make debugging difficult because the compiler or debugger operates on the expanded code, not the original macro definitions. C preprocessor macros, in particular, are notorious for causing subtle bugs due to their lack of type safety and the potential for unintended multiple evaluation of arguments.
The concept of macros originated in assembly language during the 1950s, with early implementations in assemblers for machines like the IBM 704. A significant advancement was Douglas McIlroy's contribution to macro processors while at Bell Labs. The development of Lisp (programming language) in the late 1950s by John McCarthy introduced macros that manipulated linked list structures, profoundly influencing later languages. The C (programming language), created at Bell Labs by Dennis Ritchie, incorporated a powerful macro preprocessor. Modern languages like Rust (programming language) and Julia (programming language) have continued to evolve macro systems, emphasizing hygienic macros and better integration with module systems. Category:Metaprogramming Category:Programming constructs