From 88e5267f0962f77ae5447adcdd38e756224076f1 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 5 Mar 2019 15:22:11 -0800 Subject: [PATCH] =?UTF-8?q?xenobase%=20=E2=86=92=20x:base%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xenomorph/xenomorph/array.rkt | 2 +- xenomorph/xenomorph/base.rkt | 18 +++++++++--------- xenomorph/xenomorph/bitfield.rkt | 2 +- xenomorph/xenomorph/buffer.rkt | 2 +- xenomorph/xenomorph/enum.rkt | 2 +- xenomorph/xenomorph/number.rkt | 2 +- xenomorph/xenomorph/optional.rkt | 2 +- xenomorph/xenomorph/pointer.rkt | 4 ++-- xenomorph/xenomorph/reserved.rkt | 2 +- xenomorph/xenomorph/string.rkt | 2 +- xenomorph/xenomorph/struct.rkt | 2 +- 11 files changed, 20 insertions(+), 20 deletions(-) diff --git a/xenomorph/xenomorph/array.rkt b/xenomorph/xenomorph/array.rkt index 9bb4d23e..14be6b07 100644 --- a/xenomorph/xenomorph/array.rkt +++ b/xenomorph/xenomorph/array.rkt @@ -12,7 +12,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee |# (define x:array% - (class xenobase% + (class x:base% (super-new) (init-field [(@type type)] [(@len len)] [(@count-bytes? count-bytes?)]) diff --git a/xenomorph/xenomorph/base.rkt b/xenomorph/xenomorph/base.rkt index 3d5c76fb..3743bfd9 100644 --- a/xenomorph/xenomorph/base.rkt +++ b/xenomorph/xenomorph/base.rkt @@ -1,5 +1,5 @@ #lang racket/base -(require racket/class) +(require racket/class racket/match) (provide (all-defined-out)) (struct x:ptr (type val parent) #:transparent) @@ -45,7 +45,7 @@ (define (size xo [val #f] #:parent [parent #f]) (send xo size val parent)) -(define (xenomorphic-type? x) (is-a? x xenobase%)) +(define (xenomorphic-type? x) (is-a? x x:base%)) (define xenomorphic? xenomorphic-type?) (define-syntax-rule (generate-subclass CLASS PRE-ENCODE-PROC POST-DECODE-PROC) @@ -65,7 +65,7 @@ (define/override (post-decode x) (POST-DECODE-PROC (super post-decode x))))] [else CLASS])) -(define xenobase% +(define x:base% (class object% (super-new) @@ -73,14 +73,14 @@ (post-decode (inner (error 'decode-not-augmented) decode input-port parent))) (define/pubment (encode val output-port [parent #f]) - (define encode-result (inner (error 'encode-not-augmented) encode (pre-encode val) output-port parent)) - (when (bytes? encode-result) (write-bytes encode-result output-port))) + (match (inner (error 'encode-not-augmented) encode (pre-encode val) output-port parent) + [(? bytes? encode-result) (write-bytes encode-result output-port)] + [_ (void)])) (define/pubment (size [val #f] [parent #f] . args) - (define asize (inner 0 size val parent . args)) - (unless (and (integer? asize) (not (negative? asize))) - (raise-argument-error 'size "nonnegative integer" asize)) - asize) + (match (inner 0 size val parent . args) + [(? exact-nonnegative-integer? size) size] + [other (raise-argument-error 'size "nonnegative integer" other)])) (define/public (post-decode val) val) (define/public (pre-encode val) val))) \ No newline at end of file diff --git a/xenomorph/xenomorph/bitfield.rkt b/xenomorph/xenomorph/bitfield.rkt index fd661589..7b77f0fc 100644 --- a/xenomorph/xenomorph/bitfield.rkt +++ b/xenomorph/xenomorph/bitfield.rkt @@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee |# (define x:bitfield% - (class xenobase% + (class x:base% (super-new) (init-field [(@type type)][(@flagsĀ flags)]) (unless (andmap (Ī» (f) (or (symbol? f) (not f))) @flags) diff --git a/xenomorph/xenomorph/buffer.rkt b/xenomorph/xenomorph/buffer.rkt index f1f105a7..111ee667 100644 --- a/xenomorph/xenomorph/buffer.rkt +++ b/xenomorph/xenomorph/buffer.rkt @@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee |# (define x:buffer% - (class xenobase% + (class x:base% (super-new) (init-field [(@len len)]) (unless (length-resolvable? @len) diff --git a/xenomorph/xenomorph/enum.rkt b/xenomorph/xenomorph/enum.rkt index de92efb1..88373dbf 100644 --- a/xenomorph/xenomorph/enum.rkt +++ b/xenomorph/xenomorph/enum.rkt @@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee |# (define x:enum% - (class xenobase% + (class x:base% (super-new) (init-field [(@type type)] [(@values values)]) diff --git a/xenomorph/xenomorph/number.rkt b/xenomorph/xenomorph/number.rkt index d47eaeaa..266c3f69 100644 --- a/xenomorph/xenomorph/number.rkt +++ b/xenomorph/xenomorph/number.rkt @@ -24,7 +24,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (define system-endian (if (system-big-endian?) 'be 'le)) (define x:number% - (class xenobase% + (class x:base% (super-new) (init-field [(@size size)] [(@endian endian)]) diff --git a/xenomorph/xenomorph/optional.rkt b/xenomorph/xenomorph/optional.rkt index 9ac33365..9d28e341 100644 --- a/xenomorph/xenomorph/optional.rkt +++ b/xenomorph/xenomorph/optional.rkt @@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee |# (define x:optional% - (class xenobase% + (class x:base% (super-new) (init-field [(@type type)] [(@condition condition)]) diff --git a/xenomorph/xenomorph/pointer.rkt b/xenomorph/xenomorph/pointer.rkt index 2c22b706..3ffcb4e5 100644 --- a/xenomorph/xenomorph/pointer.rkt +++ b/xenomorph/xenomorph/pointer.rkt @@ -24,7 +24,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee [else (raise-argument-error 'x:pointer "VoidPointer" val)])) (define x:pointer% - (class xenobase% + (class x:base% (super-new) (init-field [(@offset-type offset-type)] [(@type type)] @@ -113,7 +113,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee [null-value null-value])) ;; A pointer whose type is determined at decode time -(define x:void-pointer% (class xenobase% +(define x:void-pointer% (class x:base% (super-new) (init-field type value))) (define (x:void-pointer . args) (apply make-object x:void-pointer% args)) diff --git a/xenomorph/xenomorph/reserved.rkt b/xenomorph/xenomorph/reserved.rkt index 8e45ef8f..fc5d879b 100644 --- a/xenomorph/xenomorph/reserved.rkt +++ b/xenomorph/xenomorph/reserved.rkt @@ -8,7 +8,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee |# (define x:reserved% - (class xenobase% + (class x:base% (super-new) (init-field [(@type type)] [(@count count)]) diff --git a/xenomorph/xenomorph/string.rkt b/xenomorph/xenomorph/string.rkt index 23f68d5e..cafccc6c 100644 --- a/xenomorph/xenomorph/string.rkt +++ b/xenomorph/xenomorph/string.rkt @@ -29,7 +29,7 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee (not (eof-object? (peek-byte port)))) (define x:string% - (class xenobase% + (class x:base% (super-new) (init-field [(@len len)] [(@encoding encoding)]) diff --git a/xenomorph/xenomorph/struct.rkt b/xenomorph/xenomorph/struct.rkt index 1a695d0f..6120943d 100644 --- a/xenomorph/xenomorph/struct.rkt +++ b/xenomorph/xenomorph/struct.rkt @@ -44,7 +44,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee h) (define x:struct% - (class xenobase% + (class x:base% (super-new) (init-field [(@fields fields)])