aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorphaller <hallerp@gmail.com>2012-10-24 16:18:24 +0200
committerphaller <hallerp@gmail.com>2012-10-24 16:18:24 +0200
commitee48c7ab3fb9d29cbb23e38136228d51de0f4f5c (patch)
treed163cadcb2d5b60f8b8793fe2db37f7f08fe84c8 /src
parent8c5cab6a7aa4ad6f4d0abd3629f41b96383f045e (diff)
downloadscala-async-ee48c7ab3fb9d29cbb23e38136228d51de0f4f5c.tar.gz
scala-async-ee48c7ab3fb9d29cbb23e38136228d51de0f4f5c.tar.bz2
scala-async-ee48c7ab3fb9d29cbb23e38136228d51de0f4f5c.zip
Add ExprBuilder.mkHandlerTree
Diffstat (limited to 'src')
-rw-r--r--src/async/library/scala/async/Async.scala34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/async/library/scala/async/Async.scala b/src/async/library/scala/async/Async.scala
index c2a9cbb..60b9f44 100644
--- a/src/async/library/scala/async/Async.scala
+++ b/src/async/library/scala/async/Async.scala
@@ -18,6 +18,7 @@ class ExprBuilder[C <: Context with Singleton](val c: C) {
builder =>
import c.universe._
+ import Flag._
/* Make a partial function literal handling case #num:
*
@@ -26,6 +27,7 @@ class ExprBuilder[C <: Context with Singleton](val c: C) {
* }
*/
def mkHandler(num: Int, rhs: c.Expr[Unit]): c.Expr[PartialFunction[Int, Unit]] = {
+/*
val numLiteral = c.Expr[Int](Literal(Constant(num)))
reify(new PartialFunction[Int, Unit] {
@@ -36,6 +38,36 @@ class ExprBuilder[C <: Context with Singleton](val c: C) {
rhs.splice
}
})
+*/
+ val rhsTree = c.resetAllAttrs(rhs.tree.duplicate)
+ val handlerTree = mkHandlerTree(num, rhsTree)
+ c.Expr(handlerTree).asInstanceOf[c.Expr[PartialFunction[Int, Unit]]]
+ }
+
+ def mkHandlerTree(num: Int, rhs: c.Tree): c.Tree = {
+ val partFunClass = c.mirror.staticClass("scala.PartialFunction")
+ val partFunIdent = Ident(partFunClass)
+ val intIdent = Ident(definitions.IntClass)
+ val unitIdent = Ident(definitions.UnitClass)
+
+ Block(List(
+ ClassDef(Modifiers(FINAL), newTypeName("$anon"), List(), Template(List(AppliedTypeTree(partFunIdent, List(intIdent, unitIdent))),
+ emptyValDef, List(
+
+ DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))),
+
+ DefDef(Modifiers(), newTermName("isDefinedAt"), List(), List(List(ValDef(Modifiers(PARAM), newTermName("x$1"), intIdent, EmptyTree))), TypeTree(), Apply(Select(Ident(newTermName("x$1")), newTermName("$eq$eq")), List(Literal(Constant(num))))),
+
+ DefDef(Modifiers(), newTermName("apply"), List(), List(List(ValDef(Modifiers(PARAM), newTermName("x$1"), intIdent, EmptyTree))), TypeTree(),
+ Match(Ident(newTermName("x$1")), List(
+ CaseDef(Bind(newTermName("any"), Typed(Ident(nme.WILDCARD), intIdent)), Apply(Select(Ident(newTermName("any")), newTermName("$eq$eq")), List(Literal(Constant(num)))), rhs)
+ ))
+ )
+
+ ))
+ )),
+ Apply(Select(New(Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List())
+ )
}
class AsyncStateBuilder {
@@ -195,7 +227,7 @@ object Async extends AsyncUtils {
}
i += 1
}
- // asyncStates(i) does not end with `await`
+ // asyncStates(i) does not end with `await` (asyncStates(i).awaitable == null)
(handlerExpr, i)
}