\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: Software Language Engineering Sandwich DSL Case Study (Lua)},
            pdfauthor={H. Conrad Cunningham},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\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: Software Language Engineering\\
Sandwich DSL Case Study (Lua)}
\author{\textbf{H. Conrad Cunningham}}
\date{\textbf{3 March 2018}}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{4}
\tableofcontents
}
Copyright (C) 2013, 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 March
2018 is a recent version of Firefox from Mozilla.

\hypertarget{sandwich-dsl-case-study}{%
\section{Sandwich DSL Case Study}\label{sandwich-dsl-case-study}}

\hypertarget{problem-description}{%
\subsection{Problem Description}\label{problem-description}}

Suppose Emerald de Gassy, the owner of the Oxford-based catering
business Deli-Gate, hires us to design a domain-specific language (DSL)
for describing sandwich platters. The DSL scripts will direct
Deli-Gate's robotic kitchen appliance SueChef (Sandwich and Utility
Electronic Chef) to assemble platters of sandwiches.

In discussing the problem with Emerald and the Deli-Gate staff, we
discover the following:

\begin{itemize}
\item
  A sandwich platter consists of zero or more sandwiches. (Zero? Why
  not! Although a platter with no sandwiches may not be a useful, or
  profitable, case, there does not seem to be any harm in allowing this
  degenerate case. It may simplify some of the coding and
  representation.)
\item
  Each sandwich consists of layers of ingredients.
\item
  The categories of ingredients are breads, meats, cheeses, vegetables,
  and condiments.
\item
  Available breads are white, wheat, and rye.
\item
  Available meats are turkey, chicken, ham, roast beef, and tofu. (Okay,
  tofu is not a meat, but it is a good protein source for those who do
  not wish to eat meat. This is a college town after all. Oh, there is
  also a special meat served for football games Thanksgiving week called
  ``bulldog'', but it is really just chicken, so we can ignore that
  choice for our purposes here.)
\item
  Available cheeses are American, Swiss, jack, and cheddar.
\item
  Available vegetables are tomato, lettuce, onion, and bell pepper.
\item
  Available condiments are mayo, mustard, relish, and Tabasco. (Of
  course, this being the South, the mayo is Blue Plate Mayonnaise and
  the mustard is a Creole mustard.) Creole mustard.)
\item
  The sandwiches must be assembled in Oxford Standard Order (OS slice of
  bread on the bottom, then zero or more meats layered above that, then
  zero or more cheeses, then zero or more vegetables, then zero or more
  condiments, and then a slice of bread on top. The top and bottom
  slices of bread must be of the same type. (OSO was defined by the
  mysterious and picky food critic known as CH.)
\end{itemize}

\hypertarget{tasks}{%
\subsection{Tasks}\label{tasks}}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Describe an appropriate semantic model for your Lua Sandwich DSL. That
  is, what concrete data structures might you use? What
  functions/methods to manipulate the semantic model?
\item
  Remember the sandwiches must be assembled in OSO. Choose to do either
  an external DSL or an internal DSL. Tell me which. Describe the syntax
  for your DSL.
\item
  \begin{description}
  \item[Give a Lua script for configuring the following platter:]
  one turkey and Swiss on rye with tomatoes and mayo; one tofu and
  mustard on wheat.
  \end{description}
\item
  Describe a design for an implementation for your DSL. That is, how
  would you translate a DSL script into an appropriate configuration of
  the semantic model? What DSL implementation techniques or tools would
  you use? Be specific.
\end{enumerate}

\hypertarget{one-lua-solution}{%
\subsection{One Lua Solution}\label{one-lua-solution}}

\hypertarget{lua-data-representation}{%
\subsubsection{Lua Data Representation}\label{lua-data-representation}}

This semantic model design and implementation (in Lua module
\href{sandwich_model.lua}{\texttt{sandwich\_model.lua}}) uses Lua tables
to represent platters and sandwiches. (This representation is more
general than needed for the Sandwich DSL problem from the exam.)

A list-style Lua table

\begin{Shaded}
\begin{Highlighting}[]
    \OtherTok{\{}\NormalTok{ PLATTER}\OtherTok{,}\NormalTok{ n}\OtherTok{,}\NormalTok{ sandwich1}\OtherTok{,}\NormalTok{ sandwich2}\OtherTok{,} \OtherTok{...}\NormalTok{ sandwichM }\OtherTok{\}}
\end{Highlighting}
\end{Shaded}

describes a set of \texttt{n} identical platters, for some
\texttt{n\ \textgreater{}\ 0}. \texttt{PLATTER} is a tag constant. Each
platter has \texttt{M} descriptions of sandwiches, for some
\texttt{M\ \textgreater{}=\ 0}.

A list-style table

\begin{Shaded}
\begin{Highlighting}[]
    \OtherTok{\{}\NormalTok{ SANDWICH}\OtherTok{,}\NormalTok{ n}\OtherTok{,}\NormalTok{ layer1}\OtherTok{,}\NormalTok{ layer2}\OtherTok{,} \OtherTok{...}\NormalTok{ layerM }\OtherTok{\}}
\end{Highlighting}
\end{Shaded}

describes a set of \texttt{n} identical sandwiches, for some
\texttt{n\ \textgreater{}\ 0.} Each sandwich has \texttt{M} layers of
ingredients ordered from bottom to top, for some
\texttt{M\ \textgreater{}=\ 0}.

Unlike the Lair semantic model, this semantic model is not technically
object-oriented. However, the tables described above are objects
constructed and manipulated by the functions in this module. A DSL
builder module should not create or access these directly. But more
functions may be needed to enable effective use of this model by other
tools.

\hypertarget{lua-internal-dsl}{%
\subsubsection{Lua Internal DSL}\label{lua-internal-dsl}}

The Lua module
\href{sandwich_builder.lua}{\texttt{sandwich\_builder.lua}} implements
an internal Sandwich DSL in Lua. It uses Fowler's Function Sequence and
Context Variables DSL patterns. The syntax has the structure:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    platter}\OtherTok{()}
\NormalTok{    sandwich}\OtherTok{()}
\NormalTok{        ingredient commands}
\NormalTok{    sandwich}\OtherTok{()}
        \OtherTok{...}
