@@ -262,8 +262,11 @@ def bitwiseNot(n: Int): Int = {
262
262
}
263
263
}
264
264
265
+ /// split emitted bytes and emit the individual bits in big-endian bit order
265
266
def ungroupBytes{ body: => Unit / emit[Byte] }: Unit / emit[Bit] =
266
267
for[Byte]{body}{ b => bits(b) }
268
+
269
+ /// streaming negation in 2s-complement for little-endian bitstreams
267
270
def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
268
271
var carry = true
269
272
try body()
@@ -272,6 +275,9 @@ def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
272
275
case B1() => if(carry) { do emit(B1()); carry = false } else { do emit(B0()) }; resume(())
273
276
}
274
277
}
278
+
279
+ /// group 8 bits into a byte each, big-endian bit order.
280
+ /// NOTE: The remainder is dropped.
275
281
def groupBytesBE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
276
282
var next = 0
277
283
var p = 128
@@ -288,6 +294,9 @@ def groupBytesBE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
288
294
}
289
295
}
290
296
}
297
+
298
+ /// group 8 bits into a byte each, little-endian bit order.
299
+ /// NOTE: The remainder is dropped.
291
300
def groupBytesLE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
292
301
var next = 0
293
302
var p = 1
@@ -304,9 +313,15 @@ def groupBytesLE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
304
313
}
305
314
}
306
315
}
316
+
317
+ /// group 8 bits into a byte each, big-endian bit order.
318
+ /// NOTE: The remainder is dropped.
307
319
def groupBytes{ body: => Unit / emit[Bit] }: Unit / emit[Byte] =
308
320
groupBytesBE{body}
309
321
322
+ /// Return just nth element of the given stream.
323
+ /// Raises MissingValue if not enough elements are emitted
324
+ // TODO could be in stream
310
325
def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
311
326
var m = n
312
327
try {
@@ -322,6 +337,10 @@ def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
322
337
}
323
338
}
324
339
}
340
+
341
+ /// Return just first element of the given stream.
342
+ /// Raises MissingValue if no elements are emitted
343
+ // TODO could be in stream
325
344
def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
326
345
try {
327
346
body()
@@ -331,15 +350,18 @@ def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
331
350
}
332
351
// Literals/splices
333
352
// ----------------
334
- effect BinSplices = {
353
+ /// Splices allowed in bit stream literals
354
+ effect BitSplices = {
335
355
splice[Unit], splice[Bit],
336
356
splice[Byte],
337
357
splice[LE[Int]], splice[BE[Int]],
338
358
splice[LE[Signed[Int]]], splice[BE[Signed[Int]]],
339
359
splice[OfWidth[LE[Int]]], splice[OfWidth[BE[Int]]],
340
360
splice[OfWidth[LE[Signed[Int]]]], splice[OfWidth[BE[Signed[Int]]]]
341
361
}
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] = {
343
365
try {
344
366
ungroupBytes{
345
367
try {
@@ -394,6 +416,8 @@ def bit{ body: => Unit / { literal, BinSplices } }: Unit / emit[Bit] = {
394
416
}
395
417
}
396
418
419
+ // Simple usage examples
420
+ // =====================
397
421
namespace examples {
398
422
def main() = {
399
423
mainSuite("Simple literals"){
0 commit comments