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 reflexivity.
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.
A simple example of a metaprogram is this bash script, which is an example of generative programming:
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 which prints out the numbers 1\u2013992. 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 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.