Implementation
Let's create an internal DSL to control a smart house. This example fits well into the Internet of things era, which is getting more and more attention nowadays. The user is able to control their home using a very simple event notation. An event has the form of command -> receiver -> arguments
. The arguments part is optional. Not all events require arguments. An example of an event that does not require any arguments is shown:
open -> gate
An example of an event that requires arguments is shown:
increase -> boiler temperature -> 3 degrees
The ->
symbol is used to mark the end of one part of an event and state the beginning of the next one. There are many ways to implement an internal DSL. We can use plain old regular expressions, string processing, a combination of operator overloading, and metaprogramming, or a library/tool that can do the hard work for us. Although, officially, Interpreter does not address parsing, I feel that a practical example needs...