From 78a415bf9cdeef9960ea7fc78b844a944b22e144 Mon Sep 17 00:00:00 2001 From: furidosu <86096478+furidosu@users.noreply.github.com> Date: Thu, 30 Oct 2025 21:34:46 +0800 Subject: [PATCH 1/2] Add to loop size array in data-types.md --- pattern_language/core-language/data-types.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pattern_language/core-language/data-types.md b/pattern_language/core-language/data-types.md index 9b7d19c..3f0dff4 100644 --- a/pattern_language/core-language/data-types.md +++ b/pattern_language/core-language/data-types.md @@ -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). + +```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 From 1f112734d19f69d1aa383ece352c4417fccf420b Mon Sep 17 00:00:00 2001 From: furidosu <86096478+furidosu@users.noreply.github.com> Date: Thu, 30 Oct 2025 21:38:22 +0800 Subject: [PATCH 2/2] Fix example in loop size array in data-types.md --- pattern_language/core-language/data-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pattern_language/core-language/data-types.md b/pattern_language/core-language/data-types.md index 3f0dff4..3d1106f 100644 --- a/pattern_language/core-language/data-types.md +++ b/pattern_language/core-language/data-types.md @@ -152,7 +152,7 @@ For more complex conditions, combine an array to `eof` with a struct containing ```rust struct Command { u8 opcode; - if (opcode != 0x01 || opcode != 0x02) { break; } + if (opcode != 0x01 && opcode != 0x02) { break; } if (opcode == 0x01) { continue; } if (opcode == 0x02) { u8 src;