Refactor to use Type instead of TypeRepr

This commit is contained in:
Eugene Yokota 2022-09-19 01:55:29 -04:00
parent 61d4fe2d30
commit 73c7236c2c
5 changed files with 53 additions and 41 deletions

View File

@ -149,9 +149,9 @@ trait Cont:
$instanceExpr.pure[A1] { () => $body }
}
eitherTree match
case Left(_) => pure0[Effect[A]](body.asExprOf[Effect[A]])
case Left(_) => pure0[Effect[A]](inner(body).asExprOf[Effect[A]])
case Right(_) =>
flatten(pure0[F[Effect[A]]](body.asExprOf[F[Effect[A]]]))
flatten(pure0[F[Effect[A]]](inner(body).asExprOf[F[Effect[A]]]))
// m should have type F[F[A]]
// the returned Tree will have type F[A]
@ -179,9 +179,11 @@ trait Cont:
// the call is addType(Type A, Tree qual)
// The result is a Tree representing a reference to
// the bound value of the input.
def substitute(name: String, tpe: TypeRepr, qual: Term, replace: Term) =
convert[A](name, qual) transform { (tree: Term) =>
typed[a](Ref(param.symbol))
val substitute = [x] =>
(name: String, tpe: Type[x], qual: Term, replace: Term) =>
given t: Type[x] = tpe
convert[x](name, qual) transform { (tree: Term) =>
typed[x](Ref(param.symbol))
}
transformWrappers(body.asTerm.changeOwner(sym), substitute, sym)
}
@ -194,9 +196,9 @@ trait Cont:
).asExprOf[F[A1]]
eitherTree match
case Left(_) =>
genMap0[Effect[A]](body.asExprOf[Effect[A]])
genMap0[Effect[A]](inner(body).asExprOf[Effect[A]])
case Right(_) =>
flatten(genMap0[F[Effect[A]]](body.asExprOf[F[Effect[A]]]))
flatten(genMap0[F[Effect[A]]](inner(body).asExprOf[F[Effect[A]]]))
def genMapN(body: Term, inputs: List[Input]): Expr[F[Effect[A]]] =
def genMapN0[A1: Type](body: Expr[A1]): Expr[F[A1]] =
@ -213,13 +215,15 @@ trait Cont:
// the call is addType(Type A, Tree qual)
// The result is a Tree representing a reference to
// the bound value of the input.
def substitute(name: String, tpe: TypeRepr, qual: Term, oldTree: Term) =
convert[A](name, qual) transform { (replacement: Term) =>
val idx = inputs.indexWhere(input => input.qual == qual)
Select
.unique(Ref(p0.symbol), "apply")
.appliedToTypes(List(br.inputTupleTypeRepr))
.appliedToArgs(List(Literal(IntConstant(idx))))
val substitute = [x] =>
(name: String, tpe: Type[x], qual: Term, oldTree: Term) =>
given Type[x] = tpe
convert[x](name, qual) transform { (replacement: Term) =>
val idx = inputs.indexWhere(input => input.qual == qual)
Select
.unique(Ref(p0.symbol), "apply")
.appliedToTypes(List(br.inputTupleTypeRepr))
.appliedToArgs(List(Literal(IntConstant(idx))))
}
transformWrappers(body.asTerm.changeOwner(sym), substitute, sym)
}
@ -244,19 +248,21 @@ trait Cont:
eitherTree match
case Left(_) =>
genMapN0[Effect[A]](body.asExprOf[Effect[A]])
genMapN0[Effect[A]](inner(body).asExprOf[Effect[A]])
case Right(_) =>
flatten(genMapN0[F[Effect[A]]](body.asExprOf[F[Effect[A]]]))
flatten(genMapN0[F[Effect[A]]](inner(body).asExprOf[F[Effect[A]]]))
// Called when transforming the tree to add an input.
// For `qual` of type F[A], and a `selection` qual.value.
def record(name: String, tpe: TypeRepr, qual: Term, oldTree: Term) =
convert[A](name, qual) transform { (replacement: Term) =>
inputBuf += Input(tpe, qual, replacement, freshName("q"))
oldTree
val record = [a] =>
(name: String, tpe: Type[a], qual: Term, oldTree: Term) =>
given t: Type[a] = tpe
convert[a](name, qual) transform { (replacement: Term) =>
inputBuf += Input(TypeRepr.of[a], qual, replacement, freshName("q"))
oldTree
}
val inlined = inlineExtensionProxy(expr.asTerm)
val tx = transformWrappers(inlined, record, Symbol.spliceOwner)
val tr = makeApp(inner(tx), inputBuf.toList)
val tr = makeApp(tx, inputBuf.toList)
tr
end Cont

View File

@ -37,7 +37,7 @@ trait Convert[C <: Quotes & Singleton](override val qctx: C) extends ContextUtil
*/
def transformWrappers(
tree: Term,
subWrapper: (String, TypeRepr, Term, Term) => Converted,
subWrapper: [a] => (String, Type[a], Term, Term) => Converted,
owner: Symbol,
): Term =
object ApplySelectOrIdent:
@ -53,13 +53,16 @@ trait Convert[C <: Quotes & Singleton](override val qctx: C) extends ContextUtil
override def transformTerm(tree: Term)(owner: Symbol): Term =
tree match
case ApplySelectOrIdent(nme, targ, qual) =>
subWrapper(nme, targ.tpe, qual, tree) match
case Converted.Success(tree, finalTransform) =>
finalTransform(tree)
case Converted.Failure(position, message) =>
report.errorAndAbort(message, position)
case _ =>
super.transformTerm(tree)(owner)
val tpe = targ.tpe.asType
tpe match
case '[a] =>
subWrapper[a](nme, tpe.asInstanceOf[Type[a]], qual, tree) match
case Converted.Success(tree, finalTransform) =>
finalTransform(tree)
case Converted.Failure(position, message) =>
report.errorAndAbort(message, position)
case _ =>
super.transformTerm(tree)(owner)
case _ =>
super.transformTerm(tree)(owner)
end appTransformer

View File

@ -83,7 +83,7 @@ class FullConvert[C <: Quotes & scala.Singleton](override val qctx: C)
private def wrapInit[A: Type](tree: Term): Converted =
val expr = tree.asExprOf[Initialize[A]]
val t = '{
Def.toITask($expr)
Def.toITask[A]($expr)
}
Converted.success(t.asTerm)

