fix isSameAs for ArrayIndexed expressions, and by extension, assignment.isAugmentable()

This commit is contained in:
Irmen de Jong 2020-10-01 23:26:43 +02:00
parent 1464050bf5
commit 9cd3a9f8e8

View File

@ -303,6 +303,8 @@ class ArrayIndex(var index: Expression, override val position: Position) : Node
} }
fun constIndex() = (index as? NumericLiteralValue)?.number?.toInt() fun constIndex() = (index as? NumericLiteralValue)?.number?.toInt()
infix fun isSameAs(other: ArrayIndex) = index.isSameAs(other.index)
} }
open class Assignment(var target: AssignTarget, var value: Expression, override val position: Position) : Statement() { open class Assignment(var target: AssignTarget, var value: Expression, override val position: Position) : Statement() {
@ -441,19 +443,20 @@ data class AssignTarget(var identifier: IdentifierReference?,
infix fun isSameAs(value: Expression): Boolean { infix fun isSameAs(value: Expression): Boolean {
return when { return when {
this.memoryAddress != null -> { memoryAddress != null -> {
// if the target is a memory write, and the value is a memory read, they're the same if the address matches // if the target is a memory write, and the value is a memory read, they're the same if the address matches
if (value is DirectMemoryRead) if (value is DirectMemoryRead)
this.memoryAddress.addressExpression isSameAs value.addressExpression this.memoryAddress.addressExpression isSameAs value.addressExpression
else else
false false
} }
this.identifier != null -> value is IdentifierReference && value.nameInSource == identifier!!.nameInSource identifier != null -> value is IdentifierReference && value.nameInSource == identifier!!.nameInSource
this.arrayindexed != null -> value is ArrayIndexedExpression && arrayindexed != null -> {
value.identifier.nameInSource == arrayindexed!!.identifier.nameInSource && if(value is ArrayIndexedExpression && value.identifier.nameInSource == arrayindexed!!.identifier.nameInSource)
value.arrayspec.constIndex() != null && arrayindexed!!.arrayspec isSameAs value.arrayspec
arrayindexed!!.arrayspec.constIndex() != null && else
value.arrayspec.constIndex() == arrayindexed!!.arrayspec.constIndex() false
}
else -> false else -> false
} }
} }