~vdupras/duskos

ref: f68c6b8222cfe8a02d2f5192c54492bba7674a44 duskos/fs/lib/struct.fs -rw-r--r-- 1.3 KiB
f68c6b82Virgil Dupras Move ufields from lib/struct to bootlo, as "field" a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
\ Structures

\ This unit helps the management of structures in memory. A structure is a
\ address in memory where offsets compared to this address are mapped to names.
\ Here's an example:

\ struct Pos bfield pos.x bfield pos.y

\ This structure will be 8 bytes in size, x maps to Pos+0, y maps to Pos+4.
\ But up until now, our Pos exists nowhere. This unit doesn't manage structure
\ allocation in memory, you have to take care of this yourself. But once you
\ did, how will pos.x and pos.y know where to go? Pos is a simple value that is
\ expected to point to its current "base" address:

\ here to Pos 42 , 12 ,
\ pos.x .x1 --> 2a
\ pos.y .x1 --> 0c

\ You want to use another Pos structure? Simply write its address to Pos.

\ Struct fields support the "to" semantics:
\ 54 to pos.x

\ The "b" in "bfield" is for "bound".


0 value laststruct
0 value lastoffset

: struct 0 value current to laststruct 0 to lastoffset ;

: bfield doer laststruct to' execute , lastoffset , 4 to+ lastoffset does>
  dup @ @ swap 4 + @ + to? ?dup if execute else @ then ;

\ A 'bfield returns the address of the field instead of the value. It doesn't
\ follow "to" semantics and does not increase struct size.
: 'bfield doer laststruct to' execute , lastoffset , does>
  dup @ @ swap 4 + @ + ;