-
I am currently using Pkl to define hardware information in a programmatically verifiable format and have gotten to the point where I am trying to output the definitions to a YAML file. Since most of the integers I'm working with are commonly referenced by their hexadecimal value, I was hoping to output certain attributes to YAML in hex format (e.g. Unfortunately, as far as I can tell there isn't a straightforward way to output numbers in any format other than base-10. Is there something I'm missing or is the only option to write a custom integer to hex conversion function? (See below for an example of what I'm trying to accomplish) Pkl Input Fileclass Field {
bits: String
name: String
}
class Register {
address: UInt
size: DataSize(this >= 1.b, this <= 2.b)
readable: Boolean
writable: Boolean
resetValue: UInt | Null
fields: Mapping<String, Types.Field>
}
EXAMPLE_REGISTER: Register = new {
address = 0x00
readable = true
writable = true
resetValue = null
fields {
["ExampleFieldID"] {
bits = "B15..0"
name = "Example Field Name"
}
}
} Desired YAML OutputEXAMPLE_REGISTER:
address: 0x00
readable: true
writable: true
fields:
ExampleFieldID:
bits: "B15..0"
name: "Example Field Name" |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Assuming that you don't want to affect all output {
renderer = new YamlRenderer {
converters {
[Register] = (reg) -> reg.toMap().mapValues((k, v) -> if (k == "address") new RenderDirective {
text = " 0x\(v.toRadixString(16).padStart(2, "0"))"
} else v)
}
}
} This says:
Based on your example (with minor edits so it evaluates successfully), this results in: EXAMPLE_REGISTER:
address: 0x00
readable: true
writable: true
fields:
ExampleFieldID:
bits: B15..0
name: Example Field Name |
Beta Was this translation helpful? Give feedback.
Assuming that you don't want to affect all
UInt
instances (only Register.address), you can use an output converter like this:This says:
Register
class,Map
address
, replace it with aRenderDirective
that has text0x<zero-padded hex representation of the value>
Based on your example (with minor edits so it evaluates successfully), this results in:
EXAMPLE_RE…