e01.org

Metaprogramming

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...

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 ==References== ==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

References

  1. ^ Sondergaard, Harald (2013). "Course on Program Analysis and Transformation" . Retrieved 18 September 2014 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  2. ^ Through Common Lisp Object System 's "Meta Object Protocol" Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  3. ^ "C++ Template Metaprogramming" . aszt.inf.elte.hu . Retrieved 2022-07-23 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1658530800.0
  4. ^ Lisp (programming language) "Self-evaluating forms and quoting", quasi-quote operator. Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  5. ^ "LMS: Program Generation and Embedded Compilers in Scala" . scala-lms.github.io . Retrieved 2017-12-06 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1512514800.0
  6. ^ Rompf, Tiark; Odersky, Martin (June 2012). "Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs" . Communications of the ACM . 55 (6): 121– 130. doi : 10.1145/2184319.2184345 . ISSN 0001-0782 . S2CID 52898203 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  7. ^ Chlipala, Adam (June 2010). "Ur: statically-typed metaprogramming with type-level record computation" (PDF) . ACM SIGPLAN Notices . PLDI '10. 45 (6): 122– 133. doi : 10.1145/1809028.1806612 . Retrieved 29 August 2012 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  8. ^ Czarnecki, Krzysztof ; Eisenecker, Ulrich W. (2000). Generative Programming . Addison Wesley. ISBN 0-201-30977-7 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  9. ^ Walker, Max. "The Art of Metaprogrmming in Java" . New Circle . Retrieved 28 January 2014 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  10. ^ Krauss, Aaron. "Programming Concepts: Type Introspection and Reflection" . Archived from the original on 10 March 2016 . Retrieved 10 March 2016 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  11. ^ Joshi, Prateek (5 April 2014). "What Is Metaprogramming? – Part 2/2" . Perpetual Enigma . Retrieved 14 August 2014 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  12. ^ for example, instance_eval in Ruby takes a string or an anonymous function. "Class BasicObject Ruby 3.5" . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  13. ^ "Art of Metaprogramming" . IBM . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  14. ^ Terry, Matt (21 August 2013). "Beware of Metaprogramming" . Medium.com . Medium Corporation . Retrieved 21 August 2014 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  15. ^ a b Bicking, Ian. "The challenge of metaprogramming" . IanBicking.org . Retrieved 21 September 2016 . Wikipedia revision 1374069173, retrieved 2026-11-21 · 1795215600.0
  16. Sondergaard, Harald. Course on Program Analysis and Transformation . 2013. retrieved 18 September 2014 · 1788908400.0
  17. LMS: Program Generation and Embedded Compilers in Scala . scala-lms.github.io . retrieved 2017-12-06 · 1512514800.0
  18. Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs . · 1788908400.0
  19. Chlipala, Adam. Ur: statically-typed metaprogramming with type-level record computation . June 2010. · 1788908400.0
  20. Walker, Max. The Art of Metaprogrmming in Java . New Circle . retrieved 28 Janu · 1788908400.0
  21. Krauss, Aaron. Programming Concepts: Type Introspection and Reflection . · 1788908400.0
  22. Joshi, Prateek. What Is Metaprogramming? – Part 2/2 . Perpetual Enigma . 5 April 2014. · 1788908400.0
  23. for example, instance_eval in Ruby (programming language)|Ruby takes a string or an anonymous function. cite web |url=https://docs.ruby-lang.org/en/master/BasicObject.html#method-i-instance_eval . · 1788908400.0
  24. Art of Metaprogramming . IBM . · 1788908400.0
  25. Bicking, Ian. The challenge of metaprogramming . IanBicking.org . retrieved 21 September 2016 · 1788908400.0
  26. Terry, Matt. Beware of Metaprogramming . Medium.com . 21 August 2013. · 1788908400.0
  27. C++ Template Metaprogramming . aszt.inf.elte.hu . retrieved 2022-07-23 · 1658530800.0
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Metaprogramming". 1788965961. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1374069173

JSON record · API collection · citation policy