Metaprogramming
Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data.
≈ 3 min read
Metaprogramming. encyclopegia.org, snapshot enwiki-001374069173. /a/metaprogramming
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse, or transform other programs, and even modify itself, while running.
In some cases, this allows programmers to minimize the number of lines of code to express a solution, in turn reducing development time. It also allows programs more flexibility to efficiently handle new situations with no recompiling.
Metaprogramming can be used to move computations from runtime to compile time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage allows reflective programming, and is termed reflection. Reflection is a valuable language feature to facilitate metaprogramming.
Metaprogramming was popular in the 1970s and 1980s using list processing languages such as Lisp. Lisp machine hardware gained some notice in the 1980s, and enabled applications that could process code.
They were often used for artificial intelligence applications. !/bin/sh metaprogram echo '#!/bin/sh' > program for i in $(seq 992) do echo "echo $i" >> program done 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 less than a minute, and will have generated over 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. Quines are generally of recreational or theoretical interest only.
Not all metaprogramming involves generative programming. If programs are modifiable at runtime, or if incremental compiling is available (such as in C#, Forth, Frink, Groovy, JavaScript, Lisp, Elixir, Lua, Nim, Perl, PHP, Python, Rebol, Ruby, Rust, R, SAS, Smalltalk, and Tcl), then techniques can be used to perform metaprogramming without generating source code.
One style of generative approach is to employ domain-specific languages (DSLs). A fairly common example of using DSLs involves generative metaprogramming: lex and yacc, two tools used to generate lexical analysers 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
Aspect weaver Comparison of code generation tools Compile-time function execution Genetic programming Homoiconicity Instruction set simulator Interpreted language Machine learning Metacompiler Metaobject Partial evaluation Reflective programming, also compile time reflection Self-interpreter Self-modifying code Source code generation Transcompiler (also known as transpilation) Very-large-scale integration Halting problem
External links
c2.com Wiki: Metaprogramming article Meta Programming on the Program Transformation Wiki Code generation Vs Metaprogramming "Solenoid": The first metaprogramming framework for eXist-db
Text derived from https://en.wikipedia.org/w/index.php?oldid=1374069173 under CC BY-SA 4.0; modifications have been made.