Skip to content

Instantly share code, notes, and snippets.

@jrk
Created March 28, 2009 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jrk/87146 to your computer and use it in GitHub Desktop.
Save jrk/87146 to your computer and use it in GitHub Desktop.
# A tiny DSL implementation for a minimal subset of BASIC in Scala
# Via http://www.nabble.com/-scala--ease-of-making-DSLs-in-Scala---nobody-cares-to22450638.html
class BasicClass {
abstract sealed class BasicLine
case class PrintLine(num: Int, s: String) extends BasicLine
case class GotoLine(num: Int, to: Int) extends BasicLine
val lines = new scala.collection.mutable.HashMap[Int, BasicLine]
case class linebuilder(num: Int) {
def GOTO(to: Int) = lines(num) = GotoLine(num, to)
def PRINT(s: String) = lines(num) = PrintLine(num, s)
}
private def gotoLine(line: Int) {
lines(line) match {
case PrintLine(_, s) => println(s); gotoLine(line + 10)
case GotoLine(_, to) => gotoLine(to)
}
}
def RUN {
gotoLine(lines.keys.toList.first)
}
implicit def int2linebuilder(i: Int) = linebuilder(i)
}
object Playground extends BasicClass {
def main(args: Array[String]) {
10 PRINT "SCALA ROCKS!"
20 GOTO 10
RUN
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment