evm v0.1.4 EVM.Helpers

Various helper functions with no other home.

Link to this section Summary

Functions

Gets the byte at position pos in binary

Encodes signed ints using twos compliment

Helper function to print an instruction message

Inverts a map so each key becomes a value, and vice versa

Gets the word size of a value or returns 0 if the value is 0

Wrap ints greater than the max int around back to 0

Link to this section Functions

Link to this function binary_get(binary, pos)
binary_get(binary, integer) :: integer

Gets the byte at position pos in binary.

Examples

iex> EVM.Helpers.binary_get(<<1, 2, 3, 4>>, 2)
3

iex> EVM.Helpers.binary_get(<<1, 2, 3, 4>>, 4)
** (ArgumentError) argument error
Link to this function bit_position(byte_position)
Link to this function decode_signed(n)
decode_signed(integer) :: EVM.val
Link to this function encode_signed(n)
encode_signed(integer) :: EVM.val

Encodes signed ints using twos compliment

Examples

iex> EVM.Helpers.encode_signed(1)
1

iex> EVM.Helpers.encode_signed(-1)
EVM.max_int() - 1
Link to this function encode_val(n)
encode_val(integer | [integer]) :: [EVM.val]
Link to this function inspect(msg, prefix)

Helper function to print an instruction message.

Link to this function invert(m)
invert(map) :: map

Inverts a map so each key becomes a value, and vice versa.

Examples

iex> EVM.Helpers.invert(%{a: 5, b: 10})
%{5 => :a, 10 => :b}

iex> EVM.Helpers.invert(%{dog: "cat"})
%{"cat" => :dog}

iex> EVM.Helpers.invert(%{cow: :moo})
%{moo: :cow}

iex> EVM.Helpers.invert(%{"name" => "bob"})
%{"bob" => "name"}

iex> EVM.Helpers.invert(%{})
%{}
Link to this function word_size(n)
word_size(binary) :: integer

Gets the word size of a value or returns 0 if the value is 0

Examples

iex> EVM.Helpers.word_size(<<7::256>>)
1

iex> EVM.Helpers.word_size(for val <- 1..256, into: <<>>, do: <<val>>)
8
Link to this function wrap_int(n)
wrap_int(integer) :: EVM.val

Wrap ints greater than the max int around back to 0

Examples

iex> EVM.Helpers.wrap_int(1)
1

iex> EVM.Helpers.wrap_int(EVM.max_int() + 1)
1