main
Matthew Butterick 7 years ago
parent b3894c42e6
commit 2a15a3f371

@ -3,17 +3,22 @@
;; approximates https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(define TYPES (let-values ([(intkeys intvalues)
(for*/lists (intkeys intvalues)
([signed (in-list '(U ""))]
[size (in-list '(8 16 24 32))]
[endian (in-list '("" BE LE))])
(values
(string->symbol (format "~aInt~a~a" signed size endian))
(/ size 8)))])
(for/hash ([key (in-list (append '(Float Double) intkeys))]
[value (in-list (append '(4 8) intvalues))])
(values key value))))
(provide (rename-out [type-sizes TYPES]))
(define type-sizes (let-values ([(intkeys intvalues)
(for*/lists (intkeys intvalues)
([signed (in-list '(U ""))]
[size (in-list '(8 16 24 32))])
(values
(format "~aInt~a" signed size)
(/ size 8)))])
(for/hash ([key (in-list (append '(Float Double) intkeys))]
[value (in-list (append '(4 8) intvalues))]
#:when key
[endian '("" BE LE)])
(values (string->symbol (format "~a~a" key endian)) value))))
(define-subclass object% (DecodeStream [buffer #""])
(field [pos 0]

@ -15,7 +15,9 @@
(check-false (unsigned-type? 'Int16)))
(define-subclass BinaryIO% (NumberT type [endian (if (system-big-endian?) 'BE 'LE)])
(getter-field [fn (format "~a~a" type (if (ends-with-8? type) "" endian))])
(getter-field [fn (string->symbol (format "~a~a" type (if (ends-with-8? type) "" endian)))])
(unless (hash-has-key? type-sizes fn)
(raise-argument-error 'NumberT "valid type and endian" (format "~v ~v" type endian)))
(define/override (decode stream)
(define bstr (read-bytes-exact (size) stream))
@ -25,7 +27,7 @@
(define/override (encode op val) 'foo)
(define/override (size) (hash-ref TYPES type)))
(define/override (size) (hash-ref type-sizes fn)))
(test-module

Loading…
Cancel
Save