Skip to content

Add Dotty-style tree constructor DSL to reflect #6628

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
Jun 7, 2019
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
37 changes: 37 additions & 0 deletions library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,43 @@ trait TreeOps extends Core {
def pos(implicit ctx: Context): Position = kernel.Term_pos(self)
def underlyingArgument(implicit ctx: Context): Term = kernel.Term_underlyingArgument(self)
def underlying(implicit ctx: Context): Term = kernel.Term_underlying(self)

/** A unary apply node with given argument: `tree(arg)` */
def appliedTo(arg: Term)(implicit ctx: Context): Term =
appliedToArgs(arg :: Nil)

/** An apply node with given arguments: `tree(arg, args0, ..., argsN)` */
def appliedTo(arg: Term, args: Term*)(implicit ctx: Context): Term =
appliedToArgs(arg :: args.toList)

/** An apply node with given argument list `tree(args(0), ..., args(args.length - 1))` */
def appliedToArgs(args: List[Term])(implicit ctx: Context): Apply =
Apply(self, args)

/** The current tree applied to given argument lists:
* `tree (argss(0)) ... (argss(argss.length -1))`
*/
def appliedToArgss(argss: List[List[Term]])(implicit ctx: Context): Term =
((self: Term) /: argss)(Apply(_, _))

/** The current tree applied to (): `tree()` */
def appliedToNone(implicit ctx: Context): Apply = appliedToArgs(Nil)

/** The current tree applied to given type argument: `tree[targ]` */
def appliedToType(targ: Type)(implicit ctx: Context): Term =
appliedToTypes(targ :: Nil)

/** The current tree applied to given type arguments: `tree[targ0, ..., targN]` */
def appliedToTypes(targs: List[Type])(implicit ctx: Context): Term =
appliedToTypeTrees(targs map (Inferred(_)))

/** The current tree applied to given type argument list: `tree[targs(0), ..., targs(targs.length - 1)]` */
def appliedToTypeTrees(targs: List[TypeTree])(implicit ctx: Context): Term =
if (targs.isEmpty) self else TypeApply(self, targs)

/** A select node that selects the given symbol.
*/
def select(sym: Symbol)(implicit ctx: Context): Select = Select(self, sym)
}

object IsTerm {
Expand Down
46 changes: 46 additions & 0 deletions tests/run-macros/reflect-dsl/assert_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import scala.quoted._
import scala.tasty._

object scalatest {

inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition, '{""}) }

def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = {
import refl._
import util._

def isImplicitMethodType(tp: Type): Boolean =
Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty

cond.unseal.underlyingArgument match {
case t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
let(lhs) { left =>
let(rhs) { right =>
val app = left.select(sel.symbol).appliedTo(right)
let(app) { result =>
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal.cast[Unit]
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = qual.appliedTo(left).select(sel.symbol).appliedTo(right)
let(Apply(app, implicits)) { result =>
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal.cast[Unit]
}
}

}
29 changes: 29 additions & 0 deletions tests/run-macros/reflect-dsl/test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
object Test {
import scalatest._

case class Box[T](v: T) {
def >(that: Box[T]): Boolean = this == that
}

trait EqInt
implicit val eq: EqInt = new EqInt {}

implicit class AnyOps[T](x: T) {
def === (y: T)(implicit c: EqInt) = x == y
}

def main(args: Array[String]): Unit = {
val a = Box(Some(10))
val five: Float = 5.0f
val six: Double = 6.0
val ten: Int = 10
assert(a.v === Some(10))
assert(five < six)
assert(five > 4)
assert(ten > 5)
assert(six < 7)
assert(six > 5L)
assert(Box(6) > Box(6))
assert(Box("h") > Box("h"))
}
}