make pattern definition flexible

This commit is contained in:
xenia 2020-11-16 01:01:28 -05:00
parent 64bbf52590
commit d294034a6e
1 changed files with 21 additions and 7 deletions

View File

@ -16,7 +16,7 @@
;; 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/>.
(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))