Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually[citation needed], or it gives programs greater flexibility to efficiently handle new situations without recompilation.

The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity.

Reflection is a valuable language feature to facilitate metaprogramming. Having the programming language itself as a first-class data type (as in Lisp, Forth or Rebol) is also very useful. Generic programming invokes a metaprogramming facility within a language, in those languages supporting it.

Metaprogramming usually works in one of three ways[citation needed]. The first way is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs). The second approach is dynamic execution of string expressions that contain programming commands. Thus, "programs can write programs". Although both approaches can be used in the same language, most languages tend to lean toward one or the other.

The third way is to step outside the language entirely. General purpose program transformation systems, which accept language descriptions and can carry out arbitrary transformations on those languages, are direct implementations of general metaprogramming. This allows metaprogramming to be applied to virtually any target language without regard to whether that target language has any metaprogramming abilities of its own.



In statically typed functional languages

Template meta-programming

  • C++ Templates

Staged meta-programming

Macro systems

IBM/360 assembler

The IBM/360 and derivatives had powerful Assembler macro facilities that were often used to generate complete programs[citation needed] or sections of programs (for different operating systems for instance). Macros provided with CICS transaction processing system had Assembler macros that generated COBOL statements as a pre-processing step.


A simple example of a metaprogram is this bash script, which is an example of generative programming:

# metaprogram
echo '#!/bin/bash' >program
for ((I=1; I<=992; I++)) do
    echo "echo $I" >>program
chmod +x program

This script (or program) generates a new 993-line program that prints out the numbers 1–992. This is only an illustration of how to use code to write more code; it is not the most efficient way to print out a list of numbers. Nonetheless, a programmer can write and execute this metaprogram in just a couple of minutes, and will have generated exactly 1000 lines of code in that amount of time.

A quine is a special kind of metaprogram that produces its own source code as its output.

Not all metaprogramming involves generative programming. If programs are modifiable at runtime or if an incremental compilation is available (such as in C#, Forth, Frink, Groovy, JavaScript, Lisp, Lua, Perl, PHP, Python, REBOL, Ruby, Smalltalk, and Tcl), then techniques can be used to perform metaprogramming without actually generating source code.

Lisp is probably the quintessential language with metaprogramming facilities, both because of its historical precedence and because of the simplicity and power of its metaprogramming. In Lisp metaprogramming, the quasiquote operator (typically a comma) introduces code that is evaluated at program definition time rather than at run time. The metaprogramming language is thus identical to the host programming language, and existing Lisp routines can be directly reused for metaprogramming, if desired.

This approach has been implemented in other languages by incorporating an interpreter in the program, which works directly with the program’s data. There are implementations of this kind for some common high-level languages, such as RemObject’s Pascal Script for Object Pascal.

One style of metaprogramming is to employ domain-specific programming languages (DSLs). A fairly common example of using DSLs involves generative metaprogramming: lex and yacc, two tools used to generate lexical analyzers and parsers, let the user describe the language using regular expressions and context-free grammars, and embed the complex algorithms required to efficiently parse the language.


See also

External links

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • metaprogramming — noun Writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at runtime that would otherwise be done at compile time. See Also: metaprogram …   Wiktionary

  • Metaprogramming (management) — This article is about the management technique. For the programming technique, see metaprogramming. Metaprogramming is the name of a management technique invented by Charles Simonyi (who also developed Hungarian notation) for organizing the work… …   Wikipedia

  • Template metaprogramming — is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates include compile time… …   Wikipedia

  • List of programming languages by category — Programming language lists Alphabetical Categorical Chronological Generational This is a list of programming languages grouped by category. Some languages are listed in multiple categories. Contents …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • Multi-paradigm programming language — A multi paradigm programming language is a programming language that supports more than one programming paradigm. As Leda designer Tim Budd holds it: The idea of a multiparadigm language is to provide a framework in which programmers can work in… …   Wikipedia

  • Nemerle — Paradigm(s) multi paradigm: metaprogramming, functional, object oriented, imperative Appeared in 2003 Designed by Kamil Skalski, Michał Moskal, Prof. Leszek Pacholski and Paweł Olszta at Wrocław University Stable release …   Wikipedia

  • Comparison of Java and C++ — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • D (programming language) — For other programming languages named D, see D (disambiguation)#Computing. D programming language Paradigm(s) multi paradigm: imperative, object oriented, functional, meta Appeared in 1999 (1999) Designed by …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”