Skip to content

Commit

Permalink
refactor: in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
d01c2 committed Feb 10, 2025
1 parent 54da42e commit e3fc46a
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/main/scala/esmeta/phase/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,62 @@ package esmeta.phase
import esmeta.*
import esmeta.cfg.CFG
import esmeta.interpreter.*
import esmeta.ty.{*, given}
import esmeta.state.*
import esmeta.util.*
import esmeta.util.SystemUtils.*
import esmeta.es.*
import scala.collection.mutable.{Map => MMap}

/** `eval` phase */
case object Eval extends Phase[CFG, State] {
val name = "eval"
val help = "evaluates an ECMAScript file."

var totalErrors: MMap[TypeError, Set[String]] = MMap()

def apply(
cfg: CFG,
cmdConfig: CommandConfig,
config: Config,
): State =
if (config.multiple)
if (config.multiple) {
var st = State(cfg, Context(cfg.main))
for {
path <- cmdConfig.targets
file <- walkTree(path)
filename = file.toString
if jsFilter(filename)
} st = run(cfg, config, filename, config.tyCheck)
} st = run(cfg, config, filename)
if (config.tyCheck)
val pw = getPrintWriter(EVAL_LOG_DIR)
val sorted: Vector[TypeError] =
totalErrors.iterator.toVector
.sortBy { case (_, tests) => -tests.size }
.map(_._1)
pw.println(s"${sorted.length} type errors detected." + LINE_SEP)
for { error <- sorted } do {
pw.println(error)
pw.println(s"- Found in ${totalErrors(error).size} file(s)")
val sample = totalErrors(error).head
pw.println(s" - sample: ${sample}")
pw.println(LINE_SEP)
pw.flush
}
st
else
run(cfg, config, getFirstFilename(cmdConfig, this.name), config.tyCheck)
} else run(cfg, config, getFirstFilename(cmdConfig, this.name))

def run(cfg: CFG, config: Config, filename: String, tyCheck: Boolean): State =
if (tyCheck)
def run(cfg: CFG, config: Config, filename: String): State =
if (config.tyCheck) {
val (finalSt, errors) = TypeChecker(cfg.init.fromFile(filename))
for (error <- errors)
println(error)
println(LINE_SEP)
if (config.multiple) {
for { error <- errors } do {
val updated = totalErrors.getOrElse(error, Set()) + filename
totalErrors += error -> updated
}
} else for (error <- errors) println(error.toString + LINE_SEP)
finalSt
else
} else
Interpreter(
cfg.init.fromFile(filename),
log = config.log,
Expand Down

0 comments on commit e3fc46a

Please sign in to comment.