mirror of
https://github.com/KarolS/millfork.git
synced 2026-04-20 18:16:35 +00:00
Unions, typed pointers, indirect field access via pointers
This commit is contained in:
@@ -65,6 +65,12 @@ Such expressions have the property that the only register they may clobber is Y.
|
||||
Expressions of the shape `h:l` where `h` and `l` are of type byte, are considered expressions of type word.
|
||||
If and only if both `h` and `l` are assignable expressions, then `h:l` is also an assignable expression.
|
||||
|
||||
## Indirect field access operator
|
||||
|
||||
`->`
|
||||
|
||||
TODO
|
||||
|
||||
## Binary arithmetic operators
|
||||
|
||||
* `+`, `-`:
|
||||
@@ -133,12 +139,14 @@ Note you cannot mix those operators, so `a <= b < c` is not valid.
|
||||
`enum == enum`
|
||||
`byte == byte`
|
||||
`simple word == simple word`
|
||||
`word == constant`
|
||||
`simple long == simple long`
|
||||
|
||||
* `!=`: inequality
|
||||
`enum != enum`
|
||||
`byte != byte`
|
||||
`simple word != simple word`
|
||||
`word != constant`
|
||||
`simple long != simple long`
|
||||
|
||||
* `>`, `<`, `<=`, `>=`: inequality
|
||||
|
||||
@@ -50,6 +50,23 @@ Numeric types can be converted automatically:
|
||||
|
||||
* from a type of defined signedness to a type of undefined signedness (`sbyte`→`byte`)
|
||||
|
||||
## Typed pointers
|
||||
|
||||
For every type `T`, there is a pointer type defined called `pointer.T`.
|
||||
|
||||
Unlike raw pointers, they are not subject to arithmetic.
|
||||
|
||||
Examples:
|
||||
|
||||
pointer.t p
|
||||
p.raw // expression of type pointer, pointing to the same location in memory as 'p'
|
||||
p.lo // equivalent to 'p.raw.lo'
|
||||
p.hi // equivalent to 'p.raw.lo'
|
||||
p[0] // valid only if the type 't' is of size 1 or 2, accesses the pointed element
|
||||
p[i] // valid only if the type 't' is of size 1, equivalent to 't(p.raw[i])'
|
||||
p->x // valid only if the type 't' has a field called 'x', accesses the field 'x' of the pointed element
|
||||
p->x.y->z // you can stack it
|
||||
|
||||
## Boolean types
|
||||
|
||||
TODO
|
||||
@@ -115,3 +132,20 @@ Offsets are available as `structname.fieldname.offset`:
|
||||
|
||||
// alternatively:
|
||||
ptr = p.y.addr
|
||||
|
||||
## Unions
|
||||
|
||||
union <name> { <field definitions (type and name), separated by commas or newlines>}
|
||||
|
||||
Unions are pretty similar to structs, with the difference that all fields of the union
|
||||
start at the same point in memory and therefore overlap each other.
|
||||
|
||||
struct point { byte x, byte y }
|
||||
union point_or_word { point p, word w }
|
||||
|
||||
point_or_word u
|
||||
u.p.x = 0
|
||||
u.p.y = 0
|
||||
if u.w == 0 { ok() }
|
||||
|
||||
Offset constants are also available, but they're obviously all zero.
|
||||
|
||||
Reference in New Issue
Block a user