\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
  \usepackage{unicode-math}
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
            pdftitle={CSci 658-01: Software Language Engineering Metaprogramming},
            pdfauthor={H. Conrad Cunningham},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

\usepackage{caption}
\DeclareCaptionLabelFormat{nolabel}{}
\captionsetup{labelformat=nolabel}

\title{CSci 658-01: Software Language Engineering\\
Metaprogramming}
\author{\textbf{H. Conrad Cunningham}}
\date{\textbf{22 February 2018}}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{4}
\tableofcontents
}
Copyright (C) 2017, 2018, \href{http://www.cs.olemiss.edu/~hcc}{H.
Conrad Cunningham}\\
Professor of \href{https://www.cs.olemiss.edu}{Computer and Information
Science}\\
\href{http://www.olemiss.edu}{University of Mississippi}\\
211 Weir Hall\\
P.O. Box 1848\\
University, MS 38677\\
(662) 915-5358

\textbf{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.

\hypertarget{definition}{%
\subsection{Definition}\label{definition}}

Basically, \emph{metaprogramming} is writing code that writes code.

\begin{description}
\tightlist
\item[\textbf{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{]}
\end{description}

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

\begin{itemize}
\item
  Our web applications may generate HTML, JavaScript, and CSS code to
  enable the display of data in a web browser.
\item
  Our Java programs may use \texttt{instanceof} to check the type of
  objects or otherwise manipulate itself with the Java reflection
  package.
\item
  Our programs may use macros to define new features in terms of
  existing features.
\end{itemize}

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

\begin{itemize}
\item
  The \texttt{pic} little language processor takes a program expressed
  in an external textual language that describes a picture and generates
  output expressed in another language that gives instructions to a
  display program.
\item
  Several of the State Machine DSL processors read and parse a program
  written in in a special-purpose textual language, represent the
  program internally in a semantic model, and then ``execute'' the model
  on inputs.
\item
  Other of the State Machine processors use the semantic model to
  generate a program in another language such as C or the Graphviz dot
  language for graphs.
\item
  The Computer Configuration and Email Message internal DSLs use the
  host language itself to encode special-purpose languages. The
  processors can then read and parse descriptions written in these
  special-purpose languages and manipulate the resulting data structures
  similarly to the external DSLs.
\item
  The Survey and Lair Configuration DSLs manipulate the structure of the
  processing program itself to implement the special-purpose language.
\end{itemize}

The latter are examples of \emph{reflexive metaprogramming}.

\begin{description}
\tightlist
\item[\textbf{Reflexive metaprogramming}:]
the writing of computer programs that manipulate themselves as data.
\end{description}

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.

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Java is a statically typed, compiled language. What are
  metaprogramming features available in Java?

  \begin{quote}
  dynamic class loaders, reflection API, annotation processing, dynamic
  method invocation (JVM feature), JVM bytecode manipulation (mostly
  with external tools), etc.
  \end{quote}
\item
  Lua is a dynamically typed, interpreted language. What are the
  metaprogramming features available in Lua?

  \begin{quote}
  metatables, metamethods, manipulation of environments, debug library
  (introspection/reflection features), \texttt{loadfile} and
  \texttt{loadstring} functions to dynamically execute code, extensions
  in C, etc.
  \end{quote}
\end{enumerate}

\end{document}
