Metaprogramming
From Wikipedia, the free encyclopedia
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 is otherwise done at run time. 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 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 or Rebol) is also very useful. Generic programming invokes a metaprogramming facility within a language, in those languages supporting it.
Metaprogramming usually works through one of two ways. 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, most languages tend to lean toward one or the other.
[edit] Examples
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 of how to use code to write more code, 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.
Not all metaprogramming involves generative programming. If programs are modifiable at runtime (such as in Lisp, Python, REBOL, Smalltalk, Ruby, PHP, Perl, Tcl, Lua, and JavaScript), then techniques can be used to perform 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 is a fundamental tool for programming since, in most cases, it is vastly impractical to write the machine language program directly.
Another still fairly common example of metaprogramming is the use of lex and yacc, two tools used to generate lexical analyzers and parsers. Yacc is often used as a compiler compiler, a tool to generate a tool for translating high-level language programs to machine language.
A quine is a special kind of metaprogram that produces its own source code as its output.
One style of programming which focuses heavily on metaprogramming is language-oriented programming, which is done via domain-specific programming languages.
[edit] See also
- Source code generation
- Compile-time reflection
- Forth (programming language)
- Lisp (programming language)
- Boo (programming language)
- Nemerle
- Scheme (programming language)
- Interpreted language
- Introspector (program)
- MetaL - Metaprogramming language with source code in XML that is used to generate code in several target languages: Java, Perl, PHP, etc.
- Metaobject
- MetaOCaml, multi-stage extension of OCaml
- Maude system, reflective language based on rewriting programming
- OpenC++
- Partial evaluation
- REBOL
- Recoder
- Self-interpreter
- Self-modifying code
- Template Haskell, extension to Haskell 98 for type-safe compile-time metaprogramming
- Template metaprogramming, a C++ compile-time example
- XL Programming Language
[edit] External links
- The Art of Enterprise Metaprogramming
- c2.com Wiki: Metaprogramming article
- Metaprogramming and Free Availability of Sources by François-René Rideau
- 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
- David Mertz. A Primer on Python Metaclass Programming. ONLamp. Retrieved on June 28, 2006.
- declarative meta programming DMP at Vrije Universiteit Brussel, Université catholique de Louvain and Université Libre de Bruxelles
- meta-programming in REBOL at codeconscious.com
- meta-programming in PROLOG at Bartak prolog overview
- Logtalk meta-programming for most PROLOG dialects
- Mercury Discussion of how Mercury permits meta-programming
- [1] C++ Metaprogramming Tutorial
|