Home > Programming > First baby steps towards DSL nirvana

First baby steps towards DSL nirvana

I am sure, you must have come across this term, DSL – Domain Specific Language, if you read blogs from some well known industry experts. Well, I read this term long back, and got interested in it, and even bought the MEAP version of Ayende’s excellent book (and the only available, at the time, in the market on the subject) on Writing DSLs in Boo.

Obviously, I tried to write a DSL after reading the book. Maybe, I didn’t read it thoroughly, or for some other reason, but I couldn’t write one. And then I read this quote by Richard Feynman – β€œWhat I cannot create, I do not understand.” Obviously I was thoroughly disappointed, and felt bad and what not, but somehow I couldn’t perform the DSL magic.

Then I came across this excellent article on Lisp and it’s melleability for doing magic (read bending the language at will) and I was convinced about the power of the language. Then somehow, I came across Clojure and started learning it only because it’s a Lisp variant for JVM. And, then suddenly, one day, after spending not a very substantial time with it, the light bulb did glow for me πŸ™‚

So, a few of the basic characteristics which make a language suitable for DSL development are –

1) The language should have a very minimal syntax; yes, no semi-colons, no curly braces πŸ˜‰ [ I mean, these are noise ]
2) There should be some way by which the language supports lazy evaluation

Enough rambling and time for some code. Look at the following C# code –

public string [] GetPathsOfAllFiles(string folderName, string extension)
{
    return Directory.GetFiles(folderName, String.Format("*.{0}", extension));
}

And now look at this code (please try to imagine the context behind this code; I’ll post more on this later) –


(defmacro source-files-of-type [file-filter from folder]
Β Β Β  (list 'GetPathsOfAllFiles folder file-filter))

And yes, the above is valid code, which will run on .Net πŸ™‚ by way of Clojure’s port on CLR. So instead of –

SomeClassOrObject.GetPathsOfAllFiles("C:\\abc", "*.txt);

one can write –

(source-files-of-type "*.txt" from "C:\\abc")

Do you notice a difference in the readability? And as I mentioned before, I’ve started learning Clojure a mere three months back. Sweet πŸ™‚

I’ll be posting more on this once I am done with a first full implementation of a DSL, so until then, take care.

Advertisements
Categories: Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: