2009.02.03
The World's Smallest Domain-Specific Language
Domain-specific languages, also known as little languages, allow us to express knowledge in a form close to the problem at hand. In contrast to general-purpose languages, like Java or C++, they are specialized for a narrow domain. Earlier today I wanted to initialize a rectangular array of Boolean values to represent the stick figure of a human. For that I devised a tiny domain-specific language (DSL) consisting of two symbols (representing an on and an off pixel) and wrote its commensurably simple interpreter.
Here is the stick figure, drawn in the DSL.
String simg = " # " +
"###" +
" # " +
" # " +
"# #";
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
img[r][c] = (simg.charAt(r * cols + c) == '#');