Skip to content

Commit

Permalink
Support do while expression (fix #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Jun 6, 2015
1 parent ff27c14 commit 4148c33
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,28 @@ object Transformer {
transform(treeCopy.Block(block, body, Literal(Constant(())))).monad)),
origin.tpe)
}
case LabelDef(name, params, rhs) => {
???
case LabelDef(name1, List(), block@Block(body, If(condition, Apply(Ident(name2), List()), Literal(Constant(())))))
if name1 == name2 => {
new MonadTree(
Block(List(
ValDef(Modifiers(), name1, TypeTree(), transform(treeCopy.Block(block, body, Literal(Constant(())))).monad)),
Apply(
Apply(
TypeApply(
Select(TypeApply(reify(_root_.scalaz.Bind).tree, List(TypeTree(monadType))), TermName("bind")),
List(TypeTree(typeOf[_root_.scala.Unit]), TypeTree(origin.tpe))),
List(Ident(name1))),
List(
Function(
List(ValDef(Modifiers(PARAM), TermName(c.freshName()), TypeTree(typeOf[_root_.scala.Unit]), EmptyTree)),
Apply(
TypeApply(
Select(TypeApply(reify(_root_.scalaz.Monad).tree, List(TypeTree(monadType))), TermName("whileM_")),
List(TypeTree(origin.tpe))),
List(
transform(condition).monad,
Ident(name1))))))),
origin.tpe)
}
case EmptyTree | _: Throw | _: Return | _: New | _: Ident | _: Literal | _: Super | _: This | _: TypTree | _: New | _: TypeDef | _: Function | _: DefDef | _: ClassDef | _: ModuleDef | _: Import | _: ImportSelector => {
new PlainTree(origin, origin.tpe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,51 @@ def testCatch(): Unit = {
Assert.assertEquals(Some("foo"), optionHead)
}

@Test
def testIoDoWhile(): Unit = {
val transformer = Transformer[IO]
import transformer._

def s = IO("123")
var count = 0
val io = async {
var i = 0
do {
count += s.length
i += 1
} while (i < 100)
i
}

Assert.assertEquals(100, io.unsafePerformIO())

Assert.assertEquals(300, count)
}


@Test
def testDoWhile(): Unit = {
import scalaz.std.option._

val transformer = Transformer[Option]
import transformer._

def s = Some("123")
var count = 0
val option = async {
var i = 0
do {
count += s.length
i += 1
} while (i < 0)
i
}

Assert.assertEquals(Some(1), option)

Assert.assertEquals(3, count)
}

}


0 comments on commit 4148c33

Please sign in to comment.