Skip to content

Commit 5e0f2c6

Browse files
More docs and structure
1 parent dfaa06b commit 5e0f2c6

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

libraries/common/binstream.effekt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ record Signed[A](raw: A)
2424
/// Bits
2525
type Bit { B0(); B1() }
2626

27+
/// not on Bits
28+
def not(b: Bit): Bit = b match {
29+
case B0() => B1()
30+
case B1() => B0()
31+
}
32+
2733
/// Splices allowed in hex/byte stream literals
2834
effect HexSplices = {
2935
splice[Char], splice[String],
@@ -89,7 +95,10 @@ def signedBytesLE(int: Int): Unit / emit[Byte] = signedBytesLE(int, 4)
8995
/// emit bytes of the given int as 4 bytes (in 2s-complement) in big-endian byte order
9096
def signedBytesBE(int: Int): Unit / emit[Byte] = signedBytesBE(int, 4)
9197

98+
/// emit bits of the given int as 32 Bits in big-endian bit order
9299
def bitsBE(int: Int): Unit / emit[Bit] = bitsBE(int, 32)
100+
101+
/// collect bits in big-endian bit order into an Int
93102
def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
94103
var res = 0
95104
try body() with emit[Bit] { b =>
@@ -101,11 +110,10 @@ def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
101110
}
102111
res
103112
}
104-
def not(b: Bit): Bit = b match {
105-
case B0() => B1()
106-
case B1() => B0()
107-
}
113+
114+
/// bitwise not on integers
108115
def bitwiseNot(n: Int): Int = {
116+
// TODO currently implemented in Effekt, should use extern ?
109117
collectBitsBE{
110118
try bitsBE(n) with emit[Bit]{ b => resume(do emit(not(b))) }
111119
}
@@ -114,6 +122,8 @@ def bitwiseNot(n: Int): Int = {
114122
// Splicers
115123
// --------
116124

125+
/// Splicer to emit the bytes in hex notation given, plus evenutal splices
126+
/// Ignores whitespace
117127
def hex{ body: => Unit / { literal, HexSplices } }: Unit / emit[Byte] = {
118128
try {
119129
try {
@@ -153,6 +163,7 @@ def hex{ body: => Unit / { literal, HexSplices } }: Unit / emit[Byte] = {
153163
with splice[OfWidth[BE[Signed[Int]]]] { w => signedBytesBE(w.raw.raw.raw, w.width); resume(()) }
154164
}
155165

166+
/// convert the given hex notation to an integer (big-endian)
156167
def x{ body: => Unit / { literal, HexSplices } }: Int = {
157168
var res = 0
158169
for[Byte]{ hex{body} }{ v => res = res * 256 + v.toInt }
@@ -161,8 +172,16 @@ def x{ body: => Unit / { literal, HexSplices } }: Int = {
161172

162173
// Counting and padding
163174
// --------------------
175+
// TODO could also be part of stream library
176+
177+
/// Request padding to a multiple of fac, by calling gen for each
164178
effect pad[A](fac: Int){ gen: => A }: Unit
179+
180+
/// get current position in stream
165181
effect getPos(): Int
182+
183+
/// Track position in stream, starting with init
184+
/// Handles getPos and pad
166185
def tracking[A](init: Int){ body: => Unit / { emit[A], getPos, pad[A] } }: Unit / emit[A] = {
167186
var n = init
168187
try body()
@@ -177,6 +196,9 @@ def tracking[A](init: Int){ body: => Unit / { emit[A], getPos, pad[A] } }: Unit
177196
}
178197
}
179198
}
199+
200+
/// Track how many bytes were emitted
201+
/// Handles getPos and pad
180202
def tracking[A]{ body: => Unit / { emit[A], getPos, pad[A] } }: Unit / emit[A] =
181203
tracking[A](0){body}
182204

0 commit comments

Comments
 (0)