|
| 1 | +package x |
| 2 | + |
| 3 | +import scala.quoted._ |
| 4 | + |
| 5 | +trait CB[T]: |
| 6 | + def map[S](f: T=>S): CB[S] = ??? |
| 7 | + |
| 8 | +def await[T](x:CB[T]):T = ??? |
| 9 | + |
| 10 | +object CBM: |
| 11 | + def pure[T](t:T):CB[T] = ??? |
| 12 | + |
| 13 | +object X: |
| 14 | + |
| 15 | + inline def process[T](inline f:T) = ${ |
| 16 | + processImpl[T]('f) |
| 17 | + } |
| 18 | + |
| 19 | + def processImpl[T:Type](f:Expr[T])(using qctx: Quotes):Expr[CB[T]] = |
| 20 | + import quotes.reflect._ |
| 21 | + |
| 22 | + def transform(term:Term): Either[Term, Term] = |
| 23 | + term match |
| 24 | + case ap@Apply(Select(obj,"foreach"),args) => |
| 25 | + // println("handle-foreach") |
| 26 | + val nArgs = args.map(x => shiftLambda(x).right.get) |
| 27 | + val nSelect = Select.unique(obj, "foreach_async") |
| 28 | + Right(Apply(nSelect,nArgs)) |
| 29 | + case ap@Apply(sel@Select(obj,"awrite"),args) => |
| 30 | + // println("handle-awrite") |
| 31 | + transform(args.head) match |
| 32 | + case Left(unchanded) => Left(ap) |
| 33 | + case Right(changed) => |
| 34 | + val r = '{ ${changed.asExprOf[CB[?]]}.map(x => ${Apply(sel,List('x.asTerm)).asExpr}) } |
| 35 | + Right(r.asTerm) |
| 36 | + case Apply(tap@TypeApply(Ident("await"),targs),args) => |
| 37 | + // println("handle-await") |
| 38 | + transform(args.head) match |
| 39 | + case Left(unchanged) => Right(unchanged) |
| 40 | + case Right(changed) => |
| 41 | + val r = Apply(tap,List(changed)) |
| 42 | + Right(r) |
| 43 | + case Lambda(v, body) => ??? |
| 44 | + case Block(stats, last) => transform(last) match |
| 45 | + case Left(unchanged) => Left(Block(stats, last)) |
| 46 | + case Right(changed) => Right(Block(stats, changed)) |
| 47 | + case Inlined(x,List(),body) => transform(body) |
| 48 | + case Inlined(x,bindings,body) => transform(body) match |
| 49 | + case Left(unchanged) => Left(term) |
| 50 | + case Right(changed) => Right(Inlined(x,bindings,changed)) |
| 51 | + case Typed(arg,tp) => transform(arg) |
| 52 | + case Ident(x) => Left(term) |
| 53 | + case l@Literal(x) => Left(l) |
| 54 | + case other => |
| 55 | + throw RuntimeException(s"Not supported $other") |
| 56 | + |
| 57 | + def shiftLambda(term:Term): Either[Term,Term] = |
| 58 | + term match |
| 59 | + case lt@Lambda(params, body) => |
| 60 | + transform(body) match |
| 61 | + case Left(unchanged) => Left(term) |
| 62 | + case Right(nBody) => |
| 63 | + val paramTypes = params.map(_.tpt.tpe) |
| 64 | + val paramNames = params.map(_.name+"Changed") |
| 65 | + val mt = MethodType(paramNames)(_ => paramTypes, _ => TypeRepr.of[CB].appliedTo(body.tpe.widen) ) |
| 66 | + val r = Lambda(Symbol.spliceOwner, mt, (owner,args) => changeArgs(params,args,nBody).changeOwner(owner) ) |
| 67 | + Right(r) |
| 68 | + case _ => |
| 69 | + throw RuntimeException("lambda expected") |
| 70 | + def changeArgs(oldArgs:List[Tree], newArgs:List[Tree], body:Term):Term = |
| 71 | + // println(s"changeArgs: oldArgs=${oldArgs.map(_.symbol.hashCode)}, newArgs=${newArgs.map(_.symbol.hashCode)} ") |
| 72 | + val association: Map[Symbol, Term] = (oldArgs zip newArgs).foldLeft(Map.empty){ |
| 73 | + case (m, (oldParam, newParam: Term)) => m.updated(oldParam.symbol, newParam) |
| 74 | + case (m, (oldParam, newParam: Tree)) => throw RuntimeException("Term expected") |
| 75 | + } |
| 76 | + val changes = new TreeMap() { |
| 77 | + override def transformTerm(tree:Term)(owner: Symbol): Term = |
| 78 | + tree match |
| 79 | + case ident@Ident(name) => association.getOrElse(ident.symbol, super.transformTerm(tree)(owner)) |
| 80 | + case _ => super.transformTerm(tree)(owner) |
| 81 | + } |
| 82 | + changes.transformTerm(body)(Symbol.spliceOwner) |
| 83 | + |
| 84 | + transform(f.asTerm) match |
| 85 | + case Left(unchanged) => '{ CBM.pure($f) } |
| 86 | + case Right(changed) => changed.asExprOf[CB[T]] |
0 commit comments