Fix ladder diagram execution order in generated ST code #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix ladder diagram execution order in generated ST code
Summary
Fixes a critical bug where ladder diagram rungs were being executed out of order in generated Structured Text programs. The issue affected projects where blocks without explicit
executionOrderId
were sorted by position, causing incorrect top-to-bottom execution sequence.Root cause: Two bugs in the sorting logic:
SortInstances()
usedoperator.eq()
which returns booleans, butcmp_to_key()
requires integer comparisons (-1/0/1)ComputeProgram()
sortedoutVariables&coils
andblocks
separately then concatenated them, always placing coils before blocks regardless of Y-positionFix: Changed to use integer subtraction for proper comparison and combine all unordered instances before sorting by Y-coordinate.
Impact: Blocks now execute in correct top-to-bottom order based on their visual position in the ladder diagram.
Review & Testing Checklist for Human
Notes
Link to Devin run: https://app.devin.ai/sessions/c3ee655cdb0f4f7ab869c1554723a288
Requested by: Thiago Alves (@thiagoralves)