rusty icon indicating copy to clipboard operation
rusty copied to clipboard

for-loop runtime behaviour when `STEP` changes signage in loop body

Open mhasel opened this issue 1 year ago • 0 comments

Considering the following example, how many iterations do we expect? Currently, we determine the loop-terminating predicate depending on whether or not the STEP variable is positive or negative upon entering the loop, however there are alternative approaches which drastically change the behaviour.

    FUNCTION main : DINT
    VAR
        i, step, temp, iteration: DINT;
    END_VAR
        step := 1;
        iteration := 0;
        FOR i := 3 TO 10 BY step DO
            step := (step + 1) * -2;
            // i:     3, -1,   5, -9, 17
            // step: -4,  5, -14, 26
            iteration := iteration + 1;
        END_FOR
        
        main := iteration;
    END_FUNCTION

We should determine how for-loops behave in codesys when the statement parameters are changed in the loop-body and reevaluate our approach taking those findings into consideration.

Original discussion: Originally posted by @mhasel in https://github.com/PLC-lang/rusty/pull/1248#discussion_r1648205167

mhasel avatar Jun 27 '24 13:06 mhasel