I would like to generate a bunch of types for each possible
file data format. There is a header with a nibble, X, that
determines which fields are present.
struct A_X <: B # X == 0x0 .. 0xF
vall::Int64 # in every struct
v1::Int16 # if bit 0 == 1
v2::Int16 # if bit 1 == 1
v3::Int16 # if bit 2 == 1
v4a::Int16 # if bit 3 == 1
v4b::Vector{Int16} # size(v4b) == v4a
end
This should generate struct with names A_0x0, A_0x1,…, A_0xF
and the fields present are from the above template. For example:
struct A_0xD <: B
vall::Int64
v1::Int16 # if bit 0 == 1
# v2::Int16 # if bit 1 == 1
v3::Int16 # if bit 2 == 1
v4a::Int16 # if bit 3 == 1
v4b::Vector{Int16} # size(v4b) == v4a
end
The 15 other struct declarations follow similarly.
My first attempt was to make a function to produce a string
that is the desired julia code. I could not figure out how
to use it without something ugly like writing a temporary
file with the expanded declarations and include()
-ing it.
It seems that I need to be generating the code at some lower
level which seems to amount to macro programming.
Am I understanding things correctly? In no particular order,
here are some problems I have:
-
Meta::parse() only seems to work for complete expressions
How am I supposed to build up the result and from which pieces? -
Is there a way to convert my desired julia struct definition
into the exact AST (or whatever) that I would need to use to
produce the needed struct declaration? -
Is there some sort of comprehensive documentation/codex that
covers this in more detail? I’ve read the manual under
code generation and things seem pretty low on specifics.
I useddump()
to look at the struct and the output did
not obviously map onto the 7-arg form that was suggested
elsewhere in the manual.
Thanks!