struct assignment from array value now checks number of elements

This commit is contained in:
Irmen de Jong 2020-10-02 22:48:39 +02:00
parent 46fbe01df9
commit c1f2ecd413
2 changed files with 7 additions and 0 deletions

View File

@ -376,6 +376,9 @@ internal class AstChecker(private val program: Program,
if (sourceVar?.struct != null) {
if (sourceVar.struct !== targetVar.struct)
errors.err("assignment of different struct types", assignment.position)
} else if(sourceVar?.isArray==true) {
if((sourceVar.value as ArrayLiteralValue).value.size != targetVar.struct?.numberOfElements)
errors.err("number of elements doesn't match struct definition", sourceVar.position)
}
}
}

View File

@ -187,6 +187,8 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
// structs are not the same in assignment
return listOf() // error will be printed elsewhere
}
if(struct.statements.size!=sourceStruct.statements.size)
return listOf() // error will be printed elsewhere
return struct.statements.zip(sourceStruct.statements).map { member ->
val targetDecl = member.first as VarDecl
val sourceDecl = member.second as VarDecl
@ -203,6 +205,8 @@ internal class StatementReorderer(val program: Program) : AstWalker() {
}
sourceVar.isArray -> {
val array = (sourceVar.value as ArrayLiteralValue).value
if(struct.statements.size!=array.size)
return listOf() // error will be printed elsewhere
return struct.statements.zip(array).map {
val decl = it.first as VarDecl
val mangled = mangledStructMemberName(identifierName, decl.name)