Wednesday, August 5, 2009

First steps in creating an internal DSL with Groovy

Sten Anderson has a great post about creating an internal DSL with Groovy: A Text Adventure DSL in Groovy.

The "domain" of this example DSL is old-school adventure games like King's Quest.

The post shows the different steps how to transform the following Java-like code...
game.look();
game.go("north");
game.take("dagger");
game.take("sword");
game.take("key");
game.take("axe");

... into the following statements:
look
go north
take dagger
grab sword
hold key
yoink axe

Both of the above snippets are valid Groovy code that can be executed on the JVM!

To understand and learn about the implementation of internal DSLs I implemented this DSL example.

The project can be browsed and downloaded on my github account. The project is fully mavenized, and should easily be buildable and runnable with Maven.

5 comments:

  1. looks like rather simple message selection to me. what about chaining? could this be done as easily?

    ReplyDelete
  2. Yoink axe? :-)

    And please, explain why the sword is needed at all if you got an axe.

    ReplyDelete
  3. @Anonymous
    I have a slight suspicion that you you are a dwarf...

    Maybe you could use the sword for grooming you beard?

    ReplyDelete
  4. Hey thanks for the link. I think it's cool that you implemented the DSL.

    ReplyDelete
  5. Hey

    That looks a bit familiar! Check out groovymud,it's an entire multi user dungeon code base written in groovy that uses a very simila dsl to this o_0
    http://code.google.com/p/groovymud

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...