Skip to content

Instantly share code, notes, and snippets.

@cmditch
Created July 3, 2018 02:42
Show Gist options
  • Save cmditch/933e11b4a1cb6dcd1c16761968811a45 to your computer and use it in GitHub Desktop.
Save cmditch/933e11b4a1cb6dcd1c16761968811a45 to your computer and use it in GitHub Desktop.
trigggrd evm bytecode decoder for list of list support
type EvmDecoder a
= EvmDecoder (Tape -> Result String ( Tape, a ))
type Tape
= Tape String String
dynamicArray : EvmDecoder a -> EvmDecoder (List a)
dynamicArray decoder =
let
hexToLength =
Hex.fromString >> Result.map ((*) 2)
in
EvmDecoder <|
\(Tape original altered) ->
take64 altered
|> hexToLength
|> Result.andThen
(\lengthIndex ->
String.slice lengthIndex (lengthIndex + 64) original
|> Hex.fromString
|> Result.andThen
(\dataLength ->
String.dropLeft (lengthIndex + 64) original
|> \newAltered ->
(Tape original newAltered)
|> arrayHelp [] dataLength decoder
|> Result.map (\( Tape o a, d ) -> ( Tape original (drop64 altered), d ))
)
)
staticArray : Int -> EvmDecoder a -> EvmDecoder (List a)
staticArray len dec =
EvmDecoder <|
arrayHelp [] len dec
arrayHelp : List a -> Int -> EvmDecoder a -> Tape -> Result String ( Tape, List a )
arrayHelp accum len (EvmDecoder decoder) tape =
case len of
0 ->
Ok ( tape, List.reverse accum )
n ->
decoder tape
|> Result.andThen
(\( tape, val ) ->
arrayHelp (val :: accum) (n - 1) (EvmDecoder decoder) tape
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment