blog dds

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 =     " # " +
		  "###" +
		  " # " +
		  " # " +
		  "# #";
and here is its three-line interpreter.
for (int r = 0; r < rows; r++)
    for (int c = 0; c < cols; c++)
        img[r][c] = (simg.charAt(r * cols + c) == '#');
DSLs need not be complex, but they can provide a terrific bang for the buck.

Read and post comments, or share through   


Creative Commons License Last modified: Tuesday, February 3, 2009 1:04 pm
Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.