PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
201 stars 52 forks source link

Support for array type comparisions #1075

Open ghaith opened 7 months ago

ghaith commented 7 months ago

We need to support some kind of comparison possibilities when it comes to arrays. See discussion below

For context, making a default struct comparison function is not a trivial task, and often also not something the user wants of a language. The difficulty is to decide what fields in a struct are relevant for a comparison, this is why in other languages you see the struct comparison always implemented by the user and not offered by the language. So for structs there is already a solution in place that we can't make better imho which is that the user implements a comparision function for the struct. If we provide a default one based on memcmp in the standard functions we will bind it with a warning. But I think at least for structs and array types that are declared in Datatypes the solution is implement the function yourself, this is what the error suggests :

error: Missing compare function 'FUNCTION Structure_type_EQUAL : BOOL VAR_INPUT a,b : Structure_type; END_VAR ...'.
   ┌─ myprogram.st:42:24
   │
42 │   bStructCompResult := (Struct1 = Struct2);
   │                         ^^^^^^^^^^^^^^^^^ Missing compare function 'FUNCTION Structure_type_EQUAL : BOOL VAR_INPUT a,b : Structure_type; END_VAR ...'.

error: Missing compare function 'FUNCTION Array_type_EQUAL : BOOL VAR_INPUT a,b : Array_type; END_VAR ...'.
   ┌─ myprogram.st:62:23
   │
62 │   bArrayCompResult := (Array1 = Array2);
   │                        ^^^^^^^^^^^^^^^ Missing compare function 'FUNCTION Array_type_EQUAL : BOOL VAR_INPUT a,b : Array_type; END_VAR ...'.

For arrays that are not declared in types we would have to provide a method that either calls memcmp which is problematic, or a deep equals method which is slow. Maybe we could also just give that to the user as int ARRAY_EQUALS function to implement and they can decide if they want deep equals or not depending on the datatype they have.

Originally posted by @ghaith in https://github.com/PLC-lang/rusty/discussions/1050#discussioncomment-8217546

mhasel commented 7 months ago

I think changing the error message for missing compare functions would also be quite helpful for the user, to make clear that they are expected to implement these themselves/provide their own logic for how they want them compared.