syntax-rules: constant pattern support

This commit is contained in:
Yuichi Nishiwaki 2015-06-13 18:23:46 +09:00
parent 86ba26b02e
commit dfcf8c73bd
1 changed files with 12 additions and 9 deletions

View File

@ -40,19 +40,22 @@
(map (lambda (s) `(,(car s) . ,(f (cdr s)))) assoc)) (map (lambda (s) `(,(car s) . ,(f (cdr s)))) assoc))
;; TODO ;; TODO
;; - constants
;; - literals ;; - literals
;; - splicing ;; - splicing
;; - placeholder ;; - placeholder
;; - vector ;; - vector
;; p ::= () ;; p ::= constant
;; | var ;; | var
;; | (p . p) ;; | (p . p)
;; | (p ...) ;; | (p ...)
(define (compile ellipsis literals rules) (define (compile ellipsis literals rules)
(define (constant? obj)
(and (not (pair? obj))
(not (variable? obj))))
(define (many? pat) (define (many? pat)
(and (pair? pat) (and (pair? pat)
(pair? (cdr pat)) (pair? (cdr pat))
@ -65,8 +68,8 @@
((pattern-validator ((pattern-validator
(lambda (pat form) (lambda (pat form)
(cond (cond
((null? pat) ((constant? pat)
#`(null? #,form)) #`(equal? '#,pat #,form))
((variable? pat) ((variable? pat)
#t) #t)
((many? pat) ((many? pat)
@ -83,7 +86,7 @@
(define (pattern-variables pat) ; pattern -> (freevar) (define (pattern-variables pat) ; pattern -> (freevar)
(cond (cond
((null? pat) ((constant? pat)
'()) '())
((variable? pat) ((variable? pat)
`(,pat)) `(,pat))
@ -95,7 +98,7 @@
(define (pattern-levels pat) ; pattern -> ((var * int)) (define (pattern-levels pat) ; pattern -> ((var * int))
(cond (cond
((null? pat) ((constant? pat)
'()) '())
((variable? pat) ((variable? pat)
`((,pat . 0))) `((,pat . 0)))
@ -110,7 +113,7 @@
((pattern-selectors ((pattern-selectors
(lambda (pat form) (lambda (pat form)
(cond (cond
((null? pat) ((constant? pat)
'()) '())
((variable? pat) ((variable? pat)
`((,pat . ,form))) `((,pat . ,form)))
@ -124,8 +127,8 @@
(define (template-representation pat levels selectors) (define (template-representation pat levels selectors)
(cond (cond
((null? pat) ((constant? pat)
'()) pat)
((variable? pat) ((variable? pat)
(let ((it (assq pat levels))) (let ((it (assq pat levels)))
(if it (if it