Code generating C headers/structs from PKL - fixed length Listings, values-as-types #662
Replies: 1 comment 2 replies
-
This is an interesting question. It's true that
It's not (yet) obvious to me how you could/would, besides creating a wholly new category of types, where you'd allow (literal? I see three options right off the bat. 1. Generate pointersYou could use the traditional C approach to variable length arrays; pointers. Let's take a look at an example Pkl definition that would be incompatible with VLAs (because multiple // my_config.pkl
foo: Listing<String>
bar: Listing<Int> would generate
This does require that you implement your 2. Define
|
Beta Was this translation helpful? Give feedback.
-
I'm trying to use pkl to generate some C headers (that define the types) and C sources (that define values of those types) from pkl types using the
reflect
module.I can successfully generate definitions for basic structs and primitive types (and fixed-width integers), but
Listing
s/arrays and arbitrary external identifiers are causing trouble because information that I need to define types in C (Listing
/array length, the C type of an external identifier) are values in pkl (Listing
length, literal C type is aString
in a struct).Ideally what I'd like to write is something like this:
To get output like this:
I can successfully generate variable-length arrays with a
length
field, but this approach isn't ideal because VLAs are a non-standard extension, they can only appear once per struct at the end, andsizeof()
and friends behave unexpectedly.I can constrain the
length
of aListing
, but that constraint seems to only be visible for pkl values, not types.When I use
reflect
to traverse the module, I can't access the type constraint to figure out how long the declared array should be.External references (ex. a function pointer, externally declared C types) have a similar problem. I can define a
class
that contains information about the identifier (C type name, what header to include), but I can't access that information whenreflect
ing over the declarations, because the information is part of a pkl value and not a type:Any ideas? Is this the kind of thing that I'd have to implement natively in pkl to support? I could potentially ex. export to json and then generate C from there, but that feels like it defeats most of the point of using pkl.
Beta Was this translation helpful? Give feedback.
All reactions