Posts Tagged ‘Java’

DSLs: tweaking your language into something else

May 26, 2009

I found myself a new subject to dive into. There’s nothing like tweaking a programming language the way you want it. Adding your own constructs to your programming language, making it more expressive. Coding is all about expressing your intent as clearly as possible. So why stick to the plain old language constructs that others created for you? If you’re specifying calculations for an insurance company, why not use the abstractions and expressions that your customer knows? Wouldn’t that make communication with your customer easier? Of course, the actual calculations will be written in plain old straightforward code. But wouldn’t it be nice to have a thin layer on top of it, allowing you to express domain specific knowledge a bit more like natural language?

Such a thin layer is called a Domain Specific Language (DSL). Martin Fowler is writing an excellent book on the subject and he describes three kinds of DSLs:

  • internal DSL’s, that build on top of an existing programming language
  • external DSL’s, that have their own parser
  • language modelling frameworks, that have a user friendly click and paste GUI

As you might have guessed, I’m especially interested in the first one. Groovy has excellent support for it. And in Lisp – though its syntax sucks – it is very common to create your own domain specific commands. Actually, studying some Lisp last year got me started in “language tweaking” in the first place. But also in Java you can do some things.

There are a number of grammatical constructs you can use in a DSL. Most of them can be implemented in Groovy. Some of them can be implemented in Java. So what I’ll do the coming weeks, is explore each of the grammatical constructs and compare them in Groovy and Java. First one coming up soon.