on master help sign in
Got mat and rub working (I think)
commit636da105d6488d3cf5fc28729631144bfa5aff3d authoriko <mail@iko.soy> dateSat 27 May 2023, 19:51 (3 years ago) parent3c6e943
6 files changed+138−46
elm.json+3−2 src/BitWriter.elm+16−14 src/Urbit.elm+51−8 tests/Test/BitParser.elm → tests/Test/Bit.elm+4−22 tests/Test/Urbit.elm+40 tests/Test/Utils.elm+24
elm.json+3−2
@@ -9,11 +9,12 @@
99 ],
1010 "elm-version": "0.19.0 <= v < 0.20.0",
1111 "dependencies": {
12+ "TSFoster/elm-bytes-extra": "1.3.0 <= v < 1.4.0",
1213 "elm/bytes": "1.0.8 <= v < 2.0.0",
13− "elm/core": "1.0.0 <= v < 1.0.5"
14+ "elm/core": "1.0.0 <= v < 1.0.5",
15+ "elm-community/list-extra": "8.7.0 <= v < 9.0.0"
1416 },
1517 "test-dependencies": {
16− "TSFoster/elm-bytes-extra": "1.3.0 <= v < 1.4.0",
1718 "elm-explorations/test": "1.0.0 <= v < 2.0.0"
1819 }
1920}
src/BitWriter.elm+16−14
@@ -1,6 +1,11 @@
1−module BitWriter exposing (..)
1+module BitWriter exposing
2+ ( BitWriter
3+ , bit
4+ , bits
5+ , empty
6+ , run
7+ )
28
3−import BitParser as BP
49import Bitwise
510import Bytes exposing (Bytes)
611import Bytes.Encode as BE
@@ -67,15 +72,12 @@ bits bs writer =
6772
6873
6974
70−-- PERF: don't go through BitParser to not hold all bits in memory
71−
72−
73−bytes : Bytes -> BitWriter -> BitWriter
74−bytes bite writer =
75− case BP.run (BP.rawBits (Bytes.width bite * 8)) bite of
76− -- this is bad, but shouldn't happen
77− Nothing ->
78− writer
79−
80− Just bs ->
81− bits bs writer
75+-- -- PERF: don't go through BitParser to not hold all bits in memory
76+-- bytes : Bytes -> BitWriter -> BitWriter
77+-- bytes bite writer =
78+-- case BP.run (BP.rawBits (Bytes.width bite * 8)) bite of
79+-- -- this is bad, but shouldn't happen
80+-- Nothing ->
81+-- writer
82+-- Just bs ->
83+-- bits bs writer
src/Urbit.elm+51−8
@@ -1,9 +1,11 @@
1−module Urbit exposing (..)
1+module Urbit exposing (Noun(..), mat, rub)
22
33import BitParser as BP exposing (BitParser)
4+import BitWriter as BW exposing (BitWriter)
5+import Bitwise
46import Bytes exposing (Bytes)
5−import Bytes.Decode as BD
6−import Bytes.Encode as BE
7+import Bytes.Extra as Bytes
8+import List.Extra as List
79
810
911type Noun
@@ -11,13 +13,39 @@ type Noun
1113 | Atom Bytes
1214
1315
16+isSig : Bytes -> Bool
17+isSig bytes =
18+ Bytes.toByteValues bytes |> List.all (\x -> x == 0)
19+
20+
21+mat : Bytes -> BitWriter -> BitWriter
22+mat bytes writer =
23+ if isSig bytes then
24+ writer |> BW.bit 1
25+
26+ else
27+ let
28+ bits =
29+ bytesToBits bytes |> List.dropWhileRight (\x -> x == 0)
30+
31+ lengthBits =
32+ bits |> List.length |> intToBits |> List.reverse |> List.drop 1 |> List.reverse
33+ in
34+ writer
35+ |> BW.bit 0
36+ |> BW.bits (List.repeat (List.length lengthBits) 0)
37+ |> BW.bit 1
38+ |> BW.bits lengthBits
39+ |> BW.bits bits
40+
41+
1442rub : BitParser Bytes
1543rub =
1644 BP.bit
1745 |> BP.andThen
1846 (\zeroBit ->
1947 if zeroBit == 1 then
20− BP.succeed (BE.unsignedInt8 0 |> BE.encode)
48+ BP.succeed Bytes.empty
2149
2250 else
2351 let
@@ -40,7 +68,7 @@ rub =
4068 (\preLengthRawBits ->
4169 let
4270 length =
43− BP.bitsToInt (1 :: preLengthRawBits)
71+ BP.bitsToInt (preLengthRawBits ++ [ 1 ])
4472 in
4573 BP.bits length
4674 )
@@ -48,7 +76,22 @@ rub =
4876 )
4977
5078
79+bytesToBits : Bytes -> List Int
80+bytesToBits bytes =
81+ case
82+ BP.run (BP.rawBits (Bytes.width bytes * 8)) bytes
83+ of
84+ Nothing ->
85+ []
86+
87+ Just bits ->
88+ bits
89+
90+
91+intToBits : Int -> List Int
92+intToBits n =
93+ if n <= 0 then
94+ []
5195
52−-- met : BitParser Bytes
53−-- met =
54−-- 1
96+ else
97+ Bitwise.and 1 n :: intToBits (Bitwise.shiftRightBy 1 n)
tests/Test/BitParser.elm → tests/Test/Bit.elmrenamed+4−22
@@ -1,13 +1,11 @@
1−module Test.BitParser exposing (tests)
1+module Test.Bit exposing (tests)
22
33import BitParser
44import BitWriter
5−import Bytes exposing (Bytes)
6−import Bytes.Encode as BE
7−import Bytes.Extra
8−import Expect exposing (Expectation)
9−import Fuzz exposing (Fuzzer)
5+import Bytes
6+import Expect
107import Test exposing (..)
8+import Test.Utils exposing (..)
119
1210
1311tests : Test
@@ -37,19 +35,3 @@ tests =
3735 )
3836 ]
3937 ]
40−
41−
42−bytes : Fuzzer Bytes
43−bytes =
44− (Fuzz.intRange 0 255 |> Fuzz.list)
45− |> Fuzz.map (\l -> l |> List.map BE.unsignedInt8 |> BE.sequence |> BE.encode)
46−
47−
48−bytesEq : Bytes -> Bytes -> Expectation
49−bytesEq a b =
50− Expect.equal (Bytes.Extra.toByteValues a) (Bytes.Extra.toByteValues b)
51−
52−
53−maybeBytesEq : Maybe Bytes -> Maybe Bytes -> Expectation
54−maybeBytesEq a b =
55− Expect.equal (Maybe.map Bytes.Extra.toByteValues a) (Maybe.map Bytes.Extra.toByteValues b)
tests/Test/Urbit.elmnew file+40
@@ -0,0 +1,40 @@
1+module Test.Urbit exposing (tests)
2+
3+import BitParser
4+import BitWriter
5+import Bytes exposing (Bytes)
6+import Bytes.Extra as Bytes
7+import Fuzz exposing (Fuzzer)
8+import List.Extra as List
9+import Test exposing (..)
10+import Test.Utils exposing (..)
11+import Urbit exposing (..)
12+
13+
14+tests : Test
15+tests =
16+ concat
17+ [ fuzz atom
18+ "mat <-> rub"
19+ (\a ->
20+ maybeBytesEq (Just a) (BitWriter.run (mat a BitWriter.empty) |> BitParser.run rub)
21+ )
22+ ]
23+
24+
25+atom : Fuzzer Bytes
26+atom =
27+ bytes
28+ |> Fuzz.map
29+ (\a -> a |> Bytes.toByteValues |> List.dropWhileRight (\x -> x == 0) |> Bytes.fromByteValues)
30+
31+
32+noun : Fuzzer Noun
33+noun =
34+ (\() ->
35+ Fuzz.oneOf
36+ [ bytes |> Fuzz.map Atom
37+ , Fuzz.map2 (\a b -> Cell ( a, b )) noun noun
38+ ]
39+ )
40+ ()
tests/Test/Utils.elmnew file+24
@@ -0,0 +1,24 @@
1+module Test.Utils exposing (..)
2+
3+import Bytes exposing (Bytes)
4+import Bytes.Encode as BE
5+import Bytes.Extra
6+import Expect exposing (Expectation)
7+import Fuzz exposing (Fuzzer)
8+import Test exposing (..)
9+
10+
11+bytes : Fuzzer Bytes
12+bytes =
13+ (Fuzz.intRange 0 255 |> Fuzz.list)
14+ |> Fuzz.map (\l -> l |> List.map BE.unsignedInt8 |> BE.sequence |> BE.encode)
15+
16+
17+bytesEq : Bytes -> Bytes -> Expectation
18+bytesEq a b =
19+ Expect.equal (Bytes.Extra.toByteValues a) (Bytes.Extra.toByteValues b)
20+
21+
22+maybeBytesEq : Maybe Bytes -> Maybe Bytes -> Expectation
23+maybeBytesEq a b =
24+ Expect.equal (Maybe.map Bytes.Extra.toByteValues a) (Maybe.map Bytes.Extra.toByteValues b)