Skip to content

Add minimal prototype for f interpolator macro #5532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/run-separate-compilation/f-interpolation-1/FQuote_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import scala.quoted._
import scala.tasty.Reflection

import scala.language.implicitConversions

object FQuote {

implicit class SCOps(ctx: StringContext) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm: I remember some time ago, we need to use => StringContext. I guess with underlyingArgument this is no longer required.

inline def ff(args: => Any*): String = ~impl('(this), '(args))
}

/*private*/ def impl(receiver: Expr[SCOps], args: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[String] = {
import reflect._

def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match {
case x :: xs =>
val head = x.seal[Any]
val tail = liftListOfAny(xs)
'{ ~head :: ~tail }
case Nil => '(Nil)
}

def isStringConstant(tree: Term) = tree match {
case Term.Literal(_) => true
case _ => false
}

def isSCOpsConversion(tree: Term) =
tree.symbol.fullName == "FQuote$.SCOps"

def isStringContextApply(tree: Term) =
tree.symbol.fullName == "scala.StringContext$.apply"

// FQuote.SCOps(StringContext.apply([p0, ...]: String*)
val parts = receiver.unseal.underlyingArgument match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: it seems the name underlyingArgument does not make sense here, as it's a receiver, not argument.

case Term.Apply(conv, List(Term.Apply(fun, List(Term.Typed(Term.Repeated(values), _)))))
if isSCOpsConversion(conv) &&
isStringContextApply(fun) &&
values.forall(isStringConstant) =>
values.collect { case Term.Literal(Constant.String(value)) => value }
case tree =>
throw new QuoteError(s"String literal expected, but ${tree.show} found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: better error message with positions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an example for the student to start

}

// [a0, ...]: Any*
val Term.Typed(Term.Repeated(allArgs), _) = args.unseal.underlyingArgument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would it be possible to use args.unseal.underlying and leave args not by-name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I copied this basic implementation from an old version of the xml interpolator. There are many other improvements that can be done. This is just a simple example of one way to do it.


for ((arg, part) <- allArgs.zip(parts.tail)) {
if (part.startsWith("%d") && !(arg.tpe <:< definitions.IntType)) {
return '(s"`${~arg.showCode.toExpr}` is not of type Int")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an example for the student to start. It is a simple way to see the output.

}

}

val string = parts.mkString("")
'{ new collection.immutable.StringOps(~string.toExpr).format(~args: _*) }
}
}
10 changes: 10 additions & 0 deletions tests/run-separate-compilation/f-interpolation-1/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import FQuote._

object Test {
def main(args: Array[String]): Unit = {
val one: Int = 1
assert(ff"Hello $one%d!" == "Hello 1!")
val world: String = "world"
assert(ff"Hello $world%d!" == "`world` is not of type Int")
}
}