Add support for negative array indexes

This commit is contained in:
Ricard Bejarano 2024-10-23 18:43:02 +02:00 committed by GitHub
parent 2b4d7535f5
commit 47762a99bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -294,7 +294,10 @@ func (arr *valueArray) index(i *interpreter, index int) (value, error) {
if 0 <= index && index < arr.length() {
return i.evaluatePV(arr.elements[index])
}
return nil, i.Error(fmt.Sprintf("Index %d out of bounds, not within [0, %v)", index, arr.length()))
if -arr.length() <= index && index < 0 {
return i.evaluatePV(arr.elements[index + arr.length()])
}
return nil, i.Error(fmt.Sprintf("Index %d out of bounds, not within [%v, %v)", index, -arr.length(), arr.length()))
}
func (arr *valueArray) length() int {