From d294034a6eb855846a75372278be5f9f08fcd3c7 Mon Sep 17 00:00:00 2001 From: haskal Date: Mon, 16 Nov 2020 01:01:28 -0500 Subject: [PATCH] make pattern definition flexible --- crossfire/main.rkt | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/crossfire/main.rkt b/crossfire/main.rkt index d53c6ec..6e34401 100644 --- a/crossfire/main.rkt +++ b/crossfire/main.rkt @@ -16,7 +16,7 @@ ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . -(require racket/bool racket/function racket/match +(require racket/bool racket/function racket/match racket/vector (for-syntax racket/base racket/syntax) "pattern.rkt" "codegen.rkt") @@ -61,19 +61,32 @@ (struct-copy manifest mf [isets (hash-set (manifest-isets mf) (symbol->string name) (string->integer-set val))])] - [(list 'pattern pattern) (struct-copy manifest mf [pattern pattern])]))) + [(list 'pattern pattern ...) (struct-copy manifest mf [pattern pattern])]))) (check-false name) (check-false command) (check-false pattern) (define isets (manifest-isets mf)) - (define pattern - (for/vector ([x (in-list (regexp-match* #rx"\\??." (manifest-pattern 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)) - (hash-ref isets x (lambda () (error "no such iset defined" x)))))) - (values (manifest-name mf) (manifest-mode mf) (manifest-command mf) pattern)) + (get-iset x)))) + + (define patterns + (for/list ([x (in-list (manifest-pattern mf))]) + (match x + [(? string?) (parse-string-pattern x)] + [(? symbol?) (vector (get-iset (symbol->string x)))] + [(list start end) (vector (range->integer-set start end))] + [_ (error "unrecognized pattern element in manifest" x)]))) + + (values (manifest-name mf) (manifest-mode mf) (manifest-command mf) + (apply vector-append patterns))) ;; test code @@ -83,7 +96,8 @@ '((name "test") (mode stdio) (command ("meme")) - (pattern "?d_?d")))) + (iset ?m "0123456789") + (pattern ?m "_?d")))) (pattern-codegen pattern mode (pattern-start pattern) (pattern-end pattern)) (printf "// total: ~a\n" (pattern-count pattern))