Skip to content

Commit 6e6d5c0

Browse files
committed
Invoke REPL from code
A FAQ is how to invoke REPL from code. Document the common entry points.
1 parent 7e29451 commit 6e6d5c0

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

overviews/repl/embedding.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
layout: overview-large
3+
title: Programmatic Use of Scala REPL
4+
5+
disqus: true
6+
7+
partof: repl
8+
num: 2
9+
outof: 2
10+
---
11+
12+
The REPL can be embedded and invoked programmatically.
13+
14+
It supports the `javax.script` API, or it can be used
15+
as either a code interpreter or an interactive command line.
16+
17+
import scala.tools.nsc.Settings
18+
import scala.tools.nsc.interpreter._
19+
import javax.script._
20+
21+
/** A simple example showing programmatic usage of the REPL. */
22+
object Main extends App {
23+
24+
// the REPL has some support for javax.script
25+
val scripter = new ScriptEngineManager().getEngineByName("scala")
26+
scripter.eval("""println("hello, world")""")
27+
28+
// compiler settings
29+
val settings = new Settings
30+
settings.processArgumentString("-deprecation -feature -Xfatal-warnings -Xlint")
31+
32+
// the interpreter is used by the javax.script engine
33+
val intp = new IMain(settings)
34+
def interpret(code: String): Unit = {
35+
import Results._
36+
val res = intp.interpret(code) match {
37+
case Success => "OK!"
38+
case _ => "Sorry, try again."
39+
}
40+
println(res)
41+
}
42+
interpret("""println("hello, world")""")
43+
interpret("""println(""")
44+
interpret("""val who = "world" ; println("hello, $who")""")
45+
46+
// the REPL uses a line reader and an interpreter interactively
47+
val interactive = new ILoop()
48+
interactive.process(settings)
49+
50+
// input to the REPL can be provided programmatically
51+
import java.io.{BufferedReader, StringReader, PrintWriter}
52+
val reader = new BufferedReader(new StringReader(""""hello, world""""))
53+
val canned = new ILoop(reader, new PrintWriter(Console.out, true))
54+
canned.process(settings)
55+
56+
// more canning
57+
val code = """println("hello, world") ; 42"""
58+
val out = ILoop.run(code)
59+
println(s"Output is $out")
60+
}
61+

overviews/repl/overview.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ disqus: true
66

77
partof: repl
88
num: 1
9-
outof: 1
9+
outof: 2
1010
---
1111

1212
The Scala REPL is a tool (_scala_) for evaluating expressions in Scala.
@@ -30,17 +30,29 @@ Useful REPL features include:
3030
- the REPL's IMain is bound to `$intp`.
3131
- the REPL's last exception is bound to `lastException`.
3232
- use tab for completion.
33+
- use `//print<tab>` to show typed desugarings.
3334
- use `:help` for a list of commands.
3435
- use `:paste` to enter a class and object as companions.
35-
- use `:paste -raw` to specify a package (no template wrapper).
36+
- use `:paste -raw` to disable code wrapping, to define a package.
3637
- use `:javap` to inspect class artifacts.
3738
- use `-Yrepl-outdir` to inspect class artifacts with external tools.
39+
- use `:power` to enter power mode and import compiler components.
3840

3941
Implementation notes:
4042

4143
- user code can be wrapped in either an object (so that the code runs during class initialization)
4244
or a class (so that the code runs during instance construction). The switch is `-Yrepl-class-based`.
4345

46+
### Power Mode
47+
48+
`:power` mode imports identifiers from the interpreter's compiler.
49+
50+
That is analogous to importing from the runtime reflective context using `import reflect.runtime._, universe._`.
51+
52+
Power mode also offers some utility methods as documented in the welcome banner.
53+
54+
Its facilities can be witnessed using `:imports` or `-Xprint:parser`.
55+
4456
### Contributing to Scala REPL
4557

4658
The REPL source is part of the Scala project. Issues are tracked by the standard

0 commit comments

Comments
 (0)