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