diff --git a/pitfall/restructure/number-test.rkt b/pitfall/restructure/number-test.rkt index a48c2f07..a1e8311b 100644 --- a/pitfall/restructure/number-test.rkt +++ b/pitfall/restructure/number-test.rkt @@ -338,8 +338,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee ;; modified test: `int24` is the same endianness as the platform (check-equal? (send int24 decode (bytes 0 1 2)) (send (if (system-big-endian?) - int24be - int24le) decode (bytes 0 1 2))) + int24be + int24le) decode (bytes 0 1 2))) ; @@ -405,8 +405,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee ;; modified test: `int32` is the same endianness as the platform (check-equal? (send int32 decode (bytes 0 1 2 3)) (send (if (system-big-endian?) - int32be - int32le) decode (bytes 0 1 2 3))) + int32be + int32le) decode (bytes 0 1 2 3))) @@ -471,9 +471,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee ;; modified test: `float` is the same endianness as the platform (check-equal? (send float decode (bytes 0 1 2 3)) (send (if (system-big-endian?) - floatbe - floatle) decode (bytes 0 1 2 3))) - + floatbe + floatle) decode (bytes 0 1 2 3))) ; ; describe 'floatbe', -> diff --git a/pitfall/restructure/number.rkt b/pitfall/restructure/number.rkt index 542e98af..26c94f17 100644 --- a/pitfall/restructure/number.rkt +++ b/pitfall/restructure/number.rkt @@ -43,7 +43,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (values (- signed-min signed-min) (- signed-max signed-min))))) (define (unsigned->signed uint) - (define most-significant-bit-mask (arithmetic-shift 1 (sub1 bits))) + (define most-significant-bit-mask (arithmetic-shift 1 (sub1 bits))) (- (bitwise-xor uint most-significant-bit-mask) most-significant-bit-mask)) (define (signed->unsigned sint) @@ -78,12 +78,18 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (define/override (encode stream val) (super encode stream (floor (* val _point))))) -(define fixed16 (+Fixed 16)) -(define fixed16be (+Fixed 16 'be)) -(define fixed16le (+Fixed 16 'le)) -(define fixed32 (+Fixed 32)) -(define fixed32be (+Fixed 32 'be)) -(define fixed32le (+Fixed 32 'le)) + +(define-macro (define-subclass+provide ID (BASE-CLASS . ARGS)) + (with-pattern ([ID-CLASS (prefix-id #'BASE-CLASS ":" #'ID)]) + #'(define+provide ID (let ([ID-CLASS (class BASE-CLASS (super-new))]) + (make-object ID-CLASS . ARGS))))) + +(define-subclass+provide fixed16 (Fixed 16)) +(define-subclass+provide fixed16be (Fixed 16 'be)) +(define-subclass+provide fixed16le (Fixed 16 'le)) +(define-subclass+provide fixed32 (Fixed 32)) +(define-subclass+provide fixed32be (Fixed 32 'be)) +(define-subclass+provide fixed32le (Fixed 32 'le)) (test-module @@ -125,7 +131,6 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (check-equal? (send (+Number 'uint32) size) 4) (check-equal? (send (+Number 'double) size) 8)) - ;; use keys of type-sizes hash to generate corresponding number definitions (define-macro (make-int-types) (with-pattern ([((ID BASE ENDIAN) ...) (for/list ([k (in-hash-keys type-sizes)]) @@ -138,10 +143,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (if (positive? (string-length suffix)) suffix (if (system-big-endian?) "be" "le")))))] - [(ID ...) (suffix-id #'(ID ...) #:context caller-stx)] - [(ID-CLASS ...) (prefix-id "Number:" #'(ID ...))]) - #'(begin (define+provide ID (let ([ID-CLASS (class Number (super-new))]) - (make-object ID-CLASS 'BASE 'ENDIAN))) ...))) + [(ID ...) (suffix-id #'(ID ...) #:context caller-stx)]) + #'(begin (define-subclass+provide ID (Number 'BASE 'ENDIAN)) ...))) (make-int-types)