Matthew Butterick 6 years ago
parent dbe0a8099f
commit 2406027da7

@ -13,8 +13,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|#
(define xarray%
(class xenobase%
(define x:array%
(class x:enobase%
(super-new)
(init-field [(@type type)] [(@len len)] [(@length-type length-type)])
@ -94,11 +94,11 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
#:count-bytes [count-bytes? #f]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(new (generate-subclass xarray% pre-proc post-proc) [type (or type-arg type-kwarg)]
(new (generate-subclass x:array% pre-proc post-proc) [type (or type-arg type-kwarg)]
[len (or len-arg len-kwarg)]
[length-type (if count-bytes? 'bytes length-type-arg)]))
(define (xarray? x) (is-a? x xarray%))
(define (xarray? x) (is-a? x x:array%))
(module+ test
(require rackunit "generic.rkt")

@ -7,8 +7,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee
|#
(define xbitfield%
(class xenobase%
(define x:bitfield%
(class x:enobase%
(super-new)
(init-field [(@type type)][(@flags flags)])
(unless (andmap (λ (f) (or (symbol? f) (not f))) @flags)
@ -38,7 +38,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee
#:post-decode [post-proc #f])
(define type (or type-arg type-kwarg))
(define flags (or flag-arg flag-kwarg null))
(new (generate-subclass xbitfield% pre-proc post-proc) [type type] [flags flags]))
(new (generate-subclass x:bitfield% pre-proc post-proc) [type type] [flags flags]))
(module+ test
(require rackunit "number.rkt" "generic.rkt")

@ -7,8 +7,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
|#
(define xbuffer%
(class xenobase%
(define x:buffer%
(class x:enobase%
(super-new)
(init-field [(@len len)])
(unless (length-resolvable? @len)
@ -37,4 +37,4 @@ https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(define len (or len-arg len-kwarg #xffff))
(new (generate-subclass xbuffer% pre-proc post-proc) [len len]))
(new (generate-subclass x:buffer% pre-proc post-proc) [len len]))

@ -7,8 +7,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee
|#
(define xenum%
(class xenobase%
(define x:enum%
(class x:enobase%
(super-new)
(init-field [(@type type)] [(@values values)])
@ -37,4 +37,4 @@ https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee
#:post-decode [post-proc #f])
(define type (or type-arg type-kwarg))
(define values (or values-arg values-kwarg))
(new (generate-subclass xenum% pre-proc post-proc) [type type] [values values]))
(new (generate-subclass x:enum% pre-proc post-proc) [type type] [values values]))

@ -16,7 +16,7 @@
(file-position p new-pos))
(file-position p))
(define xenomorphic<%>
(define x:enomorphic<%>
(interface* ()
([(generic-property gen:xenomorphic)
(generic-method-table
@ -38,7 +38,7 @@
(define (size xo [val #f] #:parent [parent #f])
(send xo x:size val parent)))])))
(define (xenomorphic-type? x) (is-a? x xenobase%))
(define (xenomorphic-type? x) (is-a? x x:enobase%))
(define-syntax-rule (generate-subclass CLASS PRE-ENCODE-PROC POST-DECODE-PROC)
(cond
@ -57,8 +57,8 @@
(define/override (post-decode x) (POST-DECODE-PROC (super post-decode x))))]
[else CLASS]))
(define xenobase%
(class* object% (xenomorphic<%>)
(define x:enobase%
(class* object% (x:enomorphic<%>)
(super-new)
(define/pubment (x:decode input-port [parent #f])

@ -8,8 +8,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
|#
(define xlazy-array%
(class xarray%
(define x:lazy-array%
(class x:array%
(super-new)
(inherit-field [@type type] [@len len])
@ -45,7 +45,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
#:post-decode [post-proc #f])
(define type (or type-arg type-kwarg))
(define len (or len-arg len-kwarg))
(new (generate-subclass xlazy-array% pre-proc post-proc) [type type]
(new (generate-subclass x:lazy-array% pre-proc post-proc) [type type]
[len len]
[length-type 'count]))

@ -23,8 +23,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define system-endian (if (system-big-endian?) 'be 'le))
(define xnumber%
(class xenobase%
(define x:number%
(class x:enobase%
(super-new)
(init-field [(@size size)] [(@endian endian)])
@ -37,10 +37,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define/augment (x:size . _) @size)))
(define (xint? x) (is-a? x xint%))
(define (xint? x) (is-a? x x:int%))
(define xint%
(class xnumber%
(define x:int%
(class x:number%
(super-new)
(init-field signed)
(inherit-field (@endian endian) (@size size) @bits)
@ -75,7 +75,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
#:endian [endian system-endian]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(new (generate-subclass xint% pre-proc post-proc) [size size] [signed signed] [endian endian]))
(new (generate-subclass x:int% pre-proc post-proc) [size size] [signed signed] [endian endian]))
(define int8 (+xint 1))
(define int16 (+xint 2))
@ -145,8 +145,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(check-equal? (encode int8 -1 #f) (bytes 255))
(check-equal? (encode int8 127 #f) (bytes 127)))
(define xfloat%
(class xnumber%
(define x:float%
(class x:number%
(super-new)
(inherit-field (@size size) (@endian endian))
@ -159,7 +159,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define (+xfloat [size 4] #:endian [endian system-endian]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(new (generate-subclass xfloat% pre-proc post-proc) [size size] [endian endian]))
(new (generate-subclass x:float% pre-proc post-proc) [size size] [endian endian]))
(define float (+xfloat 4))
(define floatbe (+xfloat 4 #:endian 'be))
@ -169,8 +169,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define doublebe (+xfloat 8 #:endian 'be))
(define doublele (+xfloat 8 #:endian 'le))
(define xfixed%
(class xint%
(define x:fixed%
(class x:int%
(super-new)
(init-field [(@fracbits fracbits)])
(unless (exact-positive-integer? @fracbits)
@ -190,7 +190,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
#:fracbits [fracbits (/ (* size 8) 2)]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(new (generate-subclass xfixed% pre-proc post-proc) [size size] [signed signed] [endian endian] [fracbits fracbits]))
(new (generate-subclass x:fixed% pre-proc post-proc) [size size] [signed signed] [endian endian] [fracbits fracbits]))
(define fixed16 (+xfixed 2))
(define fixed16be (+xfixed 2 #:endian 'be))

@ -7,8 +7,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
|#
(define xoptional%
(class xenobase%
(define x:optional%
(class x:enobase%
(super-new)
(init-field [(@type type)] [(@condition condition)])
@ -42,4 +42,4 @@ https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
[(and (eq? cond-arg no-val) (eq? cond-kwarg no-val)) #true]
[(not (eq? cond-arg no-val)) cond-arg]
[(not (eq? cond-kwarg no-val)) cond-kwarg]))
(new (generate-subclass xoptional% pre-proc post-proc) [type type] [condition condition]))
(new (generate-subclass x:optional% pre-proc post-proc) [type type] [condition condition]))

@ -23,8 +23,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[(xvoid-pointer? val) (values (xvoid-pointer-type val) (xvoid-pointer-value val))]
[else (raise-argument-error 'Pointer:size "VoidPointer" val)]))
(define xpointer%
(class xenobase%
(define x:pointer%
(class x:enobase%
(super-new)
(init-field [(@offset-type offset-type)]
[(@type type)]
@ -103,7 +103,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(unless (memq pointer-relative-to valid-pointer-relatives)
(raise-argument-error '+xpointer (format "~v" valid-pointer-relatives) pointer-relative-to))
(define type-in (or type-arg type-kwarg uint8))
(new (generate-subclass xpointer% pre-proc post-proc)
(new (generate-subclass x:pointer% pre-proc post-proc)
[offset-type (or offset-arg offset-kwarg uint8)]
[type (case type-in [(void) #f][else type-in])]
[pointer-relative-to pointer-relative-to]
@ -112,10 +112,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[null-value null-value]))
;; A pointer whose type is determined at decode time
(define xvoid-pointer% (class xenobase%
(define x:void-pointer% (class x:enobase%
(super-new)
(init-field type value)))
(define (+xvoid-pointer . args) (apply make-object xvoid-pointer% args))
(define (xvoid-pointer? x) (is-a? x xvoid-pointer%))
(define (+xvoid-pointer . args) (apply make-object x:void-pointer% args))
(define (xvoid-pointer? x) (is-a? x x:void-pointer%))
(define (xvoid-pointer-type x) (get-field type x))
(define (xvoid-pointer-value x) (get-field value x))

@ -7,8 +7,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee
|#
(define xreserved%
(class xenobase%
(define x:reserved%
(class x:enobase%
(super-new)
(init-field [(@type type)] [(@count count)])
@ -32,4 +32,4 @@ https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee
#:post-decode [post-proc #f])
(define type (or type-arg type-kwarg))
(define count (or count-arg count-kwarg 1))
(new (generate-subclass xreserved% pre-proc post-proc) [type type] [count count]))
(new (generate-subclass x:reserved% pre-proc post-proc) [type type] [count count]))

@ -28,8 +28,8 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
(define (bytes-left-in-port? port)
(not (eof-object? (peek-byte port))))
(define xstring%
(class xenobase%
(define x:string%
(class x:enobase%
(super-new)
(init-field [(@len len)] [(@encoding encoding)])
@ -87,10 +87,10 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
#:post-decode [post-proc #f])
(define len (or len-arg len-kwarg))
(define encoding (or enc-arg enc-kwarg 'ascii))
(new (generate-subclass xstring% pre-proc post-proc) [len len] [encoding encoding]))
(new (generate-subclass x:string% pre-proc post-proc) [len len] [encoding encoding]))
(define xsymbol%
(class xstring%
(define x:symbol%
(class x:string%
(super-new)
(inherit-field len encoding)
@ -108,7 +108,7 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
#:post-decode [post-proc #f])
(define len (or len-arg len-kwarg))
(define encoding (or enc-arg enc-kwarg 'utf8))
(new (generate-subclass xsymbol% pre-proc post-proc) [len len] [encoding encoding]))
(new (generate-subclass x:symbol% pre-proc post-proc) [len len] [encoding encoding]))
(module+ test
(require rackunit "generic.rkt")

@ -45,8 +45,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(hash-set! h k v))
h)
(define xstruct%
(class xenobase%
(define x:struct%
(class x:enobase%
(super-new)
(init-field [(@fields fields)])
@ -93,7 +93,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define pointers-size (if include-pointers (dict-ref parent 'pointerSize) 0))
(+ fields-size pointers-size))))
(define (xstruct? x) (is-a? x xstruct%))
(define (xstruct? x) (is-a? x x:struct%))
(define (+xstruct #:pre-encode [pre-proc #f]
#:post-decode [post-proc #f] . dicts)
@ -104,7 +104,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(unless (symbol? (car kv))
(raise-argument-error '+xstruct "symbol" (car kv)))
(apply cons kv)))
(new (generate-subclass xstruct% pre-proc post-proc) [fields fields]))
(new (generate-subclass x:struct% pre-proc post-proc) [fields fields]))
(module+ test
(require rackunit "number.rkt" "generic.rkt")

@ -10,8 +10,8 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|#
(define xversioned-struct%
(class xstruct%
(define x:versioned-struct%
(class x:struct%
(super-new)
(init-field [(@type type)] [(@versions versions)])
@ -100,10 +100,10 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(define pointer-size (if include-pointers (dict-ref parent 'pointerSize) 0))
(+ version-size header-size fields-size pointer-size))))
(define (xversioned-struct? x) (is-a? x xversioned-struct%))
(define (xversioned-struct? x) (is-a? x x:versioned-struct%))
(define (+xversioned-struct type [versions (dictify)]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f])
(new (generate-subclass xversioned-struct% pre-proc post-proc) [type type] [versions versions][fields #f]))
(new (generate-subclass x:versioned-struct% pre-proc post-proc) [type type] [versions versions][fields #f]))

Loading…
Cancel
Save