View File

@ -73,21 +73,21 @@ object InputTaskMacro:
)
}.asTerm
def expand(nme: String, tpeRepr: TypeRepr, tree: Term): Converted =
tpeRepr.asType match
case '[tpe] =>
nme match
case WrapInitTaskName => Converted.success(wrapInitTask[tpe](tree))
case WrapPreviousName => Converted.success(wrapInitTask[tpe](tree))
case ParserInput.WrapInitName => Converted.success(wrapInitParser[tpe](tree))
case WrapInitInputName => Converted.success(wrapInitInput[tpe](tree))
case WrapInputName => Converted.success(wrapInput[tpe](tree))
case _ => Converted.NotApplicable()
def expand[A](nme: String, tpe: Type[A], tree: Term): Converted =
given Type[A] = tpe
nme match
case WrapInitTaskName => Converted.success(wrapInitTask[A](tree))
case WrapPreviousName => Converted.success(wrapInitTask[A](tree))
case ParserInput.WrapInitName => Converted.success(wrapInitParser[A](tree))
case WrapInitInputName => Converted.success(wrapInitInput[A](tree))
case WrapInputName => Converted.success(wrapInput[A](tree))
case _ => Converted.NotApplicable()
def conditionInputTaskTree(t: Term): Term =
convert1.transformWrappers(
tree = t,
subWrapper = (nme, tpe, tree, original) => expand(nme, tpe, tree),
subWrapper = [a] =>
(nme: String, tpe: Type[a], tree: Term, original: Term) => expand[a](nme, tpe, tree),
owner = Symbol.spliceOwner,
)

View File

@ -43,6 +43,7 @@ object Assign {
val bk = taskKey[Seq[Int]]("b")
val ck = settingKey[File]("c")
val sk = taskKey[Set[_]]("s")
val bgList = taskKey[Seq[Int]]("")
val ik = inputKey[Int]("i")
val isk = inputKey[String]("is")
@ -71,6 +72,8 @@ object Assign {
ck := new File(ck.value, "asdf"),
ak := sk.value.size,
// bk ++= Seq(z.value)
intTask := ak.previous.get,
bgList := { mk.value.toString.toList.map(_.toInt) },
)
val zz = Def.task {