Home > Metaprogramming (programming)
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 runtime during compile time. This allows programmers to produce a larger amount of code and get more done in the same amount of time as they would take to write all the code manually.A simple example of a metaprogram is this bash script:
#!/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 5-10 minutes, and will have generated exactly 1000 lines of code in that amount of time.
- 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 compilers and interpreters.
- A Quine is a special kind of metaprogram that has its own source as its output.
Reflection is a valuable language feature for facilitating metaprogramming. Having the programming language itself as a first class data type (as in LispLisp is a family of functional programming languages with a long history. Developed first as an abstract notation for recursive functions, it later became the favored language of artificial intelligence research during the field's heyday in the 1970s and) is also very useful.
1 See also
- Template metaprogrammingTemplate metaprogramming is a programming technique to get some evaluations done at compile-time by using the facility of templates in C++. Most of the techniques used in template metaprogramming require a C++ compiler with fairly high Standard Compliance - a C++ compile-time example.
- Line of codeThe definition of a line of code though basic to many Software Engineering metrics, is ambiguous. What a "line of code" means will certainly vary across languages, but it also varies for a given language. In the C language, for example, a line of code mig - a measure of Software Engineering and static code analysisStatic code analysis is a set of methods for analysing software source code or object code in an effort to gain understanding of what the software does and establish certain correctness criteria. Schematically, there exist several types of static analysis.
- OpenC++OpenC+ is a software tool to parse and analyze C++ source code. It uses a metaobject protocol (MOP) to provide services for language extensions. The following tools provide similar capabilities: CLOS GCC XML Node Introspector Flexible object generator lan
2 External links