\NormalTok{    end_platter}\OtherTok{()}
\end{Highlighting}
\end{Shaded}

The ingredient commands consist of the following in any order, but
without repetition within a sandwich:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{    bread}\OtherTok{(}\NormalTok{b}\OtherTok{)}
\NormalTok{    meats}\OtherTok{(}\NormalTok{m1}\OtherTok{,}\NormalTok{m2}\OtherTok{,...)} 
\NormalTok{    cheeses}\OtherTok{(}\NormalTok{c1}\OtherTok{,}\NormalTok{c2}\OtherTok{,...)}
\NormalTok{    vegetables}\OtherTok{(}\NormalTok{v1}\OtherTok{,}\NormalTok{v2}\OtherTok{,...)}
\NormalTok{    condiments}\OtherTok{(}\NormalTok{c1}\OtherTok{,}\NormalTok{c2}\OtherTok{,...)}
\end{Highlighting}
\end{Shaded}

Note that only one bread may be specified, but one or more of the other
ingredients may be specified in a command.

A \texttt{bread} must be specified, but all the other commands are only
given if ingredients of the associated type are to be added to the
sandwich. Regardless of the order of the commands, the sandwich is
constructed in Oxford Standard Order -- bread, meats, cheeses,
vegetables, condiments, bread from bottom to top.

The call \texttt{get\_platter()} extracts the platter from the builder.

\hypertarget{testing}{%
\subsubsection{Testing}\label{testing}}

The Lua module
\href{test_sandwichDSL.lua}{\texttt{test\_sandwichDS.lua}} implements a
simple test run of this internal DSL.

\hypertarget{acknowledgements}{%
\subsection{Acknowledgements}\label{acknowledgements}}

The problem described in this document was an extended exercise on an
examination in the Lua-based CSci 658 course in Fall 2013. The text
above is extracted from the comments at the beginning of my solution
files.

In Spring 2018, I created this Pandoc Markdown document from the source
code comments for my solutions.

This problem motivated the related Haskell SandwichDSL case study
defined in Fall 2014 for CSci 450 (and then updated for Fall 2017). That
case study, in turn, was reinterpreted as Scala case study for the
Spring 2016 CSci 555 class. I updated the descriptions of these case
studies in Spring 2018.

I maintain these notes as text in Pandoc's dialect of Markdown using
embedded LaTeX markup for the mathematical formulas and then translate
the notes to HTML, PDF, and other forms as needed. The HTML version of
this document may require use of a browser that supports the display of
MathML.

\hypertarget{references}{%
\subsection{References}\label{references}}

TODO

\hypertarget{concepts}{%
\subsection{Concepts}\label{concepts}}

TODO

Internal DSL; DSL patterns Semantic Model, Function Sequence, and
Context Variable; builder

\end{document}
