If you ever need to create an interactive console/REPL that let’s users use play with your code in a simple terminal you might (still) find that there’s hardly any documentation. I fixed the console for Gremlin-Scala (a graph DSL) and thought that other people might want a very basic version of a custom shell to have a quick start.
It doesn’t do much more than importing java.lang.Math so that you can type things like `abs(-4.2)` directly in the shell without having to import java.lang.Math._ first. The important part is that your import statements and any initialisation belongs into `addThunk` – those are executed after the shell is fully initialised – otherwise you get all sorts of AST errors that don’t really help much.
This is obviously only a starting point, you can further customise it to automatically do stuff with the executed command like we do in Gremlin-Scala’s Console.
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.ILoop
object TestConsole extends App {
val settings = new Settings
settings.usejavacp.value = true
settings.deprecation.value = true
new SampleILoop().process(settings)
}
class SampleILoop extends ILoop {
override def prompt = "==> "
addThunk {
intp.beQuietDuring {
intp.addImports("java.lang.Math._")
}
}
override def printWelcome() {
echo("\n" +
" \\,,,/\n" +
" (o o)\n" +
"-----oOOo-(_)-oOOo-----")
}
}
One Response to “Create your custom Scala REPL”
Sorry, the comment form is closed at this time.
It is tough to discover knowledgeable people today on this subject, but you sound like you know what you are talking about! Thanks