[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 02/12] doc/learn-poke-in-y-minutes.pk: Update
From: |
Mohammad-Reza Nabipoor |
Subject: |
[PATCH 02/12] doc/learn-poke-in-y-minutes.pk: Update |
Date: |
Wed, 26 May 2021 02:51:05 +0430 |
2021-05-22 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* doc/learn-poke-in-y-minutes.pk (Struct Field Initializers): Update
the example according to the recent change in behavior of field
initializers.
---
ChangeLog | 6 ++++
doc/learn-poke-language-in-y-minutes.pk | 45 ++++++++++++++-----------
2 files changed, 31 insertions(+), 20 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index dff3e282..d62dd68c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-05-22 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * doc/learn-poke-in-y-minutes.pk (Struct Field Initializers): Update
+ the example according to the recent change in behavior of field
+ initializers.
+
2021-05-21 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* AUTHORS: Change my email address to gnu account.
diff --git a/doc/learn-poke-language-in-y-minutes.pk
b/doc/learn-poke-language-in-y-minutes.pk
index c72a78e7..738319d4 100644
--- a/doc/learn-poke-language-in-y-minutes.pk
+++ b/doc/learn-poke-language-in-y-minutes.pk
@@ -455,6 +455,27 @@ var packet_size = 1#Packet / 1#B; /* 10 */
var two_packet_size = 2 #Packet/#B; /* 20 */
+/* Struct Field Initializers
+ *
+ * Initialize the field to an expression.
+ */
+type HeaderWithInit =
+ struct
+ {
+ uint<8> magic = 100UB;
+ uint<8> version = 3;
+
+ offset<uint<32>,B> data_length;
+ uint<8>[data_length] data;
+ };
+
+var hdrauto = HeaderWithInit {};
+/* hdrauto.magic == 100UB && hdrauto.version == 3UB */
+
+/* It is possible to change the value of initialized fields: */
+hdrauto.magic = 200UB;
+
+
/* Struct Field Constraints
*
* It is common for struct fields to be constrained to their values to
@@ -485,27 +506,10 @@ var hdrmagic =
magic = 100UB,
};
-/* There is another way to specify the constraints: field initializers */
-
-/* Struct Field Initializers
- *
- * Field initializer has two roles:
- * - Introduce constraint of the form: `field == initializer_expression`
- * - Initialize the field with initializer expression
+/* The following statement will raise an exception, because it violates
+ * the field constraint.
*/
-type HeaderWithInit =
- struct
- {
- uint<8> magic = 100UB;
- uint<8> version = 3;
-
- offset<uint<32>,B> data_length;
- uint<8>[data_length] data;
- };
-
-/* With field initializers, this is possible: */
-var hdrauto = HeaderWithInit {};
-/* hdrauto.magic == 100UB && hdrauto.version == 3UB */
+/* hdrmagic.magic = 200UB; */
/* It is also possible to specify both a constraint and an initializer in
* the same field.
@@ -518,6 +522,7 @@ type HeaderMagicVersion =
};
var hdrmagver = HeaderMagicVersion {};
/* hdrmagver.magic == 0x55UB && hdrmagver.version == 0x2UB */
+hdrmagver.magic = 0xaaUB;
/* Integral Structs
--
2.31.1
- [PATCH 00/12] Add format function, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 01/12] Update contact info of mnabipoor, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 03/12] libpoke: Fix `muls` insn to use `pvm_alloc`, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 02/12] doc/learn-poke-in-y-minutes.pk: Update,
Mohammad-Reza Nabipoor <=
- [PATCH 04/12] libpoke/pvm.jitter: Use more `pvm_make_string_nodup`, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 05/12] libpoke/pkl-rt.pk: Fix typo s/gigi/gibi/, Mohammad-Reza Nabipoor, 2021/05/25
- [PATCH 06/12] libpoke: Fix trivial errors, Mohammad-Reza Nabipoor, 2021/05/25