@@ -11,6 +11,7 @@ import dotc.util.SourceFile
11
11
import dotc .typer .FrontEnd
12
12
import backend .jvm .GenBCode
13
13
import dotc .core .Contexts .Context
14
+ import dotc .util .Positions ._
14
15
import dotc .reporting ._
15
16
import io ._
16
17
@@ -57,35 +58,36 @@ class ReplCompiler(ictx: Context) extends Compiler {
57
58
Phases .replace(classOf [GenBCode ], _ => new REPLGenBCode :: Nil , replPhases)
58
59
}
59
60
60
- def wrapped (trees : Seq [untpd.Tree ], currentRes : Int )(implicit ctx : Context ): (NextRes , untpd.ModuleDef ) = {
61
+ def freeToAssigned (trees : Seq [untpd.Tree ], currentRes : Int )
62
+ (implicit ctx : Context ): (NextRes , Seq [untpd.Tree ]) = {
61
63
import untpd ._
64
+
62
65
def freeExpression (t : Tree ) =
63
66
t.isTerm && ! t.isInstanceOf [Assign ]
64
67
65
68
val (exps, other) = trees.partition(freeExpression)
66
-
67
69
val resX = exps.zipWithIndex.map { (exp, i) =>
68
70
ValDef (s " res ${i + currentRes}" .toTermName, TypeTree (), exp)
71
+ .withPos(exp.pos)
69
72
}
70
73
71
- val moduleIndex = resX.length + currentRes
72
- val module = {
73
- val stats = if (trees.isEmpty) List (EmptyTree ) else resX ++ other
74
- val tmpl = Template (emptyConstructor, Nil , EmptyValDef , stats)
75
- val moduleName = s " EncapsulatedRes $moduleIndex" .toTermName
74
+ (currentRes + resX.length, resX ++ other)
75
+ }
76
76
77
- ModuleDef (moduleName, tmpl).withFlags( Module )
78
- }
77
+ def wrapped ( trees : Seq [untpd. Tree ])( implicit ctx : Context ) : untpd. TypeDef = {
78
+ import untpd . _
79
79
80
- (moduleIndex + 1 , module)
80
+ val tmpl = Template (emptyConstructor, Nil , EmptyValDef , trees)
81
+ TypeDef (" TypeCheckingContext" .toTypeName, tmpl)
82
+ .withPos(Position (trees.head.pos.start, trees.last.pos.end))
81
83
}
82
84
83
85
def compile (parsed : Parsed , currentRes : Int )(implicit ctx : Context ): NextRes = {
84
86
val reporter = new StoreReporter (null ) with UniqueMessagePositions with HideNonSensicalMessages
85
87
86
88
val unit = new CompilationUnit (new SourceFile (s " repl-run- $currentRes" , parsed.sourceCode))
87
- val (newRes, tree ) = wrapped (parsed.trees, currentRes)
88
- unit.untpdTree = tree
89
+ val (newRes, trees ) = freeToAssigned (parsed.trees, currentRes)
90
+ unit.untpdTree = wrapped(trees)
89
91
90
92
val run = newRun(ctx.fresh.setReporter(reporter))
91
93
run.compileUnits(unit :: Nil )
0 commit comments