Mop
These dynamic behaviors are governed by the metaobject protocol (MOP) that each of these versions implements in their runtime. the metaobject protocol of a language defines the semantics of the extensibility of the language artifacts. take a look at the accompanying callout for a gentle introduction to the concept of MOP in programming ages.
Definition a meta-object is an alias action that manipulates the behavior of other objects. in an OOP language, a metaclass might be responsible for creating and manipulating classes. to do that, the metaclass needs to store all information that's relevant to the class, like type, interface, methods, and extension objects.
A meta-object protocol (MOP) for a language defines the semantics of the extensibility of programs written in that language. the behavior of theprogram is determined by the mop, including aspects of the program that can be extended by the programmer during compile time or runtime.
Metaprogramming is the ability to write programs that generate new programs or that change the behavior of existing programs.
In an OO language likeRuby or groovy, Required implies capabilities that extend existing object models, add hooks to alter the behaviors of existing methods (or even classes), and synthesize new methods, properties, or modules during runtime through introspection.
Ages likeLISPUse macros as the metaprogramming tool that let you syntactically extend the language during the compilation stage. although the primary form of metaprogramming That's supported by groovy or Ruby is runtime, LISP metaprogramming is compile time, and doesn't incur any runtime overhead. (both groovy and Ruby have library support for compile-time metaprogramming through explicit manipulation of the ast S. but it's nowhere near as elegant as lisp.
JavaAlso offers metaprogramming capabilities through annotation processing and Aspect-oriented programming (AOP); it also defines all its extensibility mechanisms through its mop.
Statically typed versions likeHaskellAnd ocaml that have traditionally relied on pure embedded semantics for designing DSL s now offer type-safe compile-time metaprogramming through extensions like template Haskell and metaocaml respectively. for more information, see http://www.haskell.org/th/ and http://www.metaocaml.org/
Runtime metaprogramming in DSL implementation
Why is metaprogramming support such an important feature for a language to host a DSL? The answer is that because metaprogramming support makes a language exten-sible, the DSL that you 've implemented in an extensible language also becomes transi-tively extensible.
Compile-time metaprogramming in DSL implementation
Compile-time metaprogramming lets you construct and manipulate pro-grams during compile time. you can define new constructs and interact with the compiler to perform syntactic transformations and application-specific optimizations. with compile-time metaprogramming, you can grow a language seamlessly toward your domain syntax.
To use the most common form of compile-time metaprogramming you implement syntactic macros.
Macro implementations vary in complexity and power, from the textual macros offered by C preprocessors to the sophisticated Ast-based ones offered by variants of Lisp and some statically typed versions like template Haskell and templates.
Besides macros, some versions offer other Preprocessor-based capabilities for compile-time metaprogramming, like the templates in C ++, AOP, and annotation processing. some languages like groovy and Scala also have implementations of explicit compiler plugins, which provide some metaprogramming capabilities in the form of AST manipulation.
C ++: templates
C ++ offers templates as one of the most dominant protocols for metaprogramming. c ++ templates support powerful code-generation mechanisms through manipulation of data structures during compile time. this form of compile-time metaprogramming has been used quite successfully in scientific and numerical applications for generating inline versions of algorithms that employ techniques like loop unrolling for optimizing performance.
LISP and C lojure: macros
Lisp has the most sophisticated and complete support for compile-time metaprogramming, through its system of macros. unlike C macros that have limited expressive power and that operate based on Textual substitution, LISP macros are powered by the full extensibility of the language.
Java: annotation processing and AOP support
Java also supports a limited form of compile-time metaprogramming through annotation processing and support for AOP. you can use annotations in a Java program that get processed during build-time. these annotations generate code that can supplement or alter existing program behavior.
Aspectj is the aspect-oriented extension to Java that offers a small set of powerful constructs that let you inject additional behavior into existing programs through bytecode instrumentation.
LISP
CST & AST
When you write a program in most ages, what you're writing is being represented as the concrete syntax tree (CST ). the CST is the faithful representation of your program, including the white spaces, com-ments, and any metainformation you generate.
Your program is then passed through scanners, lexical analyzers, and parsers to generate what we call the abstract syntax tree (AST ). the AST is the syntactic essence of your program that gets pipelined into the next phases of the compilation process. it goes through the usual steps of transformation, optimization, and code genera-tion.
The Parser of your language is primarily responsible for all such trans-formations that lead to the generation of the ast from the CST.
Code as data
In lisp, every program is a list structure, which is the ast of the Code itself. as a result, code is seen as having the same representation and syntax as data. by standardizing on this simple protocol, the language publishes the abstract syntax to the programmer. this abstract syntax is simple-it's a list. any metaprogram that you generate using lisp needs to conform only to this simple, standard representation.
Data as code
By using the quote special form, data syntax can be embedded easily into the code syntax. lisp macros are good examples of this philosophy. in fact, LISP extends this paradigm of data as code and offers a full-blown template mechanic for writing metaprograms. in Common LISP, we call this quasiquotation. in clojure, the same feature is implemented through syntax quote, unquote, and splicing Unquote.
Mop reference
Kiczares, Gregor, Jim des Rivieres, and Daniel G. Bobrow. 1991. The Art of The metaobject protocol. The MIT Press