<commit_before>package dotty.tools
package repl

import java.io.{ File => JFile }

import dotc.ast.{ untpd, tpd }
import dotc.{ Run, CompilationUnit, Compiler }
import dotc.core.Decorators._, dotc.core.Flags._, dotc.core.Phases
import dotc.core.Names._, dotc.core.Contexts._, dotc.core.StdNames._
import dotc.util.SourceFile
import dotc.typer.{ ImportInfo, FrontEnd }
import backend.jvm.GenBCode
import dotc.core.NameOps._
import dotc.util.Positions._
import dotc.reporting.diagnostic.{ messages, MessageContainer }
import dotc.reporting._
import io._

import results._

/** This subclass of `Compiler` replaces the appropriate phases in order to
 *  facilitate the REPL
 *
 *  Specifically it replaces the front end with `REPLFrontEnd`, and adds a
 *  custom subclass of `GenBCode`. The custom `GenBCode`, `REPLGenBCode`, works
 *  in conjunction with a specialized class loader in order to load virtual
 *  classfiles.
 */
class ReplCompiler(val directory: AbstractFile) extends Compiler {


  /** A GenBCode phase that outputs to a virtual directory */
  private class REPLGenBCode extends GenBCode {
    override def phaseName = "replGenBCode"
    override def outputDir(implicit ctx: Context) = directory
  }

  override def phases = {
    val replacedFrontend = Phases.replace(
      classOf[FrontEnd],
      _ => new REPLFrontEnd :: Nil,
      super.phases
    )

    Phases.replace(
      classOf[GenBCode],
      _ => new REPLGenBCode :: Nil,
      replacedFrontend
    )
  }

  private[this] var objectNames = Map.empty[Int, TermName]
  private def objectName(state: State) =
    objectNames.get(state.objectIndex).getOrElse {
      val newName = (str.REPL_SESSION_LINE + state.objectIndex).toTermName
      objectNames = objectNames + (state.objectIndex -> newName)
      newName
    }

  sealed case class Definitions(stats: List[untpd.Tree], state: State)

  def definitions(trees: List[untpd.Tree], state: State): Result[Definitions] = {
    import untpd._

    implicit val ctx: Context = state.run.runContext

    def createShow(name: TermName, pos: Position) = {
      val showName = name ++ "Show"
      val select = Select(Ident(name), "show".toTermName)
      ValDef(showName, TypeTree(), select).withFlags(Synthetic).withPos(pos)
    }

    val (exps, other) = trees.partition(_.isTerm)
    val resX = exps.zipWithIndex.flatMap { (exp, i) =>
      val resName = (str.REPL_RES_PREFIX + (i + state.valIndex)).toTermName
      val show = createShow(resName, exp.pos)

      exp match {
        case exp @ Assign(id: Ident, rhs) =>
          val assignName = (id.name ++ str.REPL_ASSIGN_SUFFIX).toTermName
          val assign = ValDef(assignName, TypeTree(), id).withPos(exp.pos)

          exp :: assign :: createShow(assignName, exp.pos) :: Nil
        case _ =>
          List(ValDef(resName, TypeTree(), exp).withPos(exp.pos), show)
      }
    }

    val othersWithShow = other.flatMap {
      case t: ValDef =>
        List(t, createShow(t.name, t.pos))
      case t: PatDef => {
        val variables = t.pats.flatMap {
          case Ident(name) if name != nme.WILDCARD => List((name.toTermName, t.pos))
          case pat =>
            new UntypedDeepFolder[List[(TermName, Position)]](
            (acc: List[(TermName, Position)], t: Tree) => t match {
              case _: BackquotedIdent =>
                acc
              case Ident(name) if name.isVariableName && name.toString != "_" =><commit_msg>Make the show member a `DefDef`<commit_after>package dotty.tools
package repl

import java.io.{ File => JFile }

import dotc.ast.{ untpd, tpd }
import dotc.{ Run, CompilationUnit, Compiler }
import dotc.core.Decorators._, dotc.core.Flags._, dotc.core.Phases
import dotc.core.Names._, dotc.core.Contexts._, dotc.core.StdNames._
import dotc.util.SourceFile
import dotc.typer.{ ImportInfo, FrontEnd }
import backend.jvm.GenBCode
import dotc.core.NameOps._
import dotc.util.Positions._
import dotc.reporting.diagnostic.{ messages, MessageContainer }
import dotc.reporting._
import io._

import results._

/** This subclass of `Compiler` replaces the appropriate phases in order to
 *  facilitate the REPL
 *
 *  Specifically it replaces the front end with `REPLFrontEnd`, and adds a
 *  custom subclass of `GenBCode`. The custom `GenBCode`, `REPLGenBCode`, works
 *  in conjunction with a specialized class loader in order to load virtual
 *  classfiles.
 */
class ReplCompiler(val directory: AbstractFile) extends Compiler {


  /** A GenBCode phase that outputs to a virtual directory */
  private class REPLGenBCode extends GenBCode {
    override def phaseName = "replGenBCode"
    override def outputDir(implicit ctx: Context) = directory
  }

  override def phases = {
    val replacedFrontend = Phases.replace(
      classOf[FrontEnd],
      _ => new REPLFrontEnd :: Nil,
      super.phases
    )

    Phases.replace(
      classOf[GenBCode],
      _ => new REPLGenBCode :: Nil,
      replacedFrontend
    )
  }

  private[this] var objectNames = Map.empty[Int, TermName]
  private def objectName(state: State) =
    objectNames.get(state.objectIndex).getOrElse {
      val newName = (str.REPL_SESSION_LINE + state.objectIndex).toTermName
      objectNames = objectNames + (state.objectIndex -> newName)
      newName
    }

  sealed case class Definitions(stats: List[untpd.Tree], state: State)

  def definitions(trees: List[untpd.Tree], state: State): Result[Definitions] = {
    import untpd._

    implicit val ctx: Context = state.run.runContext

    def createShow(name: TermName, pos: Position) = {
      val showName = name ++ "Show"
      val select = Select(Ident(name), "show".toTermName)
      DefDef(showName, Nil, Nil, TypeTree(), select).withFlags(Synthetic).withPos(pos)
    }

    val (exps, other) = trees.partition(_.isTerm)
    val resX = exps.zipWithIndex.flatMap { (exp, i) =>
      val resName = (str.REPL_RES_PREFIX + (i + state.valIndex)).toTermName
      val show = createShow(resName, exp.pos)

      exp match {
        case exp @ Assign(id: Ident, rhs) =>
          val assignName = (id.name ++ str.REPL_ASSIGN_SUFFIX).toTermName
          val assign = ValDef(assignName, TypeTree(), id).withPos(exp.pos)

          exp :: assign :: createShow(assignName, exp.pos) :: Nil
        case _ =>
          List(ValDef(resName, TypeTree(), exp).withPos(exp.pos), show)
      }
    }

    val othersWithShow = other.flatMap {
      case t: ValDef =>
        List(t, createShow(t.name, t.pos))
      case t: PatDef => {
        val variables = t.pats.flatMap {
          case Ident(name) if name != nme.WILDCARD => List((name.toTermName, t.pos))
          case pat =>
            new UntypedDeepFolder[List[(TermName, Position)]](
            (acc: List[(TermName, Position)], t: Tree) => t match {
              case _: BackquotedIdent =>
                acc
              case Ident(name) if name.isVariableName && name.toString != "_" =>
{'fraction_non_alphanumeric': 0.11333333333333333, 'fraction_numerical': 0.0, 'mean_word_length': 3.0689858490566038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3007799', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2146, 'n_words': 571}