@@ -67,20 +67,20 @@ class ReplCompiler(ictx: Context) extends Compiler {
67
67
)
68
68
}
69
69
70
- def freeToAssigned (trees : Seq [untpd.Tree ], state : State )
71
- (implicit ctx : Context ): (State , Seq [untpd.Tree ]) = {
70
+ def freeToAssigned (trees : Seq [untpd.Tree ], state : State ): (State , Seq [untpd.Tree ]) = {
72
71
import untpd ._
72
+ implicit val ctx = state.ictx
73
73
74
74
def freeExpression (t : Tree ) =
75
75
t.isTerm && ! t.isInstanceOf [Assign ]
76
76
77
77
val (exps, other) = trees.partition(freeExpression)
78
78
val resX = exps.zipWithIndex.map { (exp, i) =>
79
- ValDef (s " res ${i + state.freeValues }" .toTermName, TypeTree (), exp)
79
+ ValDef (s " res ${i + state.valIndex }" .toTermName, TypeTree (), exp)
80
80
.withPos(exp.pos)
81
81
}
82
82
83
- (state.copy(freeValues = state.freeValues + resX.length), resX ++ other)
83
+ (state.copy(valIndex = state.valIndex + resX.length), resX ++ other)
84
84
}
85
85
86
86
def wrapped (trees : Seq [untpd.Tree ], nextId : Int )(implicit ctx : Context ): untpd.PackageDef = {
@@ -93,28 +93,39 @@ class ReplCompiler(ictx: Context) extends Compiler {
93
93
94
94
val tmpl = Template (emptyConstructor, Nil , EmptyValDef , imports ++ trees)
95
95
PackageDef (Ident (nme.NO_NAME ),
96
- ModuleDef (( " ReplSession$" + nextId) .toTermName, tmpl)
96
+ ModuleDef (s " ReplSession $$ $ nextId" .toTermName, tmpl)
97
97
.withMods(new Modifiers (Module | Final ))
98
98
.withPos(Position (trees.head.pos.start, trees.last.pos.end)) :: Nil
99
99
)
100
100
}
101
101
102
- def compile (parsed : Parsed , state : State )(implicit ctx : Context ): Result [State ] = {
103
- val reporter = new StoreReporter (null ) with UniqueMessagePositions with HideNonSensicalMessages
104
-
105
- val unit = new CompilationUnit (new SourceFile (" ReplSession$" + (state.objects + 1 ), parsed.sourceCode))
106
- val (stateAfterAssign, trees) = freeToAssigned(parsed.trees, state)
107
- unit.untpdTree = wrapped(trees, 0 )
102
+ def createUnit (trees : Seq [untpd.Tree ], objectIndex : Int , sourceCode : String )(implicit ctx : Context ): Result [CompilationUnit ] = {
103
+ val unit = new CompilationUnit (new SourceFile (s " ReplsSession $$ $objectIndex" , sourceCode))
104
+ unit.untpdTree = wrapped(trees, objectIndex)
105
+ unit
106
+ }
108
107
108
+ def runCompilation (unit : CompilationUnit , state : State ): Result [State ] = {
109
+ implicit val ctx = state.ictx
110
+ val reporter = new StoreReporter (null ) with UniqueMessagePositions with HideNonSensicalMessages
109
111
val run = newRun(ctx.fresh.setReporter(reporter))
110
112
run.compileUnits(unit :: Nil )
111
113
112
114
val errs = reporter.removeBufferedMessages
113
115
if (errs.isEmpty) state.copy(
114
- freeValues = stateAfterAssign.freeValues ,
115
- objects = state.objects + 1 ,
116
- ictx = run.runContext
116
+ objectIndex = state.objectIndex + 1 ,
117
+ valIndex = state.valIndex ,
118
+ ictx = run.runContext
117
119
)
118
120
else Errors (errs)
119
121
}
122
+
123
+ def compile (parsed : Parsed , state : State ): Result [State ] = {
124
+ implicit val ctx = state.ictx
125
+ for {
126
+ stateAndTrees <- freeToAssigned(parsed.trees, state)
127
+ unit <- createUnit(stateAndTrees._2, state.objectIndex, parsed.sourceCode)
128
+ state <- runCompilation(unit, stateAndTrees._1)
129
+ } yield state
130
+ }
120
131
}
0 commit comments