11// Import Expr and some extension methods
22import scala .quoted ._
3+ import scala .quoted .staging ._
34import scala .tasty ._
45
56object Vectors {
67
78 // Needed to show quotes
8- implicit val toolbox : scala.quoted. Toolbox = scala.quoted. Toolbox .make
9+ given Toolbox = Toolbox .make(getClass.getClassLoader)
910
1011 /** Compute the dot product of the vectors (represented as arrays)
1112 * Returns (v1(0) * v2(0)) + (v1(1) * v2(1)) + ... + (v1(v1.length - 1) * v2(v2.length - 1))
1213 * Or throws an exception if v1.length != v2.length
1314 * Both arrays are assumed to be immutable.
1415 */
15- inline def dot (v1 : => Array [Int ], v2 : => Array [Int ]): Int = ~ dotImpl('(v1) , ' (v2))( Tasty .macroContext)
16+ inline def dot (v1 : => Array [Int ], v2 : => Array [Int ]): Int = $ { dotImpl(' {v1} , ' {v2})}
1617
1718 /** Generates code to compute the dot product.
1819 * Will try to partially evaluate any statically available data.
1920 */
20- def dotImpl (v1 : Expr [Array [Int ]], v2 : Expr [Array [Int ]])(reflect : Tasty ): Expr [Int ] = {
21- import reflect . _
21+ def dotImpl (v1 : Expr [Array [Int ]], v2 : Expr [Array [Int ]])(using qctx : QuoteContext ): Expr [Int ] = {
22+ import qctx . tasty .{ _ }
2223
2324 object EmptyArray {
2425 def unapply (arg : Tree ): Boolean = arg match {
25- case Term .Apply (Term .Apply (Term .TypeApply (Term .Select (Term .Ident (" Array" ), " apply" , _), List (TypeTree .Synthetic ())), List (Term .Typed (Term .Repeated (Nil ), TypeTree .Synthetic ()))), _) => true
26- case _ => false
26+ case Apply (Apply (TypeApply (Select (Ident (" Array" ), " apply" ),List (Inferred ())),List (Typed (Repeated (Nil ,Inferred ()),Inferred ()))),_) =>
27+ true
28+ case t =>
29+ false
2730 }
2831 }
2932
3033 // Useful methods
3134 // Use i.toExpr to lift an i:Int into an quoted.Expr[Int]
32- // Use q.toTasty to transform a q:quoted.Expr[_] to a Tasty.Tree
35+ // Use q.unseal to transform a q:quoted.Expr[_] to a Tasty.Tree
3336 // Use tree.toExpr[Int] to transform a tree:Tasty.Tree to a quoted.Expr[Int]
3437 // Use q.show to show the code of a q:quoted.Expr[_]
35- // Use tree.show to show the extractors needed to pattern match a tree:Tasty.Tree
38+ // Use tree.showExtractors to show the extractors needed to pattern match a tree:Tasty.Tree
3639
37- val generatedCode = (v1.toTasty .underlyingArgument, v2.toTasty .underlyingArgument) match {
38- case (EmptyArray (), EmptyArray ()) => '(0)
40+ val generatedCode = (v1.unseal .underlyingArgument, v2.unseal .underlyingArgument) match {
41+ case (EmptyArray (), EmptyArray ()) => ' { 0 }
3942 // TODO Exercise: optimize more cases
40- // case (EmptyArray(), _) => '()
43+ // case (EmptyArray(), _) => '{0}
4144 // ...
4245 case (tv1, tv2) =>
4346 // Print the extractors of tv1 and tv2
@@ -61,10 +64,10 @@ object Vectors {
6164 generatedCode
6265 }
6366
64- /** Staged code that computes the the dot product with a while loop */
65- def dynamicDot (v1 : Expr [Array [Int ]], v2 : Expr [Array [Int ]]): Expr [Int ] = ' {
66- val vv1 = ~ v1
67- val vv2 = ~ v2
67+ /** Staged code that computes the dot product with a while loop */
68+ def dynamicDot (v1 : Expr [Array [Int ]], v2 : Expr [Array [Int ]])( using qctx : QuoteContext ) : Expr [Int ] = ' {
69+ val vv1 = $ v1
70+ val vv2 = $ v2
6871 val len = vv1.length
6972 if (vv2.length != len)
7073 throw new Exception (s " Vectors must have the same sizes ( $len, ${vv2.length}" )
0 commit comments