Previous: Decode-Compute-Encode, Up: Motivation [Contents][Index]
This is where poke comes into play. Basically, it allows you to describe the characteristics of the data you want to compute on, and then decodes and encodes it for you, taking care of the gory details. That way you can concentrate your energy on the fun part: computing on the data at your pleasure.
Of course, you are still required to provide a description of the data. In the Poke language, these descriptions take the form of type definitions, which are declarative: you specify what you want, and poke extracts the how from that.
For example, consider the following Poke type definition:
type Packet = struct { uint<16> magic = 0xef; uint<32> size; byte[size] data @ 64#B; };
This tells poke that, in order to decode a Packet
, it should
perform the following procedure (a similar procedure is implied for
encoding):
magic
. If this unsigned 16-bit integer doesn’t equal to
0xef
, then stop and emit a “data integrity” error.
size
.
size
times:
data
array.
If during this procedure an end of file is encountered, or some other erroneous condition happens, an appropriate error is raised.
In the procedure sketched above we find a sequence of operations, implied by the struct type, and a loop, implied by the array type. As we shall see later in this book, it is also possible to decode conditionally. Union types are used for that purpose.
Previous: Decode-Compute-Encode, Up: Motivation [Contents][Index]