Metaprogramming

from Wikipedia, the free encyclopedia

In computer science, metaprogramming is the creation of computer programs ("metaprograms") that generate computer programs.

The goal is to let the computer work in whole or in part on the programming process. This makes it possible to reduce errors through automation and to create and execute computer programs ad hoc. Metaprogramming also allows the programmer to add new constructions to the programming language. Therefore, it is a very effective means of developing domain-specific languages in homo-iconic programming languages .

Basic concepts

If the programming language is homo-iconic , like Lisp and its dialects, the program can be generated by the metaprogram directly at the level of the program structure. If homoiconicity is not given, the program generation can, as a makeshift, take place lexically, with the metaprogram generating the source text of the target program. In this (more error-prone) form, metaprogramming can basically be carried out in any programming language.

A weakened means of metaprogramming is introspection , in which the structure of the target program must, however, already be established at the time the metaprogram is created.

Metaprogramming is one of the conceptual foundations of the programming language compiler . But it also serves the desire to develop adaptive software systems that can easily be adapted to changing framework conditions either at runtime or within the development phase.

classification

Metaprogramming can be divided according to different aspects:

After processing time:

  • static (at compiler time)
  • dynamic (at runtime)

By language:

  • homogeneous (metalanguage is object language)
  • heterogeneous (otherwise)

According to levels:

  • multi-level (object language is itself a metalanguage)
  • one-stage (otherwise)

Examples

Metaprogramming in homo-iconic programming languages

The macro system of Lisp and its dialects is the most powerful tool currently available for metaprogramming due to the homoiconicity of the language, since this is done here on the level of the program structure, technically on the parse tree . This makes it easy to add new control structures to Lisp, as the following definition in Common Lisp shows, which defines the WHILE loop . Common Lisp does not contain this in its usual form.

(defmacro while (cond &body body)
  (let ((name (gensym)))
    `(labels ((,name ()
                (if ,cond
                    (progn
                      ,@body
                      (,name)))))
       (,name))))

This definition leads the WHILE loop back to a terminal recursion . The new language construct can then be used directly:

(let ((a 0))
  (while (< a 10)
    (print a)
    (setq a (1+ a))))

The execution of this program then results in the output of the numbers from 0 to 9.

XSL is one of the few also homo-iconic programming languages. It describes the transformation of XML data. At the same time, XSL is valid XML, which means that homoiconicity is given.

Self-modifying code

Even in early computer systems, programs were sometimes used that process their own executable ( machine language ) representation at runtime .

Makeshift metaprogramming in non-homoiconical programming languages

  • Text macros in C make it possible to use #definethe C compiler to submit a source text that is created by macro expansion. The process was originally intended for the production of inline procedures. It is astable and very error-prone.
  • C ++ metaprogramming refers to the technique of metaprogramming within the C ++ programming language.
  • Strictly speaking, even simpler scenarios also belong to metaprogramming, such as the generation of JavaScript code for the browser using a server-side PHP script or other forms of code generation . Almost any programming language can be used. Since the program generation also takes place here on the level of the source text, it is also very error-prone.

Metaprogramming in Psychology

According to neurophysiologist John Cunningham Lilly , metaprogramming describes the programming of the human bio-computer through metacommunication .

literature

Computer science

  • Patrick M. Krusenotto Functional Programming and Metaprogramming - Interactive in Common Lisp Springer-Verlag 2016, ISBN 978-3-658-13743-4
  • Thomas Maier-Komor: Metaprogramming methods for reconfiguring software in embedded systems . Dr. Cap; Edition: 1st edition (January 31, 2007). ISBN 978-3899634709
  • Oliver Vogel, Ingo Arnold, Arif Chughtai, Timo Kehrer: Software Architecture: A Comprehensive Framework and Guide for Practitioners . Jumper; Edition: 2011 (September 17, 2011). ISBN 978-3642197352

psychology

  • John C. Lilly: Programming and Metaprogramming the Human Biocomputer . Phenomenon Publishing House; Edition: 1st edition (April 18, 2010). ISBN 978-3933321688

Web links

Individual evidence

  1. Doga Arinir: Multidimensional separation of issues in software development through feature components . W3L GmbH; Edition: 1 (July 18, 2007). ISBN 978-3937137537 . Page 50/51
  2. Metaprogramming - script chapter (University of Passau; PDF, 195 kB)
  3. Patrick M. Krusenotto Functional Programming and Metaprogramming - Interactive in Common Lisp Springer-Verlag 2016, ISBN 978-3-658-13743-4 . Page 275