Skip to content

Commit 65a91e3

Browse files
committed
Turn dotty-repl project into a Scala 2 project
1 parent f9bb446 commit 65a91e3

File tree

11 files changed

+144
-114
lines changed

11 files changed

+144
-114
lines changed

project/Build.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,10 @@ object Build {
360360

361361
lazy val `dotty-repl` = project.in(file("repl")).
362362
dependsOn(
363-
`dotty-language-server`,
364-
`dotty-compiler-bootstrapped`,
365-
`dotty-compiler-bootstrapped` % "test->test"
363+
`dotty-compiler`,
364+
`dotty-compiler` % "test->test"
366365
).
367-
settings(commonBootstrappedSettings).
366+
settings(commonNonBootstrappedSettings).
368367
settings(
369368
// set system in/out for repl
370369
connectInput in run := true,

repl/src/dotty/tools/repl/ParseResult.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ object Load {
5454
* String
5555
* ```
5656
*/
57-
case class Type(expr: String) extends Command
58-
object Type {
57+
case class TypeOf(expr: String) extends Command
58+
object TypeOf {
5959
val command = ":type"
6060
}
6161

@@ -106,7 +106,7 @@ object ParseResult {
106106
case Reset.command => Reset
107107
case Imports.command => Imports
108108
case Load.command => Load(arg)
109-
case Type.command => Type(arg)
109+
case TypeOf.command => TypeOf(arg)
110110
case _ => UnknownCommand(cmd)
111111
}
112112
case _ => {

repl/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package repl
33

44
import java.io.{ File => JFile }
55

6+
import dotc.ast.Trees._
67
import dotc.ast.{ untpd, tpd }
78
import dotc.{ Run, CompilationUnit, Compiler }
89
import dotc.core.Decorators._, dotc.core.Flags._, dotc.core.Phases
@@ -71,7 +72,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
7172
}
7273

7374
val (exps, other) = trees.partition(_.isTerm)
74-
val resX = exps.zipWithIndex.flatMap { (exp, i) =>
75+
val resX = exps.zipWithIndex.flatMap { case (exp, i) =>
7576
val resName = (str.REPL_RES_PREFIX + (i + state.valIndex)).toTermName
7677
val show = createShow(resName, exp.pos)
7778

@@ -107,7 +108,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
107108
).apply(Nil, pat).reverse
108109
}
109110

110-
t :: variables.map(createShow)
111+
t :: variables.map { case (name, pos) => createShow(name, pos) }
111112
}
112113
case t => List(t)
113114
}

repl/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dotc.ast.untpd
1111
import dotc.ast.tpd
1212
import dotc.interactive.{ SourceTree, Interactive }
1313
import dotc.core.Contexts.Context
14-
import dotc.Run
14+
import dotc.{ CompilationUnit, Run }
1515
import dotc.core.Mode
1616
import dotc.core.Flags._
1717
import dotc.core.Types._
@@ -197,21 +197,25 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
197197

198198
/** Compile `parsed` trees and evolve `state` in accordance */
199199
protected[this] final def compile(parsed: Parsed)(implicit state: State): State = {
200+
import dotc.ast.Trees._
201+
import dotc.ast.untpd._
200202
def extractNewestWrapper(tree: untpd.Tree): Name = tree match {
201-
case untpd.PackageDef(_, (obj: untpd.ModuleDef) :: Nil) => obj.name.moduleClassName
203+
case PackageDef(_, (obj: ModuleDef) :: Nil) => obj.name.moduleClassName
202204
case _ => nme.NO_NAME
203205
}
204206

205207
compiler
206208
.compile(parsed)
207209
.fold(
208210
displayErrors,
209-
(unit, newState) => {
210-
val newestWrapper = extractNewestWrapper(unit.untpdTree)
211-
val newImports = newState.imports ++ extractImports(parsed.trees)(newState.run.runContext)
212-
val newStateWithImports = newState.copy(imports = newImports)
211+
{
212+
case (unit: CompilationUnit, newState: State) => {
213+
val newestWrapper = extractNewestWrapper(unit.untpdTree)
214+
val newImports = newState.imports ++ extractImports(parsed.trees)(newState.run.runContext)
215+
val newStateWithImports = newState.copy(imports = newImports)
213216

214-
displayDefinitions(unit.tpdTree, newestWrapper)(newStateWithImports)
217+
displayDefinitions(unit.tpdTree, newestWrapper)(newStateWithImports)
218+
}
215219
}
216220
)
217221
}
@@ -329,12 +333,12 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
329333
state.withHistory(loadCmd)
330334
}
331335

332-
case Type(expr) => {
336+
case TypeOf(expr) => {
333337
compiler.typeOf(expr).fold(
334338
displayErrors,
335339
res => out.println(SyntaxHighlighting(res))
336340
)
337-
state.withHistory(s"${Type.command} $expr")
341+
state.withHistory(s"${TypeOf.command} $expr")
338342
}
339343

340344
case Quit =>
@@ -360,7 +364,7 @@ class ReplDriver(settings: Array[String], protected val out: PrintStream = Syste
360364

361365
/** Render messages using the `MessageRendering` trait */
362366
private def renderMessage(cont: MessageContainer): Context => String =
363-
messageRenderer.messageAndPos(cont.contained(), cont.pos, messageRenderer.diagnosticLevel(cont))
367+
messageRenderer.messageAndPos(cont.contained(), cont.pos, messageRenderer.diagnosticLevel(cont))(_)
364368

365369
/** Output errors to `out` */
366370
private def displayErrors(errs: Seq[MessageContainer])(implicit state: State): State = {

repl/src/dotty/tools/repl/package.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package object repl {
1717
new StoreReporter(null)
1818
with UniqueMessagePositions with HideNonSensicalMessages
1919

20-
private[repl] implicit class ListOps[A](xs: List[A]) extends AnyVal {
20+
private[repl] implicit class ListOps[A](val xs: List[A]) extends AnyVal {
2121
def intersperse(a: A): List[A] = {
2222
def recur(xs: List[A]): List[A] = xs match {
2323
case x :: Nil => List(x)
@@ -27,19 +27,15 @@ package object repl {
2727
}
2828
}
2929

30-
private[repl] implicit class ShowUser(ds: Denotation | Symbol) extends AnyVal {
30+
private[repl] implicit class ShowUser(val s: Symbol) extends AnyVal {
3131
def showUser(implicit ctx: Context): String = {
3232
val printer = new UserFacingPrinter(ctx)
33-
val text = ds match {
34-
case d: Denotation => printer.dclText(d.symbol)
35-
case s: Symbol => printer.dclText(s)
36-
}
37-
33+
val text = printer.dclText(s)
3834
text.mkString(ctx.settings.pageWidth.value)
3935
}
4036
}
4137

42-
private[repl] implicit class StoreReporterContext(ctx: Context) extends AnyVal {
38+
private[repl] implicit class StoreReporterContext(val ctx: Context) extends AnyVal {
4339
def flushBufferedMessages(): List[MessageContainer] =
4440
ctx.reporter match {
4541
case rep: StoreReporter => rep.removeBufferedMessages(ctx)

repl/src/dotty/tools/repl/results.scala

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,38 @@ import dotc.reporting.diagnostic.MessageContainer
77
* in the REPL
88
*/
99
object results {
10-
type Errors = List[MessageContainer]
11-
private case class ErrorContainer(messages: Errors)
1210

13-
/** A result of a computation is either a list of errors or the result `A` */
14-
class Result[+A] private[results] (result: ErrorContainer | A) { self =>
11+
/** Type alias for `List[MessageContainer]` */
12+
private type Errors = List[MessageContainer]
13+
/** Private ADT instead of using union type, we're not dotty yet... */
14+
private sealed trait Disjunction[+A]
15+
/** Successful version of `Disjunction[A]` */
16+
private case class Success[A](a: A) extends Disjunction[A]
17+
/** Erroneous version of `Disjunction[A]` */
18+
private case class ErrorContainer(messages: Errors) extends Disjunction[Nothing]
19+
20+
/** A result of a computation is either a list of errors or the result `A`
21+
*
22+
* @note should be replaced by the right-biased `Either` from 2.12.x, TODO
23+
*/
24+
class Result[+A] private[results] (result: Disjunction[A]) { self =>
25+
1526
def flatMap[B](f: A => Result[B]): Result[B] =
1627
result match {
1728
case ec: ErrorContainer => new Result(ec)
18-
case a: A @unchecked => f(a)
29+
case Success(a) => f(a)
1930
}
2031

2132
def map[B](f: A => B): Result[B] =
2233
result match {
2334
case ec: ErrorContainer => new Result(ec)
24-
case a: A @unchecked => new Result(f(a))
35+
case Success(a) => new Result(Success(f(a)))
2536
}
2637

2738
def fold[B](onErrors: Errors => B, onResult: A => B): B =
2839
result match {
2940
case ErrorContainer(errs) => onErrors(errs)
30-
case a: A @unchecked => onResult(a)
41+
case Success(a) => onResult(a)
3142
}
3243

3344
def recoverWith[B >: A](pf: PartialFunction[Errors, Result[B]]): Result[B] =
@@ -51,7 +62,7 @@ object results {
5162
}
5263

5364
implicit class ResultConversionA[A](val a: A) extends AnyVal {
54-
def result: Result[A] = new Result(a)
65+
def result: Result[A] = new Result(Success(a))
5566
}
5667

5768
implicit class ResultConversionErr(val xs: Errors) extends AnyVal {

repl/src/dotty/tools/repl/terminal/filters/ReadlineFilters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object ReadlineFilters {
161161
allAfterCursor.splitAt(
162162
nextNewlineIndex
163163
) match {
164-
case (Vector(), Vector('\n', allAfterNewline: _*)) =>
164+
case (Vector(), Vector('\n', allAfterNewline@ _*)) =>
165165
// if there's only a newline after cursor on line, cut it
166166
append(Vector('\n'))
167167
(allBeforeCursor ++ allAfterNewline, c)

repl/test/dotty/tools/repl/CompilerTests.scala

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,50 @@ import org.junit.Assert._
55
import org.junit.Test
66

77
import dotc.core.Contexts.Context
8-
import dotc.reporting.MessageRendering
8+
import dotc.ast.Trees._
99
import dotc.ast.untpd
1010

1111
import results._
1212
import ReplTest._
1313

14-
class ReplCompilerTests extends ReplTest with MessageRendering {
14+
class ReplCompilerTests extends ReplTest {
1515

16-
@Test def compileSingle = withState {
17-
compiler.compile("def foo: 1 = 1").fold(onErrors, _ => ())
16+
@Test def compileSingle: Unit = fromInitialState { implicit state =>
17+
compiler.compile("def foo: 1 = 1").stateOrFail
1818
}
1919

20-
@Test def compileTwo = withState {
21-
val secondState =
22-
for {
23-
(_, s1) <- compiler.compile("def foo: 1 = 1")
24-
(_, s2) <- fromState(s1) {
25-
compiler.compile("def foo(i: Int): i.type = i")
26-
}
27-
} yield s2
2820

29-
secondState.fold(onErrors, { state =>
30-
assert(state.objectIndex == 2,
31-
s"Wrong object offset: expected 2 got ${state.objectIndex}")
32-
})
33-
}
21+
@Test def compileTwo =
22+
fromInitialState { implicit state =>
23+
compiler.compile("def foo: 1 = 1").stateOrFail
24+
}
25+
.andThen { implicit state =>
26+
val s2 = compiler.compile("def foo(i: Int): i.type = i").stateOrFail
27+
assert(s2.objectIndex == 2,
28+
s"Wrong object offset: expected 2 got ${s2.objectIndex}")
29+
}
3430

35-
@Test def inspectSingle = withState {
36-
val untpdTree = compiler.compile("def foo: 1 = 1").map((u,_) => u.untpdTree)
37-
38-
untpdTree.fold(
39-
onErrors,
40-
_ match {
41-
case untpd.PackageDef(_, List(mod: untpd.ModuleDef)) =>
42-
assert(mod.name.show == "rs$line$1", mod.name.show)
43-
case tree => fail(s"Unexpected structure: $tree")
44-
}
45-
)
46-
}
31+
@Test def inspectSingle =
32+
fromInitialState { implicit state =>
33+
val untpdTree = compiler.compile("def foo: 1 = 1").map(_._1.untpdTree)
34+
35+
untpdTree.fold(
36+
onErrors,
37+
_ match {
38+
case PackageDef(_, List(mod: untpd.ModuleDef)) =>
39+
implicit val ctx = state.run.runContext
40+
assert(mod.name.show == "rs$line$1", mod.name.show)
41+
case tree => fail(s"Unexpected structure: $tree")
42+
}
43+
)
44+
}
4745

48-
@Test def testVar = withState {
46+
@Test def testVar = fromInitialState { implicit state =>
4947
compile("var x = 5")
50-
assertEquals("var x: Int = 5\n", stripColor(storedOutput()))
48+
assertEquals("var x: Int = 5\n", storedOutput())
5149
}
5250

53-
@Test def testRes = withState {
51+
@Test def testRes = fromInitialState { implicit state =>
5452
compile {
5553
"""|def foo = 1 + 1
5654
|val x = 5 + 5
@@ -65,32 +63,33 @@ class ReplCompilerTests extends ReplTest with MessageRendering {
6563
"val res0: Int = 2",
6664
"var y: Int = 5")
6765

68-
val actual = storedOutput()
69-
expected === stripColor(actual).split("\n")
66+
expected === storedOutput().split("\n")
7067
}
7168

72-
@Test def testImportMutable = withState {
73-
fromState(compile("import scala.collection.mutable")) {
74-
assert(implicitly[State].imports.nonEmpty, "Didn't add import to `State` after compilation")
69+
@Test def testImportMutable =
70+
fromInitialState { implicit state =>
71+
compile("import scala.collection.mutable")
72+
}
73+
.andThen { implicit state =>
74+
assert(state.imports.nonEmpty, "Didn't add import to `State` after compilation")
7575

7676
compile("""mutable.Map("one" -> 1)""")
7777

7878
assertEquals(
7979
"val res0: scala.collection.mutable.Map[String, Int] = Map(one -> 1)\n",
80-
stripColor(storedOutput())
80+
storedOutput()
8181
)
8282
}
83-
}
8483

85-
@Test def rebindVariable = withState {
86-
fromState(compile("var x = 5")) {
84+
@Test def rebindVariable =
85+
fromInitialState { implicit s => compile("var x = 5") }
86+
.andThen { implicit s =>
8787
compile("x = 10")
8888
assertEquals(
8989
"""|var x: Int = 5
9090
|x: Int = 10
9191
|""".stripMargin,
92-
stripColor(storedOutput())
92+
storedOutput()
9393
)
9494
}
95-
}
9695
}

0 commit comments

Comments
 (0)