Skip to content

Commit 1f5d0ac

Browse files
more docs
1 parent d185d7d commit 1f5d0ac

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

libraries/common/binstream.effekt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,11 @@ def bitwiseNot(n: Int): Int = {
262262
}
263263
}
264264

265+
/// split emitted bytes and emit the individual bits in big-endian bit order
265266
def ungroupBytes{ body: => Unit / emit[Byte] }: Unit / emit[Bit] =
266267
for[Byte]{body}{ b => bits(b) }
268+
269+
/// streaming negation in 2s-complement for little-endian bitstreams
267270
def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
268271
var carry = true
269272
try body()
@@ -272,6 +275,9 @@ def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
272275
case B1() => if(carry) { do emit(B1()); carry = false } else { do emit(B0()) }; resume(())
273276
}
274277
}
278+
279+
/// group 8 bits into a byte each, big-endian bit order.
280+
/// NOTE: The remainder is dropped.
275281
def groupBytesBE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
276282
var next = 0
277283
var p = 128
@@ -288,6 +294,9 @@ def groupBytesBE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
288294
}
289295
}
290296
}
297+
298+
/// group 8 bits into a byte each, little-endian bit order.
299+
/// NOTE: The remainder is dropped.
291300
def groupBytesLE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
292301
var next = 0
293302
var p = 1
@@ -304,9 +313,15 @@ def groupBytesLE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
304313
}
305314
}
306315
}
316+
317+
/// group 8 bits into a byte each, big-endian bit order.
318+
/// NOTE: The remainder is dropped.
307319
def groupBytes{ body: => Unit / emit[Bit] }: Unit / emit[Byte] =
308320
groupBytesBE{body}
309321

322+
/// Return just nth element of the given stream.
323+
/// Raises MissingValue if not enough elements are emitted
324+
// TODO could be in stream
310325
def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
311326
var m = n
312327
try {
@@ -322,6 +337,10 @@ def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
322337
}
323338
}
324339
}
340+
341+
/// Return just first element of the given stream.
342+
/// Raises MissingValue if no elements are emitted
343+
// TODO could be in stream
325344
def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
326345
try {
327346
body()
@@ -331,15 +350,18 @@ def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
331350
}
332351
// Literals/splices
333352
// ----------------
334-
effect BinSplices = {
353+
/// Splices allowed in bit stream literals
354+
effect BitSplices = {
335355
splice[Unit], splice[Bit],
336356
splice[Byte],
337357
splice[LE[Int]], splice[BE[Int]],
338358
splice[LE[Signed[Int]]], splice[BE[Signed[Int]]],
339359
splice[OfWidth[LE[Int]]], splice[OfWidth[BE[Int]]],
340360
splice[OfWidth[LE[Signed[Int]]]], splice[OfWidth[BE[Signed[Int]]]]
341361
}
342-
def bit{ body: => Unit / { literal, BinSplices } }: Unit / emit[Bit] = {
362+
/// Splicer to emit the bits in binary notation given, plus evenutal splices
363+
/// Ignores whitespace
364+
def bit{ body: => Unit / { literal, BitSplices } }: Unit / emit[Bit] = {
343365
try {
344366
ungroupBytes{
345367
try {
@@ -394,6 +416,8 @@ def bit{ body: => Unit / { literal, BinSplices } }: Unit / emit[Bit] = {
394416
}
395417
}
396418

419+
// Simple usage examples
420+
// =====================
397421
namespace examples {
398422
def main() = {
399423
mainSuite("Simple literals"){

0 commit comments

Comments
 (0)