Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Metaprogramming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Code generation === A simple example of a metaprogram is this [[Unix shell|POSIX Shell]] [[script (computer programming)|script]], which is an example of [[generative programming]]: <syntaxhighlight lang="bash"> #!/bin/sh # metaprogram echo '#!/bin/sh' > program for i in $(seq 992) do echo "echo $i" >> program done chmod +x program </syntaxhighlight> 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 (computing)|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 Sharp (programming language)|C#]], [[Forth (programming language)|Forth]], [[Frink (programming language)|Frink]], [[Groovy (programming language)|Groovy]], [[JavaScript]], [[Lisp (programming language)|Lisp]], [[Elixir (programming language)|Elixir]], [[Lua (programming language)|Lua]], [[Nim (programming language)|Nim]], [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[Rebol]], [[Ruby (programming language)|Ruby]], [[Rust (programming language)|Rust]], [[R (programming language)|R]], [[SAS (software)|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 language]]s (DSLs). A fairly common example of using DSLs involves generative metaprogramming: [[Lex programming tool|lex]] and [[yacc]], two tools used to generate [[lexical analysis|lexical analyser]]s and [[parsing|parser]]s, let the user describe the language using [[regular expression]]s and [[context-free grammar]]s, and embed the complex algorithms required to efficiently parse the language.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)