Tree-native binary processing

This commit is contained in:
2026-05-06 19:23:30 -05:00
parent 89bb73ed99
commit dee85efabf
2 changed files with 82 additions and 0 deletions

23
lib/binary.tri Normal file
View File

@@ -0,0 +1,23 @@
!import "base.tri" !Local
!import "list.tri" !Local
!import "bytes.tri" !Local
errUnexpectedEof = 1
ok = value rest : pair true (pair value rest)
err = code rest : pair false (pair code rest)
readU8 = (bytes : matchList
(err errUnexpectedEof t)
(h r : ok h r)
bytes)
readBytesTaken = n bytes : bytesTake n bytes
readBytesRest = n bytes : bytesDrop n bytes
readBytesEnough? = n bytes : equal? (bytesLength (readBytesTaken n bytes)) n
readBytes = (n bytes :
matchBool
(ok (readBytesTaken n bytes) (readBytesRest n bytes))
(err errUnexpectedEof bytes)
(readBytesEnough? n bytes))