Skip to content

staging

Riley Evans edited this page Feb 24, 2021 · 1 revision

Staging

Meta-Programming is a powerful technique that allows programs to treat others as values. This means that a program can modify other programs and generate new code. It is possible for programs to modify others while running. However, it is more commonly implemented into compilers to allow for the generation of temporary source code. This allows for domain specific transformations to be made to programs that a general compilation process would not be able to do.

Different languages implement meta-programming to differing degrees. For example, C allows for the use of macros. They allow for code and objects to be extracted to create macros that can be used throughout the code. However the power of them is very limited - they do not allow for C code to modify the code the macro will insert. The language provided to operate on macros is very basic with few operations that can be used.

#DEFINE TODO "Add a better example of C macros"

Another language that can take advantage of meta-programming is Haskell. Template Haskell has many more capabilities in comparison to C macros. The allow for the compiler to execute specified parts of the code to generate more specific code. It is possible to give a simple example with a function that computes the power of x^n.

power :: Int -> [|Int|] -> [|Int|]
power 0 x = [| 1 |]
power n x = [| $x * $(power (n-1) x)|]

This can be evaluated as:


TODO...

Clone this wiki locally