diff --git a/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt b/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt index 1bedc68e4..a0edc4879 100644 --- a/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt +++ b/compiler/src/prog8/ast/antlr/Antr2Kotlin.kt @@ -627,7 +627,7 @@ private fun prog8Parser.When_choiceContext.toAst(): WhenChoice { if(stmt!=null) stmtBlock.add(stmt) val scope = AnonymousScope(stmtBlock, toPosition()) - return WhenChoice(values, scope, toPosition()) + return WhenChoice(values?.toMutableList(), scope, toPosition()) } private fun prog8Parser.VardeclContext.toAst(): VarDecl { diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 0a5f2801d..edace92bd 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -991,7 +991,7 @@ class WhenStatement(var condition: Expression, override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) } -class WhenChoice(var values: List?, // if null, this is the 'else' part +class WhenChoice(var values: MutableList?, // if null, this is the 'else' part var statements: AnonymousScope, override val position: Position) : Node { override lateinit var parent: Node @@ -1008,7 +1008,9 @@ class WhenChoice(var values: List?, // if null, this is t statements = replacement replacement.parent = this } else if(choiceValues!=null && node in choiceValues) { - throw FatalAstException("cannot replace choice values") + val idx = choiceValues.indexOf(node) + choiceValues[idx] = replacement as Expression + replacement.parent = this } else { throw FatalAstException("invalid replacement") }