Skip to content

Commit

Permalink
Reverted changes to assign expression
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Nov 29, 2023
1 parent 0a91f96 commit e3f627f
Showing 1 changed file with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class AssignExpression :
return if (lhs.size > 1) {
rhs.singleOrNull()
} else {
// We need to find out which index on the lhs this variable belongs to and
// Basically, we need to find out which index on the lhs this variable belongs to and
// find the corresponding index on the rhs.
val idx = lhs.indexOf(lhsExpression)
if (idx == -1) {
Expand All @@ -135,41 +135,26 @@ class AssignExpression :
fun findTargets(rhsExpression: HasType): List<Expression> {
val type = rhsExpression.type

// We need to find out which index on the rhs this variable belongs to and
// find the corresponding index on the rhs.
val idx = rhs.indexOf(rhsExpression)

// There are now two possibilities: Either, we have a tuple type, that we need to
// deconstruct, or we have a singular type
return if (type is TupleType) {
// If we know that the rhs element is a tuple we assign enough elements on the lhs that
// coincide with his size
// here we allow tuple only to be at the first position of the rhs
// if the index at the rhs is higher or there are not enough elements on the lhs to
// match, an error will be thrown
if (idx != 0 || lhs.size < type.types.size + rhs.size - 1) {
log.info(
"Tuple type size on RHS does is to large or tuple is not in the first position of the rhs "
)
// We need to see if there is enough room on the left side. Currently, we only support
// languages that do not allow to mix tuple and non-tuple types luckily, so we can just
// assume that all arguments on the left side are assignment targets
if (lhs.size != type.types.size) {
log.info("Tuple type size on RHS does not match number of LHS expressions")
listOf()
} else {
lhs.subList(0, type.types.size)
lhs
}
} else {
// Basically, we need to find out which index on the rhs this variable belongs to and
// find the corresponding index on the rhs.
val idx = rhs.indexOf(rhsExpression)
if (idx == -1) {
listOf()
} else {
// If lhs has more elements, the first element in rhs targets enough elements at the
// start of lhs to even it out
if (idx == 0 && lhs.size > rhs.size) {
lhs.subList(0, lhs.size - rhs.size + 1)
} else {
// we map all elements from rhs to lhs from the end of both lists, when there
// are no more elements in
// lhs to be mapped to the target will be the first element in lhs (assuming it
// is a tuple of size rhs.size - lhs.size)
listOfNotNull(lhs.getOrElse(idx + lhs.size - rhs.size) { lhs.firstOrNull() })
}
listOfNotNull(lhs.getOrNull(idx))
}
}
}
Expand Down

0 comments on commit e3f627f

Please sign in to comment.