PLC-lang / rusty

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

enum declaration in VAR block #796

Closed rarris closed 10 months ago

rarris commented 1 year ago

example:

VAR
var1 : (x1:=1, x2:=2, x3:=3) := stop;
//or
var2 : (x5, x6, x7) := stop2;
//or
var3 : (x1:=1, x2:=2, x3:=3) := x2;
END_VAR

codesys support this should we support also?

riederm commented 1 year ago

we have a test inline_enums_are_generated which checks for that - so in general we can handle enums in declaration blocks. where exactly is the issue? I did some experiments in unit-tests and at least I get some code generated - I dont know if it really runs, I didnt test this far.

can yo add the outputs that you get? I tried this code in a unit test:

        VAR_GLOBAL
          x : (red, yellow, green) := 2;
        END_VAR

        PROGRAM  xxx
            VAR
                y : (redy:=1, yellowy:=2, greeny:=3) := 2;
            END_VAR
            VAR
                var1 : (x1:=1, x2:=2, x3:=3) := stop;
                //or
                var2 : (x5, x6, x7) := stop2;
            END_VAR
        END_PROGRAM

btw. I think the initializer is ignored in the godegen!

ghaith commented 1 year ago

We don't validate in unit tests so you might have missed them. But it seems like stop and stop2 are not declared variables.

Furthermore, an enum should be initialized with its enum value. Anything else is an error even if it's technically possible (We could treat it as warning in the IDE later)

mhasel commented 1 year ago

Aren't our enums constant? Allowing assignments to enums outside of their declaration just doesn't sit well with me

ghaith commented 1 year ago

Aren't our enums constant? Allowing assignments to enums outside of their declaration just doesn't sit well with me

The enums are, the variables containing an enum that can be assigned to a value of the enum. In Codesys this value can be also outside the range of the enum, usually with a warning in PLC-Developer

mhasel commented 1 year ago

Yeah I just pieced it together that it might look something like this, which seems reasonable. var1 : (start:=1, stop:=2) := stop

rarris commented 1 year ago

@riederm

error: Unexpected token: expected KeywordSemicolon but found ':= 2' ┌─ test.st:65:54 │ 65 │ y : (redy:=1, yellowy:=2, greeny:=3) := 2; │ ..................................................................................^^^^ Unexpected token: expected KeywordSemicolon but found ':= 2'

error: Unexpected token: expected KeywordSemicolon but found ':= stop' ┌─ test.st:66:46 │ 66 │ var1 : (x1:=1, x2:=2, x3:=3) := stop; │ .............................................................^^^^^^^ Unexpected token: expected KeywordSemicolon but found ':= stop'

error: Unexpected token: expected KeywordSemicolon but found ':= stop2' ┌─ test.st:68:37 │ 68 │ var2 : (x5, x6, x7) := stop2; │ .......................................^^^^^^^^ Unexpected token: expected KeywordSemicolon but found ':= stop2'