@@ -4,6 +4,7 @@ package repl
4
4
import java .io .{ File => JFile }
5
5
6
6
import dotc .ast .untpd
7
+ import dotc .ast .tpd
7
8
import dotc .{ CompilationUnit , Compiler }
8
9
import dotc .core .{ Phases , Decorators , Flags }
9
10
import Decorators ._ , Flags ._
@@ -12,35 +13,18 @@ import dotc.typer.FrontEnd
12
13
import backend .jvm .GenBCode
13
14
import dotc .core .Contexts .Context
14
15
import dotc .util .Positions ._
16
+ import dotc .reporting .diagnostic .MessageContainer
15
17
import dotc .reporting ._
16
18
import io ._
17
19
18
- class ReplCompiler (ictx : Context ) extends Compiler {
19
-
20
- ictx.base.initialize()(ictx)
20
+ class ReplTyper (ictx : Context ) extends Compiler {
21
21
22
22
type NextRes = Int
23
23
24
- /** A GenBCode phase that outputs to a virtual directory */
25
- private class REPLGenBCode extends GenBCode {
26
- override def phaseName = " replGenBCode"
27
-
28
- /** Directory to save class files to */
29
- private val virtualDirectory =
30
- if (ictx.settings.d.isDefault(ictx))
31
- new VirtualDirectory (" (memory)" , None )
32
- else
33
- new PlainDirectory (new Directory (new JFile (ictx.settings.d.value(ictx))))
34
-
35
- override def outputDir (implicit ctx : Context ) = virtualDirectory
36
- }
37
-
38
24
private class REPLFrontEnd extends FrontEnd {
39
25
override def phaseName = " replFrontEnd"
40
26
41
27
override def runOn (units : List [CompilationUnit ])(implicit ctx : Context ) = {
42
- for (unit <- units) do println(unit.untpdTree.show)
43
-
44
28
val unitContexts = for (unit <- units) yield ctx.fresh.setCompilationUnit(unit)
45
29
var remaining = unitContexts
46
30
while (remaining.nonEmpty) {
@@ -53,10 +37,7 @@ class ReplCompiler(ictx: Context) extends Compiler {
53
37
}
54
38
}
55
39
56
- override def phases = {
57
- val replPhases = Phases .replace(classOf [FrontEnd ], _ => new REPLFrontEnd :: Nil , super .phases)
58
- Phases .replace(classOf [GenBCode ], _ => new REPLGenBCode :: Nil , replPhases)
59
- }
40
+ override def phases = List (new REPLFrontEnd :: Nil )
60
41
61
42
def freeToAssigned (trees : Seq [untpd.Tree ], currentRes : Int )
62
43
(implicit ctx : Context ): (NextRes , Seq [untpd.Tree ]) = {
@@ -82,7 +63,18 @@ class ReplCompiler(ictx: Context) extends Compiler {
82
63
.withPos(Position (trees.head.pos.start, trees.last.pos.end))
83
64
}
84
65
85
- def compile (parsed : Parsed , currentRes : Int )(implicit ctx : Context ): NextRes = {
66
+ def extractStats (tree : tpd.Tree )(implicit ctx : Context ): Seq [tpd.Tree ] =
67
+ tree match {
68
+ case tpd.TypeDef (_, tpl : tpd.Template ) =>
69
+ tpl.body.collect { case tree : tpd.Tree => tree }
70
+ case _ => Nil
71
+ }
72
+
73
+ sealed trait Result
74
+ case class TypedTrees (trees : Seq [tpd.Tree ]) extends Result
75
+ case class TypeErrors (msgs : Seq [MessageContainer ]) extends Result
76
+
77
+ def typeCheck (parsed : Parsed , currentRes : Int )(implicit ctx : Context ): (NextRes , Result ) = {
86
78
val reporter = new StoreReporter (null ) with UniqueMessagePositions with HideNonSensicalMessages
87
79
88
80
val unit = new CompilationUnit (new SourceFile (s " repl-run- $currentRes" , parsed.sourceCode))
@@ -92,6 +84,11 @@ class ReplCompiler(ictx: Context) extends Compiler {
92
84
val run = newRun(ctx.fresh.setReporter(reporter))
93
85
run.compileUnits(unit :: Nil )
94
86
95
- newRes
87
+ val errs = reporter.removeBufferedMessages
88
+ val res =
89
+ if (errs.isEmpty) TypedTrees (extractStats(unit.tpdTree))
90
+ else TypeErrors (errs)
91
+
92
+ (newRes, res)
96
93
}
97
94
}
0 commit comments