A couple weeks back I came across a post on creating the simplest possible BASIC DSL written in Scala by Szymon Jachim. The implementation itself was dead simple to understand, but I suspected it was due to the fact that it only provided
Here is a simple Lunar Lander game written in Baysick.
- Variables are denoted with the single quote which corresponds to Scala’s symbol literals
- The assignment operator is the Pascal-like
- The append operator for the
- Only integer math is supported
- Strings cannot (currently) be compared using the relational operators
- Usage of Scala code within the BASIC forms is not fully supported
- There are currently only 6 math functions:
As has been mentioned by innumerable programmers advocating (and deriding) DSLs, you are often constrained by the host language in the grammar, error reporting, and hosting capabilities of your DSL. While Scala allows you to support bizarre grammars without significantly increasing the complexity, it does not necessarily facilitate a simple way to provide solid error reporting and hosting. This is not necessarily a limitation of Scala per se, but instead for general purpose languages. In order to effectively build a wide range of DSLs divorced from their underlying host we need a language that is especially geared toward building DSLs. The two important language features that Scala provides that makes it especially conducive for DSLs are operator notation and implicits. Usually, a language is considered DSL-friendly if it allows you to omit the dot on a method call, but a really powerful DSL metalanguage would have to go much much further than that.
For some strange reason I could not coax Scala into allowing my
LETforms to be parenthesis free. I assume that it is an error on my part, but I was never able to solve it before my artificial deadline expired.
- A subroutine facility via
- Allow strings to be compared using the relation operators
So there you have it — Baysick: A Scala DSL Implementing BASIC.