Metaprogramming
From Wikipedia, the free encyclopedia
- This article is about the programming technique. For the management technique, see metaprogramming (management).
Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at compile time during 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.
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 capacity of a programming language to be its own meta-language is called reflexivity.
A simple example of a metaprogram is this bash script, which is an example of generative programming:
#!/bin/bash # metaprogram echo '#!/bin/bash' >program for ((I=1; I<=992; I++)); do echo "echo $I" >>program done chmod +x program
This script (or program) generates a new 993 line program which prints out the numbers 1–992. This is only an illustration on how to use code to write more code, not the most efficient way to print out a list of numbers. Nonetheless, a good 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.
Not all metaprogramming involves generative programming. If programs are modifiable at runtime (as in Lisp, Python, Smalltalk, Ruby, PHP, Perl, Tcl and other languages), then techniques can be used to accomplish metaprogramming without actually generating source code.
- The most common metaprogramming tool is a compiler which allows a programmer to write a relatively short program in a high-level language and uses it to write an equivalent assembly language or machine language program. This generally saves a good deal of time compared to writing the machine language program directly.
- Another still fairly common example of metaprogramming might be found in the use of lex (see also: flex) and yacc (see also: bison), which are used to generate lexical analysers and parsers.
- A quine is a special kind of metaprogram that has its own source as its output.
- One style of programming which focuses heavily on metaprogramming is called Language Oriented Programming.
Reflection is a valuable language feature for facilitating metaprogramming. Having the programming language itself as a first-class data type (as in Lisp) is also very useful.
[edit] See also
- Self-interpreter
- Template metaprogramming, a C++ compile-time example
- OpenC++
- Recoder
- Code generation
- GCC RDF Introspector
- XL Programming Language
- Self-modifying code
- Partial evaluation
- Ruby on Rails, an open-source web application framework
- Assembly
- Forth
- LISP
- Nemerle
- Interpretive Languages
- REBOL
- Domain-specific programming language
- Maude system, a reflexive language based on rewriting programming
- MetaOCaml, a a multi-stage extension of the OCaml
- Template Haskell, an extension to Haskell 98 for type-safe compile-time meta-programming
[edit] Beware
Metaprogramming has nothing to do with Meta-modeling. These are two different approaches.
[edit] External links
- The Art of Enterprise Metaprogramming
- c2.com Wiki: Metaprogramming article
- Metaprogramming and Free Availability of Sources by François-René Rideau
- MetaL - Metaprogramming language with source code in XML that is used to generate code in several target languages: Java, Perl, PHP, etc.
- DSLEngine - Scheme macro metaprogramming example
- MetaML - A version of ML with homogeneous (same-language) metaprogramming
- MetaOcaml - The language currently in development that descended from MetaML
- RECODER - Framework for source code metaprogramming in Java
- Metaprogramming Weblog - At Lambda the Ultimate
- Moka, a Java-to-Java extensible compiler
- why’s (poignant) guide to ruby - Metaprogramming in Ruby
- The art of metaprogramming, Part 1 - Introduction to metaprogramming, with codes in several languages
- Metaprogramming examples for the Ruby programming language
- David Mertz. A Primer on Python Metaclass Programming. ONLamp. Retrieved on June 28, 2006.