CSci 658-01: Software Language Engineering
Metaprogramming

H. Conrad Cunningham

22 February 2018

Copyright (C) 2017, 2018, H. Conrad Cunningham
Professor of Computer and Information Science
University of Mississippi
211 Weir Hall
P.O. Box 1848
University, MS 38677
(662) 915-5358

Advisory: The HTML version of this document requires use of a browser that supports the display of MathML. A good choice as of February 2018 is a recent version of Firefox from Mozilla.

Definition

Basically, metaprogramming is writing code that writes code.

Metaprogramming:
the writing of computer programs that can treat computer programs as their data. A program can read, generate, analyze, and/or transform other programs, and even modify itself while running [Adapted from Wikipedia and other sources, 2019]

We often do metaprogramming in our tasks but just not call it that.

Under the above definition, much of our study of domain-specific languages is metaprogramming.

The latter are examples of reflexive metaprogramming.

Reflexive metaprogramming:
the writing of computer programs that manipulate themselves as data.

This manipulation may be at “compile time” involving a phase of transformations in the code before the final program is generated. Or it may be at runtime, involving manipulation of the program’s metamodel or generation of new code that is dynamically executed within the program.

The Survey DSL is a Ruby internal DSL. It takes advantage of Ruby’s metaprogramming facilities such as the abilities to trap calls to undefined methods, to dynamically add methods or variables to existing objects at runtime, and to execute dynamically generated strings as Ruby code. It also uses Ruby’s closures (first-class functions) and flexible syntax – although these are not technically metaprogramming features.

The Lair Configuration programs use the metaprogramming features of Lua in similar ways.

Consider relatively common languages and their metaprogramming features.

  1. Java is a statically typed, compiled language. What are metaprogramming features available in Java?

    dynamic class loaders, reflection API, annotation processing, dynamic method invocation (JVM feature), JVM bytecode manipulation (mostly with external tools), etc.

  2. Lua is a dynamically typed, interpreted language. What are the metaprogramming features available in Lua?

    metatables, metamethods, manipulation of environments, debug library (introspection/reflection features), loadfile and loadstring functions to dynamically execute code, extensions in C, etc.