Created
August 27, 2015 04:05
-
-
Save rozap/7179ba0eccace90d3940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def(at(<<var!(head) :: binary - size(2), rest :: binary>>, ptr, meta) when ptr >= 8 and ptr <= 9) do | |
new_posn = ptr + 2 | |
case({:meta, {:header_length, head}}) do | |
{:halt, _} = halt -> | |
halt | |
{:meta, {key, val}} -> | |
{rest, new_posn, Dict.put(meta, key, val), []} | |
result -> | |
{rest, new_posn, meta, [result]} | |
end | |
end | |
def(at(<<var!(head) :: binary - size(2), rest :: binary>>, ptr, meta) when ptr >= 10 and ptr <= 11) do | |
new_posn = ptr + 2 | |
case({:meta, {:record_length, head}}) do | |
{:halt, _} = halt -> | |
halt | |
{:meta, {key, val}} -> | |
{rest, new_posn, Dict.put(meta, key, val), []} | |
result -> | |
{rest, new_posn, meta, [result]} | |
end | |
end |
Author
rozap
commented
Aug 27, 2015
defp build_byte_reader(from_byte, to_byte, type, endianness, body) do
byte_size = (to_byte - from_byte) + 1
units_to_match = to_match_unit(byte_size, type)
matcher = build_byte_matcher(type, units_to_match, endianness)
# Macro.to_string(matcher) |> IO.puts
wat = quote do
def at(unquote(matcher), ptr, meta) when ptr >= unquote(from_byte) and ptr <= unquote(to_byte) do
new_posn = ptr + unquote(byte_size)
case unquote(body[:do]) do
{:halt, _} = halt -> halt
{:meta, {key, val}} -> {rest, new_posn, Dict.put(meta, key, val), []}
result -> {rest, new_posn, meta, [result]}
end
end
end
Macro.to_string(wat) |> IO.puts
wat
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment