implement manifest utilities

This commit is contained in:
xenia 2020-11-21 16:31:30 -05:00
parent e9e315f001
commit c0585504ea
2 changed files with 76 additions and 70 deletions

View File

@ -16,7 +16,7 @@
;; You should have received a copy of the GNU Affero General Public License ;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require racket/match (require racket/bool racket/contract racket/list racket/match racket/vector
"pattern.rkt") "pattern.rkt")
;; a manifest is a list of list with the following fields ;; a manifest is a list of list with the following fields
@ -48,73 +48,79 @@
'?s (string->integer-set " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") '?s (string->integer-set " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
'?a (range->integer-set 32 126) '?a (range->integer-set 32 126)
'?b (range->integer-set 0 255))) '?b (range->integer-set 0 255)))
(provide builtin-isets)
;; manifest contracts and definition
(define (iset-sym? x)
(let ([y (symbol->string x)])
(and (> 0 (string-length y)) (char=? #\? (string-ref y 0)))))
(define isub/c (or/c (list/c integer? integer?) (and/c symbol? iset-sym?) string?))
(define manifest-def/c (listof (or/c
(list/c 'name string?)
(cons/c 'arch (listof string?))
(cons/c 'resources (listof string?))
(list/c 'smp boolean?)
(list/c 'mode (or/c 'stdio 'callback))
(cons/c 'command (listof string?))
(cons/c 'iset (listof isub/c))
(cons/c 'pattern (listof isub/c)))))
(struct manifest [data pattern size] #:transparent)
(provide (contract-out
[struct manifest ((data manifest-def/c)
(pattern (vector/c integer-set?))
(size integer?))]))
; (define (parse-manifest manifest-def) ;; this "parses" to the extent that is necessary to avoid unnecessary pattern computations
; (struct manifest [name arch resources smp mode command isets pattern] #:transparent) ;; the original data is stored, along with a flattened integer-set vector for the pattern and size
; ;; field counting the pattern size
; (define-syntax (check-false stx) (define/contract (parse-manifest manifest-def)
; (syntax-case stx () (-> manifest-def/c manifest?)
; [(_ what) (define isets
; (with-syntax ([id (format-id stx "manifest-~a" #'what)] (for/fold ([isets builtin-isets]) ([mf-el (in-list manifest-def)]
; [idsym #''what]) #:when (symbol=? 'iset (first mf-el)))
; #'(when (false? (id mf)) (hash-set
; (error "manifest attribute missing:" idsym)))])) isets (second mf-el)
; (for/fold ([iset (make-integer-set '())]) ([iset-el (in-list (cddr mf-el))])
; (define mf (integer-set-union
; (for/fold ([mf (manifest #f '("any") '() #f 'stdio #f builtin-isets #f)]) iset (match iset-el
; ([line (in-list manifest-def)]) [(list start end) (range->integer-set start end)]
; (match line [(? symbol? sym) (hash-ref isets sym)]
; [(list 'name name) (struct-copy manifest mf [name name])] [(? string? str) (string->integer-set str)]
; [(list 'arch arch ...) (struct-copy manifest mf [arch arch])] [_ (error "unrecognized entry in iset" iset-el)]))))))
; [(list 'resources resources ...) (struct-copy manifest mf [resources resources])] (define manifest-pattern
; [(list 'smp smp) (struct-copy manifest mf [smp smp])] (let ([pattern-entry (assoc 'pattern manifest-def)])
; [(list 'mode mode) (struct-copy manifest mf [mode mode])] (when (false? pattern-entry) (error "manifest needs a pattern"))
; [(list 'command command) (struct-copy manifest mf [command command])] (rest pattern-entry)))
; [(list 'iset name val)
; (struct-copy manifest mf
; [isets (hash-set (manifest-isets mf) name (string->integer-set val))])]
; [(list 'pattern pattern ...) (struct-copy manifest mf [pattern pattern])])))
;
; (check-false name)
; (check-false command)
; (check-false pattern)
;
; (define isets (manifest-isets mf))
; (define (get-iset x)
; (hash-ref isets x (lambda () (error "no such iset defined" x))))
;
; (define (parse-string-pattern ptn)
; (for/vector ([x (in-list (regexp-match* #rx"\\??." ptn))])
; (if (= 1 (string-length x))
; (char->integer-set (string-ref x 0))
; (get-iset (string->symbol x)))))
;
; (define patterns
; (for/list ([x (in-list (manifest-pattern mf))])
; (match x
; [(? string?) (parse-string-pattern x)]
; [(? symbol?) (vector (get-iset x))]
; [(list start end) (vector (range->integer-set start end))]
; [_ (error "unrecognized pattern element in manifest" x)])))
;
; (values (manifest-name mf) (manifest-arch mf) (manifest-resources mf) (manifest-smp mf)
; (manifest-mode mf) (manifest-command mf) (apply vector-append patterns)))
(define (get-iset x)
(hash-ref isets x (lambda () (error "no such iset defined" x))))
(define (parse-string-pattern ptn)
(for/vector ([x (in-list (regexp-match* #rx"\\??." ptn))])
(if (= 1 (string-length x))
(char->integer-set (string-ref x 0))
(get-iset (string->symbol x)))))
; ;; test code (define patterns
; (for/list ([x (in-list manifest-pattern)])
; (define-values [name arch resources smp mode command pattern] (match x
; (parse-manifest [(? string?) (parse-string-pattern x)]
; [(? symbol?) (vector (get-iset x))]
; (pattern-codegen pattern mode) [(list start end) (vector (range->integer-set start end))]
; ;; inclusive range [_ (error "unrecognized pattern element in manifest" x)])))
; (define start (resolve-pattern-pos pattern (pos->pattern-pos pattern 10)))
; (define end (resolve-pattern-pos pattern (pos->pattern-pos pattern 21))) (define pattern (apply vector-append patterns))
; (printf "// args: ") (define size (pattern-count pattern))
; (for ([s (in-vector start)] [e (in-vector end)]) (manifest manifest-def pattern size))
; (apply printf "~a ~a ~a ~a "
; (map (lambda (x) (number->string x 16)) (list (car s) (cdr s) (car e) (cdr e))))) ;; get data from the manifest
; (printf "\n") (define/contract (manifest-data-get mf key [fail-thunk #f])
; (printf "// total: ~a\n" (pattern-count pattern)) (->* (manifest? symbol?) (any/c) any)
(or (rest (assoc key (manifest-data mf)))
(and (procedure? fail-thunk) (fail-thunk))
fail-thunk))
;; the struct contains the original data
(define serialize-manifest manifest-data)

View File

@ -16,15 +16,15 @@
;; You should have received a copy of the GNU Affero General Public License ;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require (only-in data/integer-set make-integer-set integer-set-contents [union integer-set-union] (require (only-in data/integer-set make-integer-set integer-set-contents integer-set?
[count integer-set-count] [intersect integer-set-intersect] [union integer-set-union] [count integer-set-count]
[subtract integer-set-subtract]) [intersect integer-set-intersect] [subtract integer-set-subtract])
racket/list racket/match racket/vector) racket/list racket/match racket/vector)
(provide char->integer-set string->integer-set range->integer-set (provide char->integer-set string->integer-set range->integer-set
;; re-export renamed integer-set accessors ;; re-export renamed integer-set accessors
make-integer-set integer-set-contents integer-set-count integer-set-union make-integer-set integer-set-contents integer-set-count integer-set-union
integer-set-intersect integer-set-subtract integer-set-intersect integer-set-subtract integer-set?
pattern-count pos->pattern-pos resolve-pattern-pos pattern-count pos->pattern-pos resolve-pattern-pos
pattern-range-take) pattern-range-take)