Created
March 28, 2009 16:50
-
-
Save jrk/87146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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