mercredi 30 juin 2021

Parsing scala 3 code from a String into Scala 3 AST at runtime

My goal is to get Scala 3 code as a String and to parse it into Abstract Syntax Tree for Scala 3 at runtime. In the process if the code has compilation errors, I should get that as part of some exception. The larger goal is to end up with Expr[T] if the scala code is valid and execute it by splicing in the right bits(I have this part covered).

This was doable in Scala 2.* using scala-reflect here.

val source =
  """
    |object HelloWorld {
    |  def main(args: Array[String]): Unit = {
    |    println("Hello, world!")
    |  }
    |}
    |
    |HelloWorld.main(Array())
    |""".stripMargin
val tree = toolbox.parse(source)
val binary = toolbox.compile(tree)
binary()

But as far as I can surmise, in Scala 3, scala-reflect will not be ported. How could I achieve the same in Scala 3?

Some relevant links here and here





Aucun commentaire:

Enregistrer un commentaire