WerWolv / WerWolv/PatternLanguage
[Suggestion] Attribute to make local variables within structs
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 275
- Forks
- 75
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 10
Description
In functions (and the global scope), u32 var = 123; is equivalent to u32 var; var = 123;. In pattern structs, u32 var = 123; creates a local variable (not part of the pattern data) that can be freely modified, whereas u32 var; var = 123; is simply forbidden (as u32 var; is parsed as part of the pattern data, and therefore must not be modified) - there is a functional difference between initialised and uninitialised variables/patterns.
However, arrays can't be initialised like that; neither u32 var[3] = {10,20,30};, u32 var[3] = [10,20,30];, u32 var[3] = 10,20,30;, nor u32 var[3] = 10; are allowed. This means that it's impossible to declare local array variables (not part of the pattern data) within pattern structs. I would've suggested "let us initialise arrays", but the same applies to structs (e.g. struct MyStruct { u32 value; }; MyStruct var = {0}; doesn't work), and I can't think of a good way to initialise structs while declaring them.
Currently, to get around that, you have to first make a section, then place an array or struct in that section, and then fill out the array/struct values. Doing this can be inconvenient, fills the Sections tab with many different sections if you e.g. create a section like this in a struct and use that struct 20 times, and makes the [[export]] attribute not display it in the Pattern Data tab (as you instead have to open and look within the respective section's own patterns).
Therefore, I suggest an attribute or a keyword to make a local variable even when it's not initialised. For example, u32 var[3] [[local]]; would create a local array with 3 zeroes, and struct MyStruct { u32 value; }; MyStruct var [[local]]; would create a local struct with value set to zero.
(For structs, care should be taken to ignore @ for its members, except when it's placed in a section created within that struct or a sub-struct (or just except when it's in any section at all?). Structs that do something like struct MyStruct { u32 value1; if (value1 > 10) u32 value2; } won't work as expected as value1 would always be zero at that time, but it's up to pattern makers to not use local structs that require being placed in memory.)
TL;DR: An attribute/keyword to force a variable to be local would be nice, even if it's not initialised (which is currently impossible for arrays/structs). All values that would've been read from (non-section) memory should probably default to 0.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file, test, or entry point is named. Start by tracing how pattern structs parse declarations, initializers, attributes, and section-backed storage; compare initialized scalar locals with arrays and structs. Done means an agreed attribute or keyword supports local uninitialized arrays and structs with zero defaults, with behavior covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100