Decoder module with macros

Hi! all, I’m playing around with macros and I want to make a Decoder module to decode a bitstring to support endianness, data_type and bytes size, something like
<<uint16_le::unsigned-little-size(16)>> = <<value::size(16)>>
I would like that unsigned, little to be dynamic, is that possible??
Actually I’m using the raw ast to accomplish this

{:=, [],
 [
   {:<<>>, [],
    [
      {:::, [],
       [
         {:uint16_le, [], Elixir},
         {:-, [context: Elixir, import: Kernel],
          [
            {:-, [context: Elixir, import: Kernel],
             [{:unsigned, [], Elixir}, {:little, [], Elixir}]},
            {:size, [], [16]}
          ]}
       ]}
    ]},
   {:<<>>, [], [{:::, [], [{:value, [], Elixir}, {:size, [], [16]}]}]}
 ]}

and changing the atom as I wish, is there another way?

If it’s in a macro then you can just unquote it into it:

endian = quote(do: unsigned-little)
quote(do: <<uint16_le::unquote(endian)-size(16)>> = <<value::size(16)>>)
1 Like