~vdupras/duskos

ref: 1ec00f29a8ab99b3effe1dc5cb84b91d8d96e72e duskos/fs/lib/struct.fs -rw-r--r-- 1.8 KiB
1ec00f29Virgil Dupras doc: add code.txt 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
39
40
41
42
43
44
45
46
47
48
49
50
51
\ 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 field pos.x field 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


0 value laststruct
0 value lastoffset

: struct 0 value current to laststruct 0 to lastoffset ;

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

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

\ Unbounded fields
\ These works a bit like struct fields, but without an associated struct. In
\ some cases, it makes more sense to have them instead of a full struct. Each
\ invocation of them require the struct's address on the top of PS. They also
\ support "to" semantics, but they are a bit awkward. Example:

\ 4 ( offset ) ufield foo
\ $1234 foo ( equivalent to $1238 @ )
\ 42 $1234 to+ foo ( equivalent to 42 $1238 +! )

: ufield ( off -- ) doer , does> ( a 'w )
  @ + to? ?dup if execute else @ then ;

: 'ufield ( off -- ) doer , does> ( a 'w ) @ + ;