allow when choice values to be replaced in ast (const-folding)

This commit is contained in:
Irmen de Jong 2021-01-05 03:49:11 +01:00
parent 54fc9c91ac
commit dde6919446
2 changed files with 5 additions and 3 deletions

View File

@ -627,7 +627,7 @@ private fun prog8Parser.When_choiceContext.toAst(): WhenChoice {
if(stmt!=null) if(stmt!=null)
stmtBlock.add(stmt) stmtBlock.add(stmt)
val scope = AnonymousScope(stmtBlock, toPosition()) val scope = AnonymousScope(stmtBlock, toPosition())
return WhenChoice(values, scope, toPosition()) return WhenChoice(values?.toMutableList(), scope, toPosition())
} }
private fun prog8Parser.VardeclContext.toAst(): VarDecl { private fun prog8Parser.VardeclContext.toAst(): VarDecl {

View File

@ -991,7 +991,7 @@ class WhenStatement(var condition: Expression,
override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent)
} }
class WhenChoice(var values: List<Expression>?, // if null, this is the 'else' part class WhenChoice(var values: MutableList<Expression>?, // if null, this is the 'else' part
var statements: AnonymousScope, var statements: AnonymousScope,
override val position: Position) : Node { override val position: Position) : Node {
override lateinit var parent: Node override lateinit var parent: Node
@ -1008,7 +1008,9 @@ class WhenChoice(var values: List<Expression>?, // if null, this is t
statements = replacement statements = replacement
replacement.parent = this replacement.parent = this
} else if(choiceValues!=null && node in choiceValues) { } 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 { } else {
throw FatalAstException("invalid replacement") throw FatalAstException("invalid replacement")
} }