Make loops generation lazy

As well as all other kinds of expressions

While it's not necessary in a sense that 'for' loop can not be plain expression,
i.e. it can't be an argument for safe-call etc., but laziness is still very convenient property.

E.g. within attached test they were generated twice in case of last expression
of coroutine block, because coroutine related codegen part is built upon
assumption that all expressions should be generated lazy.
This commit is contained in:
Denis Zharkov
2016-06-20 14:46:55 +03:00
parent ad71934c55
commit b6ccd03ef4
3 changed files with 99 additions and 20 deletions
@@ -0,0 +1,51 @@
class Controller {
var result = ""
var ok = false
suspend fun suspendHere(v: String, x: Continuation<Unit>) {
result += v
x.resume(Unit)
}
operator fun handleResult(u: Unit, v: Continuation<Nothing>) {
ok = true
}
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>): String {
val controller = Controller()
c(controller).resume(Unit)
if (!controller.ok) throw java.lang.RuntimeException("Fail ok")
return controller.result
}
fun box(): String {
val r1 = builder {
for (i in 5..6) {
suspendHere(i.toString())
}
}
if (r1 != "56") return "fail 1: $r1"
val r2 = builder {
var i = 7
while (i <= 8) {
suspendHere(i.toString())
i++
}
}
if (r2 != "78") return "fail 2: $r2"
val r3 = builder {
var i = 9
do {
suspendHere(i.toString())
i++
} while (i <= 10);
}
if (r3 != "910") return "fail 3: $r3"
return "OK"
}