A story about working with MPS
I am currently working on a DSL for scientific calculations (integrated with Java) in MPS. As part of this DSL I need an expression interpreter. For various reasons, I cannot add the typical evaluate
methods to the expression classes themselves, so I have to write a separate interpreter.
Basically what I need to do is this: depending on the concrete expression, I need to recusively call the evaluate function, doing something (e.g. multiply or divide or add) with the result. Here is an example of the typical code you would write in Java:
Now, this code is rather ugly because it has all these if-then-else statements (Java cannot do a switch/case with an instanceof) and also all this casting stuff going on. However, since I am working with MPS I am not forced to stick with existing Java. So I have spent an hour or so to enable a nicer notation:
As you can see, I have created a new dispatch expression that basically enumerates the different cases and the result of the method. The $ sign refers to the already downcasted expression over which we dispatch, so no manual cast is necessary.
This new notation is obviously much more concise, and also much more readable. The fact that the arrows are aligned also helps.
It really is nice if you can enhance the programming language yo work with on the fly :-)