diff --git a/happy.py b/happy.py new file mode 100644 index 0000000..482b7ff --- /dev/null +++ b/happy.py @@ -0,0 +1,11 @@ +count = 0 +for n1 in range(10): + for n2 in range(10): + for n3 in range(10): + for n4 in range(10): + for n5 in range(10): + for n6 in range(10): + if n1 + n2 + n3 == n4 + n5 + n6: + count += 1 + +print(count) diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt index 4a1c011..5a9973c 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt @@ -683,7 +683,14 @@ class Compiler( // insofar we suggest source object is enumerable. Later we might need to add checks val sourceObj = source.execute(forContext) - if (sourceObj.isInstanceOf(ObjIterable)) { + if (sourceObj is ObjRange && sourceObj.isIntRange) { + loopIntRange( + forContext, + sourceObj.start!!.toInt(), + if (sourceObj.isEndInclusive) sourceObj.end!!.toInt() + 1 else sourceObj.end!!.toInt(), + loopSO, body, elseStatement, label, canBreak + ) + } else if (sourceObj.isInstanceOf(ObjIterable)) { loopIterable(forContext, sourceObj, loopSO, body, elseStatement, label, canBreak) } else { val size = runCatching { sourceObj.invokeInstanceMethod(forContext, "size").toInt() } @@ -718,8 +725,7 @@ class Compiler( } else throw lbe } - } - else result = body.execute(forContext) + } else result = body.execute(forContext) if (++index >= size) break current = sourceObj.getAt(forContext, index) } @@ -736,6 +742,31 @@ class Compiler( } } + private suspend fun loopIntRange( + forContext: Context, start: Int, end: Int, loopVar: StoredObj, + body: Statement, elseStatement: Statement?, label: String?, catchBreak: Boolean + ): Obj { + var result: Obj = ObjVoid + val iVar = ObjInt(0) + loopVar.value = iVar + for( i in start ..< end) { + iVar.value = i.toLong() + if (catchBreak) + try { + result = body.execute(forContext) + } catch (lbe: LoopBreakContinueException) { + if (lbe.label == label || lbe.label == null) { + if (lbe.doContinue) continue + } + return lbe.result + } + else { + result = body.execute(forContext) + } + } + return elseStatement?.execute(forContext) ?: result + } + private suspend fun loopIterable( forContext: Context, sourceObj: Obj, loopVar: StoredObj, body: Statement, elseStatement: Statement?, label: String?,