Skip to content

Commit 8e8c96c

Browse files
Restore GotoTopLevelException promotion logic
1 parent 6e1914a commit 8e8c96c

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/control_flow_exceptions.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.smeup.rpgparser.interpreter
22

3-
import com.smeup.rpgparser.execution.MainExecutionContext
43
import com.smeup.rpgparser.parsing.ast.Statement
54
import com.smeup.rpgparser.utils.indexOfTag
65
import com.smeup.rpgparser.utils.runIfNotEmpty
@@ -44,15 +43,6 @@ class GotoException private constructor(val tag: String) : ControlFlowException(
4443
internal fun indexOfTaggedStatement(statements: List<Statement>) = statements.indexOfTag(tag)
4544
}
4645

47-
/**
48-
* Produce a scoped goto exception
49-
*/
50-
internal fun produceGotoInCurrentScope(tag: String): ControlFlowException {
51-
val shouldLookupTopLevel = MainExecutionContext.getSubroutineStack().isEmpty()
52-
val normalizedTag = tag.lowercase()
53-
return if (shouldLookupTopLevel) GotoTopLevelException(normalizedTag) else GotoException(normalizedTag)
54-
}
55-
5646
class InterpreterTimeoutException(val programName: String, val elapsed: Long, val expected: Long) : ControlFlowException() {
5747
fun ratio(): Double = if (elapsed <= 0) 0.0 else elapsed.toDouble() / expected.toDouble()
5848
override fun toString(): String {

rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/interpreter/internal_interpreter.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.smeup.rpgparser.parsing.facade.SourceReference
3030
import com.smeup.rpgparser.parsing.facade.dumpSource
3131
import com.smeup.rpgparser.parsing.facade.relative
3232
import com.smeup.rpgparser.parsing.parsetreetoast.RpgType
33+
import com.smeup.rpgparser.parsing.parsetreetoast.error
3334
import com.smeup.rpgparser.parsing.parsetreetoast.resolveAndValidate
3435
import com.smeup.rpgparser.parsing.parsetreetoast.todo
3536
import com.smeup.rpgparser.utils.ComparisonOperator.*
@@ -427,10 +428,15 @@ open class InternalInterpreter(
427428
val unwrappedStatement = main.stmts.explode(true)
428429

429430
// Recursive deal with top level goto flow
430-
while (throwable is GotoTopLevelException) {
431+
while (throwable is GotoTopLevelException || throwable is GotoException) {
431432
// We need to know the statement unwrapped in order to jump directly into a nested tag
432-
val offset = throwable.indexOfTaggedStatement(unwrappedStatement)
433-
require(0 <= offset && offset < unwrappedStatement.size) { "Offset $offset is not valid." }
433+
val (offset, tag) = when (throwable) {
434+
is GotoException -> Pair(throwable.indexOfTaggedStatement(unwrappedStatement), throwable.tag)
435+
is GotoTopLevelException -> Pair(throwable.indexOfTaggedStatement(unwrappedStatement), throwable.tag)
436+
else -> Pair(-1, "")
437+
}
438+
if (unwrappedStatement.size <= offset || offset < 0)
439+
main.error("GOTO offset $offset is not valid. Cannot find TAG '$tag'")
434440
throwable = kotlin.runCatching {
435441
executeUnwrappedAt(unwrappedStatement, offset)
436442
}.exceptionOrNull()

rpgJavaInterpreter-core/src/main/kotlin/com/smeup/rpgparser/parsing/ast/statements.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ data class GotoStmt(val tag: String, override val position: Position? = null) :
19111911
get() = "GOTO"
19121912

19131913
override fun execute(interpreter: InterpreterCore) {
1914-
throw produceGotoInCurrentScope(tag)
1914+
throw GotoException(tag)
19151915
}
19161916
}
19171917

@@ -1935,7 +1935,7 @@ data class CabStmt(
19351935
SMALLER -> interpreter.setIndicators(this, BooleanValue.FALSE, BooleanValue.TRUE, BooleanValue.FALSE)
19361936
else -> interpreter.setIndicators(this, BooleanValue.FALSE, BooleanValue.FALSE, BooleanValue.TRUE)
19371937
}
1938-
if (comparisonResult.isVerified) throw produceGotoInCurrentScope(tag)
1938+
if (comparisonResult.isVerified) throw GotoException(tag)
19391939
}
19401940
}
19411941

0 commit comments

Comments
 (0)