History

multiple attributes; combine field and fieldarr

Sigrid Solveig Haflínudóttir committed 2 years ago

Commit 260b5881 parent 1d8152b1 file · +67 −45Browse files

Unified · Split

otf.rkt+67−45
@@ -47,42 +47,48 @@ · 46 unmodified lines
4747 (-> type? positive?)
4848 (/ (type-bits t) 8))
4949
50−(define-struct field (type name unused)
50+(define (field-unused? f)
51+ (assoc 'unused (field-attrs f)))
52+
53+(define (field-count f)
54+ (assoc 'count (field-attrs f)))
55+
56+(define (field-ptr f)
57+ (if (field-count f) "*" ""))
58+
59+(define-struct field (type name attrs)
5160 #:transparent
5261 #:methods gen:code
5362 [(define/generic super-c-type c-type)
5463 (define (gen-h f)
55− (list (~a (if (field-unused f) "// unused " "")
64+ (list (~a (if (field-unused? f) "// unused " "")
5665 (super-c-type (field-type f))
5766 " "
67+ (field-ptr f)
5868 (field-name f)
5969 ";")))
6070 (define (gen-c f)
61− (list (if (field-unused f)
62− (~a "if(skip_bytes(ctx, " (type-size (field-type f)) ") < 0){")
63− (~a "if(read_" (type-name (field-type f)) "(ctx, &v->" (field-name f) ") < 0){"))
71+ (define (size t)
72+ (if (type? t) (type-size t) 0))
73+ (define (name t)
74+ (if (type? t) (type-name t) (cmplx-name t)))
75+ (list (match (field-count f)
76+ [#f
77+ (if (field-unused? f)
78+ (~a "if(skip_bytes(ctx, " (size (field-type f)) ") < 0){")
79+ (~a "if(read_" (name (field-type f)) "(ctx, &v->" (field-name f) ") < 0){"))]
80+ [count
81+ (~a "if(read_array(ctx, &v->"
82+ (field-name f)
83+ ", read_"
84+ (super-c-type (field-type f))
85+ ", v->"
86+ (cadr count)
87+ ") < 0){")])
6488 (~a "\twerror(\"%s: %r\", \"" (field-name f) "\");")
6589 (~a "\tgoto err;")
6690 (~a "}")))])
6791
68−(define-struct (fieldarr field) (count)
69− #:transparent
70− #:methods gen:code
71− [(define/generic super-c-type c-type)
72− (define (gen-h a)
73− (list (~a (super-c-type (field-type a)) " *" (field-name a) ";")))
74− (define (gen-c a)
75− (list (~a "if(read_array(ctx, &v->"
76− (field-name a)
77− ", read_"
78− (super-c-type (field-type a))
79− ", v->"
80− (fieldarr-count a)
81− ") < 0){")
82− (~a "\twerror(\"%s: %r\", \"" (field-name a) "\");")
83− (~a "\tgoto err;")
84− (~a "}")))])
85−
8692 (define-struct cmplx (name fields tag)
8793 #:transparent
8894 #:methods gen:code
@@ -135,21 +141,35 @@ · 46 unmodified lines
135141 (set! cmplxs (append cmplxs (list typ))))]
136142 [(_ typ:id fields:expr) #'(mkcmplx typ fields "")]))
137143
144+(define-for-syntax fields '())
145+
146+(define-syntax (mkattr stx)
147+ (syntax-parse stx
148+ [(_ ({~literal =} vs:number ...+)) #''(= vs ...)]
149+ [(_ ({~literal count} n:id))
150+ (begin
151+ (if (assoc (syntax->datum #`n) fields)
152+ #''(count n)
153+ (raise-syntax-error #f "no such field" stx #'n)))]
154+ [(_ {~literal unused}) #''(unused #t)]))
155+
138156 (define-syntax (mkfield stx)
139157 (syntax-parse stx
140− [(_ {~literal //} type:id name:id)
141− #'(field type
142− `name
143− #t)]
144− [(_ type:id name:id [count:id]) #'(fieldarr type `name #f `count)]
145− [(_ type:id name:id)
146− #'(field type
147− `name
148− #f)]))
158+ [(_ type:id name:id attrs ...)
159+ (begin
160+ (let ([f #'(field type
161+ `name
162+ (list (mkattr [~@ attrs]) ...))])
163+ (begin
164+ (set! fields (append fields (list (syntax->datum #'(name type)))))
165+ f)))]))
149166
150167 (define-syntax (mkfields stx)
151168 (syntax-parse stx
152− [(_ x ...) #'(list (mkfield {~@ . x}) ...)]))
169+ [(_ x ...)
170+ (begin
171+ (set! fields '())
172+ #'(list (mkfield [~@ . x]) ...))]))
153173
154174 (mktype uint8 8 u8int)
155175 (mktype int8 8 s8int)
@@ -177,39 +197,41 @@ · 21 unmodified lines
177197 (mkcmplx TableRecord (mkfields {Tag tableTag} {uint32 checksum} {Offset32 offset} {uint32 length}))
178198
179199 (mkcmplx TableDirectory
180− (mkfields {uint32 sfntVersion}
200+ (mkfields {uint32 sfntVersion (= #x000100000 #x4f54544f)}
181201 {uint16 numTables}
182202 {uint16 searchRange}
183203 {uint16 entrySelector}
184204 {uint16 rangeShift}
185− {TableRecord tableRecords [numTables]}))
205+ {TableRecord tableRecords (count numTables)}))
186206
187207 (mkcmplx EncodingRecord
188− (mkfields {uint16 platformID} {uint16 encodingID} {Offset32 subtableOffset}))
208+ (mkfields {uint16 platformID (= 0 1 2 3 4)} {uint16 encodingID} {Offset32 subtableOffset}))
189209
190210 (mkcmplx TableCmap
191− (mkfields {uint16 version} {uint16 numTables} {EncodingRecord encodingRecords [numTables]})
211+ (mkfields {uint16 version (= 0)}
212+ {uint16 numTables}
213+ {EncodingRecord encodingRecords (count numTables)})
192214 "cmap")
193215
194216 (mkcmplx TableHead
195− (mkfields {uint16 majorVersion}
196− {uint16 minorVersion}
217+ (mkfields {uint16 majorVersion (= 1)}
218+ {uint16 minorVersion (= 0)}
197219 {Fixed fontRevision}
198220 {uint32 checksumAdjustment}
199− {uint32 magicNumber}
221+ {uint32 magicNumber (= #x5f0f3cf5)}
200222 {uint16 flags}
201223 {uint16 unitsPerEm}
202− {// LONGDATETIME created}
203− {// LONGDATETIME modified}
224+ {LONGDATETIME created unused}
225+ {LONGDATETIME modified unused}
204226 {int16 xMin}
205227 {int16 yMin}
206228 {int16 xMax}
207229 {int16 yMax}
208230 {uint16 macStyle}
209231 {uint16 lowestRecPPEM}
210− {int16 fontDirectionHint}
211− {int16 indexToLocFormat}
212− {int16 glyphDataFormat})
232+ {int16 fontDirectionHint unused (= -2 -1 0 1 2)}
233+ {int16 indexToLocFormat (= 0 1)}
234+ {int16 glyphDataFormat (= 0)})
213235 "head")
214236
215237 (printf (format gen-h))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t