@@ -36,14 +36,17 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
3636 import Interpreter ._
3737 import tpd ._
3838
39+ /** Local variable environment */
3940 type Env = Map [Symbol , Object ]
41+ def emptyEnv : Env = Map .empty
42+ inline def env (using e : Env ): e.type = e
4043
4144 /** Returns the result of interpreting the code in the tree.
4245 * Return Some of the result or None if the result type is not consistent with the expected type.
4346 * Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
4447 */
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 {
4750 case obj : T => Some (obj)
4851 case obj =>
4952 // TODO upgrade to a full type tag check or something similar
@@ -54,7 +57,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
5457 /** Returns the result of interpreting the code in the tree.
5558 * Throws a StopInterpretation if the tree could not be interpreted or a runtime exception ocurred.
5659 */
57- protected def interpretTree (tree : Tree )(implicit env : Env ): Object = tree match {
60+ protected def interpretTree (tree : Tree )(using Env ): Object = tree match {
5861 case Literal (Constant (value)) =>
5962 interpretLiteral(value)
6063
@@ -136,26 +139,26 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
136139 Nil
137140 }
138141
139- private def interpretBlock (stats : List [Tree ], expr : Tree )(implicit env : Env ) = {
142+ private def interpretBlock (stats : List [Tree ], expr : Tree )(using Env ) = {
140143 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
142145 case stat : ValDef =>
143- accEnv.updated(stat.symbol, interpretTree(stat.rhs)(accEnv))
146+ accEnv.updated(stat.symbol, interpretTree(stat.rhs)(using accEnv))
144147 case stat =>
145148 if (unexpected.isEmpty)
146149 unexpected = Some (unexpectedTree(stat))
147150 accEnv
148- } )
149- unexpected.getOrElse(interpretTree(expr)(newEnv))
151+ )
152+ unexpected.getOrElse(interpretTree(expr)(using newEnv))
150153 }
151154
152- private def interpretLiteral (value : Any )( implicit env : Env ) : Object =
155+ private def interpretLiteral (value : Any ): Object =
153156 value.asInstanceOf [Object ]
154157
155- private def interpretVarargs (args : List [Object ])( implicit env : Env ) : Object =
158+ private def interpretVarargs (args : List [Object ]): Object =
156159 args.toSeq
157160
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 = {
159162 val (inst, clazz) =
160163 try
161164 if (moduleClass.name.startsWith(str.REPL_SESSION_LINE ))
@@ -175,22 +178,22 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
175178 (args : List [Object ]) => stopIfRuntimeException(method.invoke(inst, args : _* ), method)
176179 }
177180
178- private def interpretedStaticFieldAccess (sym : Symbol )( implicit env : Env ) : Object = {
181+ private def interpretedStaticFieldAccess (sym : Symbol ): Object = {
179182 val clazz = loadClass(sym.owner.fullName.toString)
180183 val field = clazz.getField(sym.name.toString)
181184 field.get(null )
182185 }
183186
184- private def interpretModuleAccess (fn : Symbol )( implicit env : Env ) : Object =
187+ private def interpretModuleAccess (fn : Symbol ): Object =
185188 loadModule(fn.moduleClass)
186189
187- private def interpretNew (fn : Symbol , args : => List [Object ])( implicit env : Env ) : Object = {
190+ private def interpretNew (fn : Symbol , args : => List [Object ]): Object = {
188191 val clazz = loadClass(fn.owner.fullName.toString)
189192 val constr = clazz.getConstructor(paramsSig(fn): _* )
190193 constr.newInstance(args : _* ).asInstanceOf [Object ]
191194 }
192195
193- private def unexpectedTree (tree : Tree )( implicit env : Env ) : Object =
196+ private def unexpectedTree (tree : Tree ): Object =
194197 throw new StopInterpretation (em " Unexpected tree could not be interpreted: ${tree.toString}" , tree.srcPos)
195198
196199 private def loadModule (sym : Symbol ): Object =
@@ -210,7 +213,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
210213 clazz.getConstructor().newInstance().asInstanceOf [Object ]
211214 }
212215
213- private def loadReplLineClass (moduleClass : Symbol )( implicit env : Env ) : Class [? ] = {
216+ private def loadReplLineClass (moduleClass : Symbol ): Class [? ] = {
214217 val lineClassloader = new AbstractFileClassLoader (ctx.settings.outputDir.value, classLoader)
215218 lineClassloader.loadClass(moduleClass.name.firstPart.toString)
216219 }
0 commit comments