@@ -35,14 +35,17 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
35
35
import Interpreter ._
36
36
import tpd ._
37
37
38
+ /** Local variable environment */
38
39
type Env = Map [Symbol , Object ]
40
+ def emptyEnv : Env = Map .empty
41
+ inline def env (using e : Env ): e.type = e
39
42
40
43
/** Returns the result of interpreting the code in the tree.
41
44
* Return Some of the result or None if the result type is not consistent with the expected type.
42
45
* Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
43
46
*/
44
- final def interpret [T ](tree : Tree )(implicit ct : ClassTag [T ]): Option [T ] =
45
- interpretTree(tree)(Map .empty ) match {
47
+ final def interpret [T ](tree : Tree )(using ct : ClassTag [T ]): Option [T ] =
48
+ interpretTree(tree)(using emptyEnv ) match {
46
49
case obj : T => Some (obj)
47
50
case obj =>
48
51
// TODO upgrade to a full type tag check or something similar
@@ -53,7 +56,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
53
56
/** Returns the result of interpreting the code in the tree.
54
57
* Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
55
58
*/
56
- protected def interpretTree (tree : Tree )(implicit env : Env ): Object = tree match {
59
+ protected def interpretTree (tree : Tree )(using Env ): Object = tree match {
57
60
case Literal (Constant (value)) =>
58
61
interpretLiteral(value)
59
62
@@ -135,26 +138,26 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
135
138
Nil
136
139
}
137
140
138
- private def interpretBlock (stats : List [Tree ], expr : Tree )(implicit env : Env ) = {
141
+ private def interpretBlock (stats : List [Tree ], expr : Tree )(using Env ) = {
139
142
var unexpected : Option [Object ] = None
140
- val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match {
143
+ val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match
141
144
case stat : ValDef =>
142
- accEnv.updated(stat.symbol, interpretTree(stat.rhs)(accEnv))
145
+ accEnv.updated(stat.symbol, interpretTree(stat.rhs)(using accEnv))
143
146
case stat =>
144
147
if (unexpected.isEmpty)
145
148
unexpected = Some (unexpectedTree(stat))
146
149
accEnv
147
- } )
148
- unexpected.getOrElse(interpretTree(expr)(newEnv))
150
+ )
151
+ unexpected.getOrElse(interpretTree(expr)(using newEnv))
149
152
}
150
153
151
- private def interpretLiteral (value : Any )( implicit env : Env ) : Object =
154
+ private def interpretLiteral (value : Any ): Object =
152
155
value.asInstanceOf [Object ]
153
156
154
- private def interpretVarargs (args : List [Object ])( implicit env : Env ) : Object =
157
+ private def interpretVarargs (args : List [Object ]): Object =
155
158
args.toSeq
156
159
157
- private def interpretedStaticMethodCall (moduleClass : Symbol , fn : Symbol )( implicit env : Env ) : List [Object ] => Object = {
160
+ private def interpretedStaticMethodCall (moduleClass : Symbol , fn : Symbol ): List [Object ] => Object = {
158
161
val (inst, clazz) =
159
162
try
160
163
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE ))
@@ -174,22 +177,22 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
174
177
(args : List [Object ]) => stopIfRuntimeException(method.invoke(inst, args : _* ), method)
175
178
}
176
179
177
- private def interpretedStaticFieldAccess (sym : Symbol )( implicit env : Env ) : Object = {
180
+ private def interpretedStaticFieldAccess (sym : Symbol ): Object = {
178
181
val clazz = loadClass(sym.owner.fullName.toString)
179
182
val field = clazz.getField(sym.name.toString)
180
183
field.get(null )
181
184
}
182
185
183
- private def interpretModuleAccess (fn : Symbol )( implicit env : Env ) : Object =
186
+ private def interpretModuleAccess (fn : Symbol ): Object =
184
187
loadModule(fn.moduleClass)
185
188
186
- private def interpretNew (fn : Symbol , args : => List [Object ])( implicit env : Env ) : Object = {
189
+ private def interpretNew (fn : Symbol , args : => List [Object ]): Object = {
187
190
val clazz = loadClass(fn.owner.fullName.toString)
188
191
val constr = clazz.getConstructor(paramsSig(fn): _* )
189
192
constr.newInstance(args : _* ).asInstanceOf [Object ]
190
193
}
191
194
192
- private def unexpectedTree (tree : Tree )( implicit env : Env ) : Object =
195
+ private def unexpectedTree (tree : Tree ): Object =
193
196
throw new StopInterpretation (" Unexpected tree could not be interpreted: " + tree, tree.srcPos)
194
197
195
198
private def loadModule (sym : Symbol ): Object =
@@ -209,7 +212,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
209
212
clazz.getConstructor().newInstance().asInstanceOf [Object ]
210
213
}
211
214
212
- private def loadReplLineClass (moduleClass : Symbol )( implicit env : Env ) : Class [? ] = {
215
+ private def loadReplLineClass (moduleClass : Symbol ): Class [? ] = {
213
216
val lineClassloader = new AbstractFileClassLoader (ctx.settings.outputDir.value, classLoader)
214
217
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
215
218
}
0 commit comments