11.14Binary string variables. 4tH 4tH Forth compiler
Pubblicità
Pubblicità
CHAPTER 11. ADVANCED PROGRAMMING 149
It is just a bit more cumbersome to set up. You can even wrap up your binary string array into a ’:REDO’ definition, giving:
:redo developers swap cells + @c begin dup names dup while emit 1+ repeat drop drop cr
;
Which means you can write:
1 developers
Which will display ”Phil”. This way, embedding e.g. UTF-8 code not only becomes possible, but also very easy to set up.
11.14
Binary string variables
You can define raw chunks of memory with ’BUFFER:’. This will allocate a raw chunk of data of 1024 bytes:
1024 buffer: a
Finally, if you want to move raw chunks of data around, there is ’MOVE’:
1024 buffer: a
1024 buffer: b a b 512 move
This wil define two raw buffers of 1024 bytes and ’MOVE’ will copy the first 512 bytes of buffer ”a” to buffer ”b”.
11.15
Records and structures
The easiest way is to allocate a structure in the Character Segment. Just define the structure like this: struct
32 +field Name
64 +field Address
32 +field City
4 +field Age end-struct /Person
This might be a familiar example to you. We’ll store information on a single person in this structure. Now we got the fields, the length of the fields and the length of the entire structure, stored in ”/Person”. Both the fields and the entire structure are nothing more than a set of constants, e.g. the offset of the field ”Name” is stored in a CONSTANT named
”Name”. However, we still haven’t allocated any memory. We can allocate room for the structure we’ve just defined by just using the word ’STRING’. Note that you can also create a cell-based structure. Then you need the word ’ARRAY’ to allocate the memory required.
CHAPTER 11. ADVANCED PROGRAMMING 150
/Person string Person
Now we can define a word which initializes the fields:
: InitRecord \ initialize fields s" Hans Bezemer" Person -> Name place s" Lagendijk 79" Person -> Address place s" Den Helder" s" 44"
Person -> City
Person -> Age place place
;
Of course, you can also use ’ACCEPT’ to enter the contents of the fields. Fields act like ordinary strings. Note that numbers are stored as strings as well. This is not too much of a problem since ’NUMBER’ can convert them back to numbers anyway.
This is a very simple use of structures. You can also use structures within structures: struct
64 +field Address
32 +field City end-struct /Location struct
32 +field Name
/Location field Location end-struct /Person
/Person string Person s" Delft" Person -> Location -> City place
If you want to make an array of structures, that can be done as well: struct
32 +field Name
64 +field Address
32 +field City
4 +field Age end-struct /Person
\ create structure
32 constant #Person \ size of array of structs
\ now allocate the room
#Person /Person * string Persons
\ make it behave properly
:redo Persons swap /Person * + ;
\ initialize the first record s" Hans Bezemer" 0 Persons -> Name place s" Lagendijk 79" 0 Persons -> Address place s" Den Helder" s" 44"
0 Persons -> City
0 Persons -> Age place place
You can also extend an already existing structure:
\ create structure struct
32 +field Name
64 +field Address
32 +field City
12 +field Age end-struct /Person
/Person
32 +field Job
16 +field Emp-number end-struct /Employee
\ now extend the structure
Scarica
Pubblicità