@@ -33,7 +33,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
3333
3434 /** A GenBCode phase that outputs to a virtual directory */
3535 private class REPLGenBCode extends GenBCode {
36- override def phaseName = " replGenBCode "
36+ override def phaseName = " genBCode "
3737 override def outputDir (implicit ctx : Context ) = directory
3838 }
3939
@@ -51,6 +51,25 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
5151 )
5252 }
5353
54+ def newRun (initCtx : Context , objectIndex : Int ) = new Run (this , initCtx) {
55+ override protected [this ] def rootContext (implicit ctx : Context ) =
56+ addMagicImports(super .rootContext.fresh.setReporter(storeReporter))
57+
58+ private def addMagicImports (initCtx : Context ): Context = {
59+ def addImport (path : TermName )(implicit ctx : Context ) = {
60+ val importInfo = ImportInfo .rootImport { () =>
61+ ctx.requiredModuleRef(path)
62+ }
63+ ctx.fresh.setNewScope.setImportInfo(importInfo)
64+ }
65+
66+ (1 to objectIndex)
67+ .foldLeft(addImport(" dotty.Show" .toTermName)(initCtx)) { (ictx, i) =>
68+ addImport(nme.EMPTY_PACKAGE ++ " ." ++ objectNames(i))(ictx)
69+ }
70+ }
71+ }
72+
5473 private [this ] var objectNames = Map .empty[Int , TermName ]
5574 private def objectName (state : State ) =
5675 objectNames.get(state.objectIndex).getOrElse {
@@ -59,9 +78,9 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
5978 newName
6079 }
6180
62- sealed case class Definitions (stats : List [untpd.Tree ], state : State )
81+ private case class Definitions (stats : List [untpd.Tree ], state : State )
6382
64- def definitions (trees : List [untpd.Tree ], state : State ): Result [ Definitions ] = {
83+ private def definitions (trees : List [untpd.Tree ], state : State ): Definitions = {
6584 import untpd ._
6685
6786 implicit val ctx : Context = state.run.runContext
@@ -80,7 +99,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
8099
81100 def createPatDefShows (patDef : PatDef ) = {
82101 def createDeepShows (tree : untpd.Tree ) = {
83- object PatFolder extends UntypedDeepFolder [List [DefDef ]] (
102+ class PatFolder extends UntypedDeepFolder [List [DefDef ]] (
84103 (acc, tree) => tree match {
85104 case Ident (name) if name.isVariableName && name != nme.WILDCARD =>
86105 createShow(name.toTermName, tree.pos) :: acc
@@ -90,7 +109,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
90109 acc
91110 }
92111 )
93- PatFolder .apply(Nil , tree).reverse
112+ ( new PatFolder ) .apply(Nil , tree).reverse
94113 }
95114
96115 // cannot fold over the whole tree because we need to generate show methods
@@ -130,12 +149,12 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
130149 }
131150
132151 Definitions (
133- state.imports.map(_._1) ++ defs,
152+ state.imports ++ defs,
134153 state.copy(
135154 objectIndex = state.objectIndex + (if (defs.isEmpty) 0 else 1 ),
136155 valIndex = valIdx
137156 )
138- ).result
157+ )
139158 }
140159
141160 /** Wrap trees in an object and add imports from the previous compilations
@@ -153,65 +172,40 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
153172 * }
154173 * ```
155174 */
156- def wrapped (defs : Definitions , sourceCode : String ): untpd.PackageDef = {
175+ private def wrapped (defs : Definitions ): untpd.PackageDef = {
157176 import untpd ._
158177
159- implicit val ctx : Context = defs.state.run.runContext
178+ assert( defs.stats.nonEmpty)
160179
161- val module = {
162- val tmpl = Template (emptyConstructor(ctx), Nil , EmptyValDef , defs.stats)
163- List (
164- ModuleDef (objectName(defs.state), tmpl)
165- .withMods(new Modifiers (Module | Final ))
166- .withPos(Position (0 , sourceCode.length))
167- )
168- }
180+ implicit val ctx : Context = defs.state.run.runContext
169181
170- PackageDef (Ident (nme.EMPTY_PACKAGE ), module)
171- }
182+ val tmpl = Template (emptyConstructor, Nil , EmptyValDef , defs.stats)
183+ val module = ModuleDef (objectName(defs.state), tmpl)
184+ .withMods(new Modifiers (Module | Final ))
185+ .withPos(Position (0 , defs.stats.last.pos.end))
172186
173- def newRun (initCtx : Context , objectIndex : Int ) = new Run (this , initCtx) {
174- override protected [this ] def rootContext (implicit ctx : Context ) =
175- addMagicImports(super .rootContext.fresh.setReporter(storeReporter), objectIndex)
187+ PackageDef (Ident (nme.EMPTY_PACKAGE ), List (module))
176188 }
177189
178- def createUnit (defs : Definitions , sourceCode : String ): Result [ CompilationUnit ] = {
190+ private def createUnit (defs : Definitions , sourceCode : String ): CompilationUnit = {
179191 val unit = new CompilationUnit (new SourceFile (objectName(defs.state).toString, sourceCode))
180- unit.untpdTree = wrapped(defs, sourceCode )
181- unit.result
192+ unit.untpdTree = wrapped(defs)
193+ unit
182194 }
183195
184- def runCompilation (unit : CompilationUnit , state : State ): Result [State ] = {
196+ private def runCompilationUnit (unit : CompilationUnit , state : State ): Result [( CompilationUnit , State ) ] = {
185197 val run = state.run
186198 val reporter = state.run.runContext.reporter
187199 run.compileUnits(unit :: Nil )
188200
189- if (! reporter.hasErrors) state.result
201+ if (! reporter.hasErrors) (unit, state) .result
190202 else run.runContext.flushBufferedMessages().errors
191203 }
192204
193205 def compile (parsed : Parsed )(implicit state : State ): Result [(CompilationUnit , State )] = {
194- for {
195- defs <- definitions(parsed.trees, state)
196- unit <- createUnit(defs, parsed.sourceCode)
197- state <- runCompilation(unit, defs.state)
198- } yield (unit, state)
199- }
200-
201- private [this ] def addMagicImports (initCtx : Context , objectIndex : Int ): Context = {
202- def addImport (path : TermName )(implicit ctx : Context ) = {
203- val ref = tpd.ref(ctx.requiredModuleRef(path.toTermName))
204- val symbol = ctx.newImportSymbol(ctx.owner, ref)
205- val importInfo =
206- new ImportInfo (implicit ctx => symbol, untpd.Ident (nme.WILDCARD ) :: Nil , None )
207- ctx.fresh.setNewScope.setImportInfo(importInfo)
208- }
209-
210- List
211- .range(1 , objectIndex + 1 )
212- .foldLeft(addImport(" dotty.Show" .toTermName)(initCtx)) { (ictx, i) =>
213- addImport(nme.EMPTY_PACKAGE ++ " ." ++ objectNames(i))(ictx)
214- }
206+ val defs = definitions(parsed.trees, state)
207+ val unit = createUnit(defs, parsed.sourceCode)
208+ runCompilationUnit(unit, defs.state)
215209 }
216210
217211 def typeOf (expr : String )(implicit state : State ): Result [String ] =
@@ -223,7 +217,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
223217 case _ =>
224218 """ Couldn't compute the type of your expression, so sorry :(
225219 |
226- |Please report this to my masters at Github .com/lampepfl/dotty
220+ |Please report this to my masters at github .com/lampepfl/dotty
227221 """ .stripMargin
228222 }
229223 }
@@ -240,7 +234,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
240234 val tmpl = Template (emptyConstructor,
241235 List (Ident (tpnme.Any )),
242236 EmptyValDef ,
243- state.imports.map(_._1) :+ valdef)
237+ state.imports :+ valdef)
244238
245239 PackageDef (Ident (nme.EMPTY_PACKAGE ),
246240 TypeDef (" EvaluateExpr" .toTypeName, tmpl)
@@ -286,7 +280,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
286280 val src = new SourceFile (s " EvaluateExpr " , expr)
287281 val runCtx =
288282 run.runContext.fresh
289- .setSetting(run.runContext.settings.YstopAfter , List (" replFrontEnd " ))
283+ .setSetting(run.runContext.settings.YstopAfter , List (" frontend " ))
290284
291285 wrapped(expr, src, state)(runCtx).flatMap { pkg =>
292286 val unit = new CompilationUnit (src)
0 commit comments