Skip to content

Commit a082638

Browse files
committed
Add u32 formats
1 parent d11c5d4 commit a082638

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

experiments/idris/src/Fathom/Format/IndexedInductiveCustom.idr

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import Fathom.Data.Sing
2424
||| bit temperamental and result in ambiguities when importing modules that
2525
||| contain types of the same name as those defined in the current module.
2626
public export
27-
record CustomFormat where
28-
constructor MkCustomFormat
29-
Rep : Type
27+
record CustomFormatOf (Rep : Type) where
28+
constructor MkCustomFormatOf
3029
decode : Decode (Rep, ByteStream) ByteStream
3130
encode : Encode Rep ByteStream
3231

@@ -41,7 +40,7 @@ data FormatOf : (A : Type) -> Type where
4140
Repeat : {0 A : Type} -> (len : Nat) -> FormatOf A -> FormatOf (Vect len A)
4241
Pair : {0 A, B : Type} -> FormatOf A -> FormatOf B -> FormatOf (A, B)
4342
Bind : {0 A : Type} -> {0 B : A -> Type} -> (f : FormatOf A) -> ((x : A) -> FormatOf (B x)) -> FormatOf (x : A ** B x)
44-
Custom : (f : CustomFormat) -> FormatOf f.Rep
43+
Custom : {0 A : Type} -> (f : CustomFormatOf A) -> FormatOf A
4544

4645

4746
namespace FormatOf
@@ -97,6 +96,51 @@ namespace FormatOf
9796
encode (Custom f) x = f.encode x
9897

9998

99+
--------------------
100+
-- CUSTOM FORMATS --
101+
--------------------
102+
103+
104+
public export
105+
u8 : FormatOf Nat
106+
u8 = Custom (MkCustomFormatOf
107+
{ decode = map cast decodeU8
108+
, encode = encodeU8 . cast {to = Bits8}
109+
})
110+
111+
112+
public export
113+
u16Le : FormatOf Nat
114+
u16Le = Custom (MkCustomFormatOf
115+
{ decode = map cast (decodeU16 LE)
116+
, encode = encodeU16 LE . cast {to = Bits16}
117+
})
118+
119+
120+
public export
121+
u16Be : FormatOf Nat
122+
u16Be = Custom (MkCustomFormatOf
123+
{ decode = map cast (decodeU16 BE)
124+
, encode = encodeU16 BE . cast {to = Bits16}
125+
})
126+
127+
128+
public export
129+
u32Le : FormatOf Nat
130+
u32Le = Custom (MkCustomFormatOf
131+
{ decode = map cast (decodeU32 LE)
132+
, encode = encodeU32 LE . cast {to = Bits32}
133+
})
134+
135+
136+
public export
137+
u32Be : FormatOf Nat
138+
u32Be = Custom (MkCustomFormatOf
139+
{ decode = map cast (decodeU32 BE)
140+
, encode = encodeU32 BE . cast {to = Bits32}
141+
})
142+
143+
100144
-------------------------
101145
-- FORMAT DESCRIPTIONS --
102146
-------------------------
@@ -224,35 +268,3 @@ namespace Format
224268
public export
225269
(>>=) : (f : Format) -> (Rep f -> Format) -> Format
226270
(>>=) = bind
227-
228-
229-
--------------------
230-
-- CUSTOM FORMATS --
231-
--------------------
232-
233-
234-
public export
235-
u8 : FormatOf Nat
236-
u8 = Custom (MkCustomFormat
237-
{ Rep = Nat
238-
, decode = map cast decodeU8
239-
, encode = encodeU8 . cast {to = Bits8}
240-
})
241-
242-
243-
public export
244-
u16Le : FormatOf Nat
245-
u16Le = Custom (MkCustomFormat
246-
{ Rep = Nat
247-
, decode = map cast (decodeU16 LE)
248-
, encode = encodeU16 LE . cast {to = Bits16}
249-
})
250-
251-
252-
public export
253-
u16Be : FormatOf Nat
254-
u16Be = Custom (MkCustomFormat
255-
{ Rep = Nat
256-
, decode = map cast (decodeU16 BE)
257-
, encode = encodeU16 BE . cast {to = Bits16}
258-
})

experiments/idris/src/Fathom/Format/InductiveRecursiveCustom.idr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ namespace Format
150150
})
151151

152152

153+
public export
154+
u32Le : Format
155+
u32Le = Custom (MkCustomFormat
156+
{ Rep = Nat
157+
, decode = map cast (decodeU32 LE)
158+
, encode = encodeU32 LE . cast {to = Bits32}
159+
})
160+
161+
162+
public export
163+
u32Be : Format
164+
u32Be = Custom (MkCustomFormat
165+
{ Rep = Nat
166+
, decode = map cast (decodeU32 BE)
167+
, encode = encodeU32 BE . cast {to = Bits32}
168+
})
169+
170+
153171
---------------------------------
154172
-- INDEXED FORMAT DESCRIPTIONS --
155173
---------------------------------

experiments/idris/src/Fathom/Format/Record.idr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ namespace Format
180180
}
181181

182182

183+
public export
184+
u32Le : Format
185+
u32Le = MkFormat
186+
{ Rep = Nat
187+
, decode = map cast (decodeU32 LE)
188+
, encode = encodeU32 LE . cast {to = Bits32}
189+
}
190+
191+
192+
public export
193+
u32Be : Format
194+
u32Be = MkFormat
195+
{ Rep = Nat
196+
, decode = map cast (decodeU32 BE)
197+
, encode = encodeU32 BE . cast {to = Bits32}
198+
}
199+
200+
183201
---------------------------------
184202
-- INDEXED FORMAT DESCRIPTIONS --
185203
---------------------------------

0 commit comments

Comments
 (0)