From 0a6b610c458ab1c65c6fbb2ff8d355c6099ae848 Mon Sep 17 00:00:00 2001 From: iko Date: Thu, 24 Aug 2023 19:12:54 +0300 Subject: [PATCH] Added debugger --- src/Ur/Deconstructor.elm | 45 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Ur/Deconstructor.elm b/src/Ur/Deconstructor.elm index ea35fa3..c126f5e 100644 --- a/src/Ur/Deconstructor.elm +++ b/src/Ur/Deconstructor.elm @@ -6,7 +6,7 @@ module Ur.Deconstructor exposing , int, signedInt, bigint , float32, float64 , cord, tape - , bytes, sig, ignore, tar, lazy + , bytes, sig, ignore, tar, lazy, debug ) {-| This module provides an API to deconstruct `Noun`s into arbitrary Elm data structures. @@ -55,7 +55,7 @@ You would parse a `[%edit @ cord]` like this: # Miscellaneous -@docs bytes, sig, ignore, tar, lazy +@docs bytes, sig, ignore, tar, lazy, debug -} @@ -66,7 +66,7 @@ import Bytes exposing (Bytes, Endianness(..)) import Bytes.Decode as BD import Bytes.Encode as BE import Bytes.Extra -import Ur.Jam exposing (cue) +import Ur.Jam exposing (cue, isSig) import Ur.Types exposing (..) @@ -337,3 +337,42 @@ Always succeeds. tar : Deconstructor Noun tar = Just + + +{-| Debug a deconstructor. +If the deconstructor from the second argument fails then the failed nock will be printed to the console. +-} +debug : (String -> String) -> Deconstructor a -> Deconstructor a +debug log f noun = + case f noun of + Just x -> + Just x + + Nothing -> + let + _ = + log (prettyNoun noun) + in + Nothing + + +prettyNoun : Noun -> String +prettyNoun noun = + let + go isRhs n = + case n of + Atom a -> + if isSig a then + "~" + + else + "@" + + Cell ( lhs, rhs ) -> + if isRhs then + go False lhs ++ " " ++ go True rhs + + else + "[" ++ go False lhs ++ " " ++ go True rhs ++ "]" + in + go False noun