Skip to content

Commit

Permalink
sligtly better error positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Mar 2, 2024
1 parent 835b4a3 commit 86f8af7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ ThisBuild/versionScheme := Some("semver-spec")
ThisBuild/resolvers ++= Opts.resolver.sonatypeOssSnapshots



val sharedSettings = Seq(
organization := "com.github.rssh",
scalaVersion := dottyVersion,
name := "dotty-cps-async",
crossScalaVersions := Seq("3.3.2")

)




lazy val root = project
.in(file("."))
Expand Down Expand Up @@ -67,6 +71,8 @@ lazy val cps = crossProject(JSPlatform, JVMPlatform, NativePlatform)
addCompilerPlugin("org.scala-native" % "junit-plugin" % nativeVersion cross CrossVersion.full)
)



lazy val CpsJVM = config("cps.jvm")
lazy val CpsJS = config("cps.js")
lazy val CpsNative = config("cps.native")
Expand Down
8 changes: 7 additions & 1 deletion docs/formalization/ismodel/CoreCps.thy
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ datatype Expr =
Await Expr
|
ExternalFun int TpExpr
|
ShiftedExternalFun int TpExpr
|
Assign int Expr
|
Block "Expr list" Expr
|
Error string






Expand Down Expand Up @@ -845,10 +850,11 @@ fun exprToCpsTreeApp :: "CpsTree \<Rightarrow> CpsTree \<Rightarrow> TpVarState
CpsTreeAsync (MflatMap (cpsTreeToExpr cpsArg tps) (Mpure fun)) AKSync
|
AKRLambda bk cpsBody \<Rightarrow>
case fun of
(case fun of
ExternalFun fi tp \<Rightarrow> errorCpsTree ''TODO''
|
x \<Rightarrow> errorCpsTree ''TODO''
)
)
)"

Expand Down
4 changes: 4 additions & 0 deletions shared/src/main/scala/cps/macros/common/TransformUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,7 @@ object TransformUtil:
case Inlined(call,binding,body) => lambdaBodyOwner(body)
case _ => Symbol.noSymbol
}

def termWithSamePos(using Quotes)(tree: quotes.reflect.Term, posTree: quotes.reflect.Term): quotes.reflect.Term =
import quotes.reflect._
???
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
CpsTree.pure(owner, applyTerm, false)
else
CpsTree.pure(owner,
Apply(
Apply.copy(applyTerm)(
Select(xx,fun.symbol),
List(yy)
)
Expand Down
72 changes: 43 additions & 29 deletions shared/src/main/scala/cps/macros/forest/CpsTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {

def isSync: Boolean = ! isAsync

def isPure: Boolean = !isAsync

/**
* @return transformed term, which is result of transformation.
**/
def transformed: Term

/**
* @return Some(syncTerm) if this tree is sync,( syncTerm can-be origin term, can0be not).
* None if this tree is async.
**/
def syncOrigin: Option[Term]


def typeApply(orig: Term, targs: List[qctx.reflect.TypeTree], ntpe: TypeRepr): CpsTree =
SelectTypeApplyCpsTree(Some(orig), this, targs, List.empty, ntpe)

Expand Down Expand Up @@ -149,7 +159,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {


def monadMap(f: Term => Term, ntpe: TypeRepr): CpsTree =
MappedCpsTree(this, f, ntpe)
MappedCpsTree(this, f, ntpe)

// pure(x).flatMap(f:A=>M[B])
def monadFlatMap(f: Term => Term, ntpe: TypeRepr): CpsTree =
Expand Down Expand Up @@ -195,7 +205,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
//given quotes = owner.asQuote // pure[Type](originTerm)(t)
val untpureTerm = cpsCtx.monad.asTerm.select(pureSymbol)
val tpureTerm = untpureTerm.appliedToType(otpe.widen)
val r = tpureTerm.appliedTo(t)
val r = Apply.copy(origin)(tpureTerm,List(t))
r
case s: Statement => BlockCpsTree(owner, Queue(s),CpsTree.empty).transformed

Expand Down Expand Up @@ -342,7 +352,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
def changeOwner(newOwner: Symbol): AwaitAsyncCpsTree =
AwaitAsyncCpsTree(nested.changeOwner(newOwner), otpe)


// TODO: add origin for generate file positions.
case class MappedCpsTree(prev: CpsTree, op: Term => Term, otpe: TypeRepr) extends AsyncCpsTree:

def isLambda: Boolean = false
Expand Down Expand Up @@ -403,7 +413,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {


case class FlatMappedCpsTree(
val prev: CpsTree,
prev: CpsTree,
opm: Term => Term,
otpe: TypeRepr) extends AsyncCpsTree:

Expand Down Expand Up @@ -947,12 +957,12 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
* selects is reversed (i.e. external added to head)
**/
case class SelectTypeApplyCpsTree(
origin: Option[Term],
nested: CpsTree,
targs:List[TypeTree],
selects: List[SelectTypeApplyRecord],
otpe: TypeRepr,
changed: Boolean = false) extends CpsTree:
optOrigin: Option[Term],
nested: CpsTree,
targs:List[TypeTree],
selects: List[SelectTypeApplyRecord],
otpe: TypeRepr,
changed: Boolean = false) extends CpsTree:

override def isAsync = nested.isAsync

Expand All @@ -964,7 +974,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {

override def inCake[F1[_],T1,C1<:CpsMonadContext[F1]](otherCake: TreeTransformScope[F1,T1,C1]): otherCake.SelectTypeApplyCpsTree =
otherCake.SelectTypeApplyCpsTree(
origin.map(_.asInstanceOf[otherCake.qctx.reflect.Term]),
optOrigin.map(_.asInstanceOf[otherCake.qctx.reflect.Term]),
nested.inCake(otherCake),
targs.map(_.asInstanceOf[otherCake.qctx.reflect.TypeTree]),
selects.map(_.inCake(otherCake)),
Expand All @@ -975,7 +985,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
if (newOwner eq owner) then
this
else
SelectTypeApplyCpsTree(origin, nested.changeOwner(newOwner), targs, selects, otpe, true)
SelectTypeApplyCpsTree(optOrigin, nested.changeOwner(newOwner), targs, selects, otpe, true)
}

def apply(term:Term):Term =
Expand All @@ -990,7 +1000,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
override def transformed: Term =
nested match
case PureCpsTree(nestOwner, nestOrigin, nestChanged) =>
origin match
optOrigin match
case Some(t) if (!isChanged) => t
case _ =>
nestOrigin match
Expand All @@ -1001,7 +1011,7 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
nested.monadMap(t => apply(t), otpe).transformed
catch
case ex:Throwable =>
println(s"Catched exception in monadMap, otpe=$otpe, origin=$origin")
println(s"Catched exception in monadMap, otpe=$otpe, origin=$optOrigin")
throw ex
case AwaitAsyncCpsTree(nNested, nOtpe) =>
AwaitSyncCpsTree(owner, apply(nested.transformed), otpe).transformed
Expand All @@ -1018,49 +1028,49 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {
case ValCpsTree(owner, valDef, rightPart, nested, canBeLambda) =>
ValCpsTree(owner, valDef, rightPart, SelectTypeApplyCpsTree.create(None, nested, targs, selects, otpe)).transformed
case AsyncLambdaCpsTree(owner, orig, params, body, nOtpe) =>
throw MacroError(s"AsyncLambda can't be transformed", posExprs(Seq()++origin++Some(orig):_*) )
throw MacroError(s"AsyncLambda can't be transformed", posExprs(Seq()++optOrigin++Some(orig):_*) )
case CallChainSubstCpsTree(owner, nestOrigin, shifted, nOtpe) =>
if (!targs.isEmpty) then
// targs is already should be here
throw MacroError("CallChainSubstCpsTree already contains applied targs", posExprs(origin.toSeq: _*))
throw MacroError("CallChainSubstCpsTree already contains applied targs", posExprs(optOrigin.toSeq: _*))
val revSelects = selects.reverse
revSelects.headOption match
case Some(head) =>
// TODO: add overloaded case
val select = Select.unique(shifted,head.symbol.name)
val selectTypeApply = if (head.targs.isEmpty) select else TypeApply(select,targs)
val nextNested = shiftedResultCpsTree(nestOrigin, selectTypeApply, None)(owner)
SelectTypeApplyCpsTree.create(origin,nextNested,targs,revSelects.tail.reverse,otpe,true).transformed
SelectTypeApplyCpsTree.create(optOrigin,nextNested,targs,revSelects.tail.reverse,otpe,true).transformed
case None =>
nested.transformed
case n@SelectTypeApplyCpsTree(nOrigin, nNested, nTargs, nSelects, nOtpe, nChanged) =>
SelectTypeApplyCpsTree(origin, nNested,targs ++ nTargs, selects ++ nSelects, otpe, changed||nChanged).transformed
SelectTypeApplyCpsTree(optOrigin, nNested,targs ++ nTargs, selects ++ nSelects, otpe, changed||nChanged).transformed
case EmptyCpsTree =>
EmptyCpsTree.transformed

override def typeApply(orig: Term, targs: List[qctx.reflect.TypeTree], ntpe: TypeRepr): CpsTree =
selects match
case head::tail =>
if (head.targs.isEmpty) then
copy(origin=Some(orig),
copy(optOrigin=Some(orig),
selects = head.copy(targs=targs)::tail,
otpe = ntpe)
else
throw MacroError("multiple type arguments are not supported", posExprs(orig))
case Nil =>
if (targs.isEmpty) then // impossible,
copy(origin=Some(orig), targs = targs)
copy(optOrigin=Some(orig), targs = targs)
else
throw MacroError("multiple type arguments are not supported", posExprs(orig))

override def select(orig: Term, symbol: Symbol, ntpe: TypeRepr): CpsTree =
val newEntry = SelectTypeApplyRecord(otpe,symbol,List.empty)
copy(origin=Some(orig), selects = newEntry::selects, otpe=ntpe)
copy(optOrigin=Some(orig), selects = newEntry::selects, otpe=ntpe)


override def syncOrigin: Option[Term] =
if (!isChanged && origin.isDefined) then
origin
if (!isChanged && optOrigin.isDefined) then
optOrigin
else
nested.syncOrigin.map(t => apply(t))

Expand Down Expand Up @@ -1108,7 +1118,11 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {

override def castOtpe(newOtpe: TypeRepr): CpsTree =
nested.syncOrigin match
case Some(t) => PureCpsTree(owner, Typed(apply(t),Inferred(newOtpe)),true)
case Some(t) =>
val nTerm = optOrigin match
case Some(ot) => Typed.copy(ot)(apply(t),Inferred(newOtpe))
case None => Typed(apply(t),Inferred(newOtpe))
PureCpsTree(owner, nTerm, true)
case None => MappedCpsTree(this, x => Typed(x,Inferred(newOtpe)),newOtpe)


Expand All @@ -1117,16 +1131,16 @@ trait CpsTreeScope[F[_], CT, CC<:CpsMonadContext[F]] {

object SelectTypeApplyCpsTree:

def create(origin: Option[Term],
nested: CpsTree,
targs:List[TypeTree],
def create(optOrigin: Option[Term],
nested: CpsTree,
targs:List[TypeTree],
selects: List[SelectTypeApplyRecord],
otpe: TypeRepr,
changed: Boolean = false) =
nested match
case prev: SelectTypeApplyCpsTree =>
SelectTypeApplyCpsTree(origin,prev.nested,targs ++ prev.targs, selects ++ prev.selects, otpe)
case _ => SelectTypeApplyCpsTree(origin,nested,targs,selects,otpe,changed)
SelectTypeApplyCpsTree(optOrigin,prev.nested,targs ++ prev.targs, selects ++ prev.selects, otpe)
case _ => SelectTypeApplyCpsTree(optOrigin,nested,targs,selects,otpe,changed)

end SelectTypeApplyCpsTree

Expand Down

0 comments on commit 86f8af7

Please sign in to comment.