2016-07-17 20:06:28 +00:00
|
|
|
;; -*-scheme-*-
|
2016-07-23 07:47:15 +00:00
|
|
|
|
2016-07-16 19:53:32 +00:00
|
|
|
(define (syntax-error message thing)
|
|
|
|
(display "syntax-error:")
|
|
|
|
(display message)
|
|
|
|
(display ":")
|
2016-07-23 12:39:33 +00:00
|
|
|
(display thing)
|
2016-07-16 19:53:32 +00:00
|
|
|
(newline))
|
2016-07-16 05:56:50 +00:00
|
|
|
|
2016-07-23 07:58:23 +00:00
|
|
|
;;; Adapted from scheme48-1.1/scheme/alt/syntax.scm
|
|
|
|
|
|
|
|
;;; Copyright (c) 1993-2004 by Richard Kelsey and Jonathan Rees. See file COPYING.
|
|
|
|
|
|
|
|
;;; scheme48-1.1/COPYING
|
|
|
|
|
|
|
|
;; Copyright (c) 1993-2004 Richard Kelsey and Jonathan Rees
|
|
|
|
;; All rights reserved.
|
|
|
|
|
|
|
|
;; Redistribution and use in source and binary forms, with or without
|
|
|
|
;; modification, are permitted provided that the following conditions
|
|
|
|
;; are met:
|
|
|
|
;; 1. Redistributions of source code must retain the above copyright
|
|
|
|
;; notice, this list of conditions and the following disclaimer.
|
|
|
|
;; 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
;; notice, this list of conditions and the following disclaimer in the
|
|
|
|
;; documentation and/or other materials provided with the distribution.
|
|
|
|
;; 3. The name of the authors may not be used to endorse or promote products
|
|
|
|
;; derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
2016-07-16 22:03:14 +00:00
|
|
|
(display "mes:define-syntax...")
|
2016-07-16 05:56:50 +00:00
|
|
|
|
2016-07-23 09:51:06 +00:00
|
|
|
(define-macro (define-syntax macro-name transformer . stuff)
|
2016-07-16 19:53:32 +00:00
|
|
|
`(define-macro (,macro-name . args)
|
2016-07-16 22:03:14 +00:00
|
|
|
(,transformer (cons ',macro-name args)
|
2016-07-17 08:38:29 +00:00
|
|
|
(lambda (x) x)
|
2016-07-22 15:13:51 +00:00
|
|
|
eq?)))
|
2016-07-16 19:53:32 +00:00
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
;; Rewrite-rule compiler (a.k.a. "extend-syntax")
|
2016-07-16 19:53:32 +00:00
|
|
|
|
2016-07-22 15:13:51 +00:00
|
|
|
;; Example:
|
|
|
|
;;
|
|
|
|
;; (define-syntax or
|
|
|
|
;; (syntax-rules ()
|
|
|
|
;; ((or) #f)
|
|
|
|
;; ((or e) e)
|
|
|
|
;; ((or e1 e ...) (let ((temp e1))
|
|
|
|
;; (if temp temp (or e ...))))))
|
2016-10-18 20:19:57 +00:00
|
|
|
|
|
|
|
(newline)
|
2016-07-16 05:56:50 +00:00
|
|
|
|
2016-07-16 22:03:14 +00:00
|
|
|
(display "mes:define-syntax syntax-rules...")
|
2016-07-16 19:53:32 +00:00
|
|
|
(newline)
|
|
|
|
|
2016-07-23 09:51:06 +00:00
|
|
|
(define-syntax syntax-rules
|
2016-07-17 20:15:31 +00:00
|
|
|
(let ()
|
2016-07-22 10:23:23 +00:00
|
|
|
(define name? symbol?)
|
|
|
|
|
|
|
|
(define (segment-pattern? pattern)
|
|
|
|
(and (segment-template? pattern)
|
|
|
|
(or (null? (cddr pattern))
|
|
|
|
(syntax-error "segment matching not implemented" pattern))))
|
2016-07-17 09:37:22 +00:00
|
|
|
|
2016-07-22 10:23:23 +00:00
|
|
|
(define (segment-template? pattern)
|
|
|
|
(and (pair? pattern)
|
|
|
|
(pair? (cdr pattern))
|
|
|
|
(memq (cadr pattern) indicators-for-zero-or-more)))
|
2016-07-17 09:37:22 +00:00
|
|
|
|
2016-07-22 10:23:23 +00:00
|
|
|
(define indicators-for-zero-or-more (list (string->symbol "...") '---))
|
2016-07-22 15:13:51 +00:00
|
|
|
|
2016-07-17 20:15:31 +00:00
|
|
|
(lambda (exp r c)
|
2016-07-17 09:37:22 +00:00
|
|
|
|
2016-07-17 20:15:31 +00:00
|
|
|
(define %input (r '%input)) ;Gensym these, if you like.
|
|
|
|
(define %compare (r '%compare))
|
|
|
|
(define %rename (r '%rename))
|
|
|
|
(define %tail (r '%tail))
|
|
|
|
(define %temp (r '%temp))
|
|
|
|
|
|
|
|
(define rules (cddr exp))
|
|
|
|
(define subkeywords (cadr exp))
|
|
|
|
|
|
|
|
(define (make-transformer rules)
|
2016-07-22 15:13:51 +00:00
|
|
|
`(lambda (,%input ,%rename ,%compare)
|
2016-07-17 20:15:31 +00:00
|
|
|
(let ((,%tail (cdr ,%input)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(cond ,@(map process-rule rules)
|
|
|
|
(else
|
2016-07-17 20:15:31 +00:00
|
|
|
(syntax-error
|
|
|
|
"use of macro doesn't match definition"
|
|
|
|
,%input))))))
|
|
|
|
|
|
|
|
(define (process-rule rule)
|
2016-07-22 15:13:51 +00:00
|
|
|
(cond ((and (pair? rule)
|
2016-07-17 20:15:31 +00:00
|
|
|
(pair? (cdr rule))
|
|
|
|
(null? (cddr rule)))
|
|
|
|
(let ((pattern (cdar rule))
|
|
|
|
(template (cadr rule)))
|
|
|
|
`((and ,@(process-match %tail pattern))
|
|
|
|
(let* ,(process-pattern pattern
|
|
|
|
%tail
|
|
|
|
(lambda (x) x))
|
|
|
|
,(process-template template
|
|
|
|
0
|
|
|
|
(meta-variables pattern 0 '()))))))
|
|
|
|
(syntax-error "ill-formed syntax rule" rule)))
|
|
|
|
|
|
|
|
;; Generate code to test whether input expression matches pattern
|
|
|
|
|
|
|
|
(define (process-match input pattern)
|
2016-07-22 15:13:51 +00:00
|
|
|
(cond ((name? pattern)
|
2016-07-17 20:15:31 +00:00
|
|
|
(cond ((member pattern subkeywords)
|
|
|
|
`((,%compare ,input (,%rename ',pattern))))
|
|
|
|
(#t `())))
|
|
|
|
((segment-pattern? pattern)
|
|
|
|
(process-segment-match input (car pattern)))
|
|
|
|
((pair? pattern)
|
|
|
|
`((let ((,%temp ,input))
|
|
|
|
(and (pair? ,%temp)
|
|
|
|
,@(process-match `(car ,%temp) (car pattern))
|
|
|
|
,@(process-match `(cdr ,%temp) (cdr pattern))))))
|
|
|
|
((or (null? pattern) (boolean? pattern) (char? pattern))
|
|
|
|
`((eq? ,input ',pattern)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else
|
2016-07-17 20:15:31 +00:00
|
|
|
`((equal? ,input ',pattern)))))
|
|
|
|
|
|
|
|
(define (process-segment-match input pattern)
|
|
|
|
(let ((conjuncts (process-match '(car l) pattern)))
|
|
|
|
(cond ((null? conjuncts)
|
|
|
|
`((list? ,input))) ;+++
|
|
|
|
(#t `((let loop ((l ,input))
|
|
|
|
(or (null? l)
|
|
|
|
(and (pair? l)
|
|
|
|
,@conjuncts
|
|
|
|
(loop (cdr l))))))))))
|
|
|
|
|
|
|
|
;; Generate code to take apart the input expression
|
|
|
|
;; This is pretty bad, but it seems to work (can't say why).
|
|
|
|
|
|
|
|
(define (process-pattern pattern path mapit)
|
|
|
|
(cond ((name? pattern)
|
|
|
|
(cond ((memq pattern subkeywords)
|
|
|
|
'())
|
|
|
|
(#t
|
|
|
|
(list (list pattern (mapit path))))))
|
|
|
|
((segment-pattern? pattern)
|
|
|
|
(process-pattern (car pattern)
|
|
|
|
%temp
|
|
|
|
(lambda (x) ;temp is free in x
|
|
|
|
(mapit (cond ((eq? %temp x)
|
|
|
|
path) ;+++
|
|
|
|
(#t
|
|
|
|
`(map (lambda (,%temp) ,x)
|
|
|
|
,path)))))))
|
|
|
|
((pair? pattern)
|
|
|
|
(append (process-pattern (car pattern) `(car ,path) mapit)
|
|
|
|
(process-pattern (cdr pattern) `(cdr ,path) mapit)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else '())))
|
2016-07-17 20:15:31 +00:00
|
|
|
|
|
|
|
;; Generate code to compose the output expression according to template
|
|
|
|
|
|
|
|
(define (process-template template rank env)
|
|
|
|
(cond ((name? template)
|
|
|
|
(let ((probe (assq template env)))
|
|
|
|
(cond (probe
|
|
|
|
(cond ((<= (cdr probe) rank)
|
|
|
|
template)
|
|
|
|
(#t (syntax-error "template rank error (too few ...'s?)"
|
|
|
|
template))))
|
|
|
|
(#t `(,%rename ',template)))))
|
|
|
|
((segment-template? template)
|
|
|
|
(let ((vars
|
|
|
|
(free-meta-variables (car template) (+ rank 1) env '())))
|
|
|
|
(cond ((null? vars)
|
|
|
|
(syntax-error "too many ...'s" template))
|
|
|
|
(#t (let* ((x (process-template (car template)
|
|
|
|
(+ rank 1)
|
|
|
|
env))
|
|
|
|
(gen (cond ((equal? (list x) vars)
|
|
|
|
x) ;+++
|
|
|
|
(#t `(map (lambda ,vars ,x)
|
|
|
|
,@vars)))))
|
|
|
|
(cond ((null? (cddr template))
|
|
|
|
gen) ;+++
|
2016-07-22 15:13:51 +00:00
|
|
|
(else
|
|
|
|
`(append ,gen ,(process-template (cddr template)
|
|
|
|
rank env)))))))))
|
2016-07-17 20:15:31 +00:00
|
|
|
((pair? template)
|
|
|
|
`(cons ,(process-template (car template) rank env)
|
|
|
|
,(process-template (cdr template) rank env)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else `(quote ,template))))
|
2016-07-17 20:15:31 +00:00
|
|
|
|
|
|
|
;; Return an association list of (var . rank)
|
|
|
|
|
|
|
|
(define (meta-variables pattern rank vars)
|
|
|
|
(cond ((name? pattern)
|
|
|
|
(cond ((memq pattern subkeywords)
|
|
|
|
vars)
|
2016-07-22 15:13:51 +00:00
|
|
|
(else (cons (cons pattern rank) vars))))
|
2016-07-17 20:15:31 +00:00
|
|
|
((segment-pattern? pattern)
|
|
|
|
(meta-variables (car pattern) (+ rank 1) vars))
|
|
|
|
((pair? pattern)
|
|
|
|
(meta-variables (car pattern) rank
|
|
|
|
(meta-variables (cdr pattern) rank vars)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else vars)))
|
2016-07-17 20:15:31 +00:00
|
|
|
|
|
|
|
;; Return a list of meta-variables of given higher rank
|
|
|
|
|
|
|
|
(define (free-meta-variables template rank env free)
|
|
|
|
(cond ((name? template)
|
|
|
|
(cond ((and (not (memq template free))
|
|
|
|
(let ((probe (assq template env)))
|
|
|
|
(and probe (>= (cdr probe) rank))))
|
|
|
|
(cons template free))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else free)))
|
2016-07-17 20:15:31 +00:00
|
|
|
((segment-template? template)
|
|
|
|
(free-meta-variables (car template)
|
|
|
|
rank env
|
|
|
|
(free-meta-variables (cddr template)
|
|
|
|
rank env free)))
|
|
|
|
((pair? template)
|
|
|
|
(free-meta-variables (car template)
|
|
|
|
rank env
|
|
|
|
(free-meta-variables (cdr template)
|
|
|
|
rank env free)))
|
2016-07-22 15:13:51 +00:00
|
|
|
(else free)))
|
2016-07-17 20:15:31 +00:00
|
|
|
|
|
|
|
c ;ignored
|
|
|
|
|
|
|
|
;; Kludge for Scheme48 linker.
|
|
|
|
;; `(cons ,(make-transformer rules)
|
|
|
|
;; ',(find-free-names-in-syntax-rules subkeywords rules))
|
|
|
|
|
|
|
|
(make-transformer rules))))
|
2016-07-16 19:53:32 +00:00
|
|
|
|
2016-07-23 09:51:06 +00:00
|
|
|
(define-macro (define-syntax-rule id-pattern . template)
|
|
|
|
`(define-syntax ,(car id-pattern)
|
|
|
|
(syntax-rules ()
|
|
|
|
((,(car id-pattern) . ,(cdr id-pattern)) ,@template))))
|