Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pattern_language/core-language/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,26 @@ Sometimes arrays need to keep on growing as long as a certain condition is met.
u8 string[while(std::mem::read_unsigned($, 1) != 0xFF)] @ 0x00;
```

For more complex conditions, combine an array to `eof` with a struct containing `if` statements. Using `continue` would move on to the next array item of the loop; using `break` would stop the loop and the growing of the array. See also the [pattern control flow](control-flow.md#pattern-control-flow).
Copy link
Copy Markdown
Contributor

@C3pa C3pa Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For more complex conditions, combine an array to `eof` with a struct containing `if` statements. Using `continue` would move on to the next array item of the loop; using `break` would stop the loop and the growing of the array. See also the [pattern control flow](control-flow.md#pattern-control-flow).
For more complex conditions, set the array to grow until `eof` with a struct containing `if` statements. Using `continue` will move on to the next array item of the loop; using `break` will stop the loop and the growing of the array. See also the [pattern control flow](control-flow.md#pattern-control-flow).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you are referring to by "Using continue would move on to the next array item of the loop; using break would stop the loop and the growing of the array."

If I am not mistaken, the following syntax: Command command_list[while(!std::mem::eof())] @ 0x00; doesn't permit using break or continue keywords since you can't specify the loop body.

Copy link
Copy Markdown
Contributor

@paxcut paxcut Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flow control is applied to the structs, not the array so for example you can do

enum Action : u8 {
     Stop,
    Skip,
    Read
};
struct Command {
    Action action;
    if (action == Action::Stop)
          break;
   if (action== Action::Skip)
      continue;
    if (action == Action::Read)
       u8 data[5];
};
Command command_list[while(!std::mem::eof())] @ 0x00;

Maybe the example code should be simpler and better formatted


```rust
struct Command {
u8 opcode;
if (opcode != 0x01 && opcode != 0x02) { break; }
if (opcode == 0x01) { continue; }
if (opcode == 0x02) {
u8 src;
u8 dst;
}
};
Command command_list[while(!std::mem::eof())] @ 0x00;
```

#### Optimized arrays

Big arrays take a long time to compute and take up a lot of memory. Because of this, arrays of built-in types are automatically optimized to only create one instance of the array type and move it around accordingly.

The same optimization can be used for custom types by marking them with the `[[static]]` attribute. However this can only be done if the custom type always has the same size and same memory layout. Otherwise results may be invalid!
The same optimization can be used for custom types by marking them with the `[[static]]` attribute. However this can only be done if the custom type always has the same size and same memory layout. If applied to a custom type with variable size such as the `Command` type above, the results may be invalid!

#### Strings

Expand Down