Files
fpp/docs/spec/Definitions/Struct-Definitions.adoc
Rob Bocchino 519b3efad5 Revise spec
2026-03-09 13:30:32 -07:00

101 lines
3.0 KiB
Plaintext

=== Struct Definitions
A *struct definition* defines a new structure type and associates a name
with it.
==== Syntax
_[_
`dictionary`
_]_
`struct` <<Lexical-Elements_Identifiers,_identifier_>>
`{` _struct-type-member-sequence_ `}`
_[_ `default` <<Expressions,_expression_>> _]_
_struct-type-member-sequence_ is an <<Element-Sequences,element sequence>>
in which the elements are struct type members, and the terminating
punctuation is a comma.
A *struct type member* has the following syntax:
<<Lexical-Elements_Identifiers,_identifier_>> `:`
_[_
`[` <<Expressions,_expression_>> `]`
_]_
<<Type-Names,_type-name_>>
_[_
`format` <<Expressions_String-Literals,_string-literal_>>
_]_
==== Semantics
The identifier is the name _N_ of the type.
The definition associates the name _N_ with a
<<Types_Struct-Types,struct type>> _T_ representing a structure with named members, each
of the specified type. Each
identifier appearing in the struct type member sequence must be distinct.
For each member _m_,
* The type name specifies the type stem:[T_m] of the member.
* The optional expression `[` _e_ `]` specifies the
*size* of _m_, i.e., the number of elements stored in _m_.
If present, _e_ must have a
<<Types_Internal-Types_Numeric-Types,numeric type>>, and it must
evaluate to a value stem:[n > 0] after conversion to
<<Types_Internal-Types_Integer,_Integer_>>.
If _e_ is not present, then _m_ represents a single value of type stem:[T_m].
Otherwise _m_ represents an array of _n_ values of type stem:[T_m].
* The optional format specifier specifies a
<<Format-Strings,format string>>.
The format is applied when displaying the struct member.
There is one argument to the format string, which is the member value.
If the size of the member is greater than one, then the
translator applies the format to each element.
The expression following the keyword `default` is optional.
If present, it specifies the <<Types_Default-Values,default value>>
associated with the type.
The type of the expression must be
<<Type-Checking_Type-Conversion,convertible to>> _T_.
If the expression specifies a value for a member with size
greater than one, then the value is applied to each element.
If the optional keyword `dictionary` is present, then the
definition must conform to the rules stated in the
<<Definitions_Dictionary-Definitions,dictionary definitions>> section.
==== Examples
[source,fpp]
----
# Defines a struct type A with members x and y
# and default value { x = 0, y = 0 }
struct A {
x: U32
y: F32
}
# Defines a struct type B with members x and y
# and default value { x = 0, y = 1 }
struct B {
x: U32
y: F32
} default { y = 1 }
# Defines a struct type C with format specifiers
struct C {
x: U32 format "{} counts"
y: F32 format "{} m/s"
}
# Defines a struct type D with member x
# After translation, x is an array of 3 U32 elements
# The associated FPP struct type is { x: U32 }
# The default value of D stores 1 into each of the 3 elements of x
struct D {
x: [3] U32
} default { x = 1 }
----