2014-07-19 01:59:34 -04:00
|
|
|
(define-library (picrin macro)
|
2014-09-08 04:08:38 -04:00
|
|
|
(import (picrin base))
|
2014-07-19 01:59:34 -04:00
|
|
|
|
2015-06-10 06:42:29 -04:00
|
|
|
;; macro primitives
|
|
|
|
|
|
|
|
(export define-macro
|
2015-06-09 09:36:04 -04:00
|
|
|
make-identifier
|
2015-06-10 06:42:29 -04:00
|
|
|
identifier?
|
2016-02-06 09:15:53 -05:00
|
|
|
identifier=?
|
2016-02-20 02:00:37 -05:00
|
|
|
identifier-base
|
2016-02-06 09:15:53 -05:00
|
|
|
identifier-environment)
|
2015-06-10 06:42:29 -04:00
|
|
|
|
|
|
|
;; simple macro
|
|
|
|
|
|
|
|
(export define-syntax
|
|
|
|
syntax-quote
|
|
|
|
syntax-quasiquote
|
|
|
|
syntax-unquote
|
|
|
|
syntax-unquote-splicing)
|
|
|
|
|
|
|
|
;; misc transformers
|
|
|
|
|
|
|
|
(export call-with-current-environment
|
2015-06-09 09:36:04 -04:00
|
|
|
make-syntactic-closure
|
|
|
|
close-syntax
|
2015-06-10 06:42:29 -04:00
|
|
|
strip-syntax
|
2015-06-09 09:36:04 -04:00
|
|
|
sc-macro-transformer
|
|
|
|
rsc-macro-transformer
|
|
|
|
er-macro-transformer
|
2015-06-10 06:42:29 -04:00
|
|
|
ir-macro-transformer)
|
|
|
|
|
|
|
|
|
|
|
|
(define-macro call-with-current-environment
|
|
|
|
(lambda (form env)
|
|
|
|
`(,(cadr form) ',env)))
|
2014-07-19 05:14:11 -04:00
|
|
|
|
2014-07-19 05:26:03 -04:00
|
|
|
|
2015-06-10 06:42:29 -04:00
|
|
|
;; syntactic closure
|
2014-07-19 05:26:03 -04:00
|
|
|
|
2015-06-10 06:42:29 -04:00
|
|
|
|
|
|
|
(define (make-syntactic-closure env free form)
|
|
|
|
(letrec
|
2017-03-31 02:59:22 -04:00
|
|
|
((wrap (let ((ephemeron (make-ephemeron-table)))
|
2015-06-10 06:42:29 -04:00
|
|
|
(lambda (var)
|
2016-02-10 07:57:20 -05:00
|
|
|
(let ((id (ephemeron var)))
|
2015-07-18 02:28:53 -04:00
|
|
|
(if id
|
|
|
|
(cdr id)
|
2015-06-10 06:42:29 -04:00
|
|
|
(let ((id (make-identifier var env)))
|
2016-02-10 07:57:20 -05:00
|
|
|
(ephemeron var id)
|
2015-07-18 02:28:53 -04:00
|
|
|
id))))))
|
2015-06-10 06:42:29 -04:00
|
|
|
(walk (lambda (f form)
|
|
|
|
(cond
|
2016-02-06 09:15:53 -05:00
|
|
|
((identifier? form)
|
2015-06-10 06:42:29 -04:00
|
|
|
(f form))
|
|
|
|
((pair? form)
|
|
|
|
(cons (walk f (car form)) (walk f (cdr form))))
|
|
|
|
((vector? form)
|
|
|
|
(list->vector (walk f (vector->list form))))
|
|
|
|
(else
|
|
|
|
form)))))
|
|
|
|
(letrec
|
|
|
|
((f (lambda (var)
|
|
|
|
(let loop ((free free))
|
|
|
|
(if (null? free)
|
2016-09-12 09:32:04 -04:00
|
|
|
(wrap var)
|
2016-02-06 09:15:53 -05:00
|
|
|
(if (identifier=? var (car free))
|
2015-06-10 06:42:29 -04:00
|
|
|
var
|
|
|
|
(loop (cdr free))))))))
|
|
|
|
(walk f form))))
|
2014-07-19 01:59:34 -04:00
|
|
|
|
|
|
|
(define (close-syntax form env)
|
|
|
|
(make-syntactic-closure env '() form))
|
|
|
|
|
2015-06-10 06:42:29 -04:00
|
|
|
(define (strip-syntax form)
|
|
|
|
(letrec
|
|
|
|
((unwrap (lambda (var)
|
2016-02-20 02:00:37 -05:00
|
|
|
(identifier-base var)))
|
2015-06-10 06:42:29 -04:00
|
|
|
(walk (lambda (f form)
|
|
|
|
(cond
|
2016-02-06 09:15:53 -05:00
|
|
|
((identifier? form)
|
2015-06-10 06:42:29 -04:00
|
|
|
(f form))
|
|
|
|
((pair? form)
|
|
|
|
(cons (walk f (car form)) (walk f (cdr form))))
|
|
|
|
((vector? form)
|
|
|
|
(list->vector (walk f (vector->list form))))
|
|
|
|
(else
|
|
|
|
form)))))
|
|
|
|
(walk unwrap form)))
|
|
|
|
|
|
|
|
|
|
|
|
;; transformers
|
|
|
|
|
|
|
|
|
|
|
|
(define (sc-transformer f)
|
|
|
|
(lambda (form use-env mac-env)
|
|
|
|
(make-syntactic-closure mac-env '() (f form use-env))))
|
|
|
|
|
|
|
|
(define (rsc-transformer f)
|
|
|
|
(lambda (form use-env mac-env)
|
|
|
|
(make-syntactic-closure use-env '() (f form mac-env))))
|
|
|
|
|
|
|
|
(define (er-transformer f)
|
|
|
|
(lambda (form use-env mac-env)
|
|
|
|
(letrec
|
2017-03-31 02:59:22 -04:00
|
|
|
((rename (let ((ephemeron (make-ephemeron-table)))
|
2015-06-10 06:42:29 -04:00
|
|
|
(lambda (var)
|
2016-02-10 07:57:20 -05:00
|
|
|
(let ((id (ephemeron var)))
|
2015-07-18 02:28:53 -04:00
|
|
|
(if id
|
|
|
|
(cdr id)
|
2015-06-10 06:42:29 -04:00
|
|
|
(let ((id (make-identifier var mac-env)))
|
2016-02-10 07:57:20 -05:00
|
|
|
(ephemeron var id)
|
2015-07-18 02:28:53 -04:00
|
|
|
id))))))
|
2015-06-10 06:42:29 -04:00
|
|
|
(compare (lambda (x y)
|
2016-02-06 09:15:53 -05:00
|
|
|
(identifier=?
|
2015-06-10 06:42:29 -04:00
|
|
|
(make-identifier x use-env)
|
|
|
|
(make-identifier y use-env)))))
|
|
|
|
(f form rename compare))))
|
|
|
|
|
|
|
|
(define (ir-transformer f)
|
|
|
|
(lambda (form use-env mac-env)
|
2017-03-31 02:59:22 -04:00
|
|
|
(let ((ephemeron1 (make-ephemeron-table))
|
|
|
|
(ephemeron2 (make-ephemeron-table)))
|
2015-06-10 06:42:29 -04:00
|
|
|
(letrec
|
|
|
|
((inject (lambda (var1)
|
2016-02-10 07:57:20 -05:00
|
|
|
(let ((var2 (ephemeron1 var1)))
|
2015-07-18 02:28:53 -04:00
|
|
|
(if var2
|
|
|
|
(cdr var2)
|
2015-06-10 06:42:29 -04:00
|
|
|
(let ((var2 (make-identifier var1 use-env)))
|
2016-02-10 07:57:20 -05:00
|
|
|
(ephemeron1 var1 var2)
|
|
|
|
(ephemeron2 var2 var1)
|
2015-07-18 02:28:53 -04:00
|
|
|
var2)))))
|
2017-03-31 02:59:22 -04:00
|
|
|
(rename (let ((ephemeron (make-ephemeron-table)))
|
2015-06-10 06:42:29 -04:00
|
|
|
(lambda (var)
|
2016-02-10 07:57:20 -05:00
|
|
|
(let ((id (ephemeron var)))
|
2015-07-18 02:28:53 -04:00
|
|
|
(if id
|
|
|
|
(cdr id)
|
2015-06-10 06:42:29 -04:00
|
|
|
(let ((id (make-identifier var mac-env)))
|
2016-02-10 07:57:20 -05:00
|
|
|
(ephemeron var id)
|
2015-07-18 02:28:53 -04:00
|
|
|
id))))))
|
2015-06-10 06:42:29 -04:00
|
|
|
(flip (lambda (var2) ; unwrap if injected, wrap if not injected
|
2016-02-10 07:57:20 -05:00
|
|
|
(let ((var1 (ephemeron2 var2)))
|
2015-07-18 02:28:53 -04:00
|
|
|
(if var1
|
|
|
|
(cdr var1)
|
|
|
|
(rename var2)))))
|
2015-06-10 06:42:29 -04:00
|
|
|
(walk (lambda (f form)
|
|
|
|
(cond
|
2016-02-06 09:15:53 -05:00
|
|
|
((identifier? form)
|
2015-06-10 06:42:29 -04:00
|
|
|
(f form))
|
|
|
|
((pair? form)
|
|
|
|
(cons (walk f (car form)) (walk f (cdr form))))
|
|
|
|
((vector? form)
|
|
|
|
(list->vector (walk f (vector->list form))))
|
|
|
|
(else
|
|
|
|
form))))
|
|
|
|
(compare (lambda (x y)
|
2016-02-06 09:15:53 -05:00
|
|
|
(identifier=?
|
2015-06-10 06:42:29 -04:00
|
|
|
(make-identifier x mac-env)
|
|
|
|
(make-identifier y mac-env)))))
|
|
|
|
(walk flip (f (walk inject form) inject compare))))))
|
|
|
|
|
|
|
|
(define-macro sc-macro-transformer
|
|
|
|
(lambda (f mac-env)
|
|
|
|
#`(lambda (form use-env)
|
|
|
|
((sc-transformer #,(cadr f)) form use-env #,mac-env))))
|
|
|
|
|
|
|
|
(define-macro rsc-macro-transformer
|
|
|
|
(lambda (f mac-env)
|
|
|
|
#`(lambda (form use-env)
|
|
|
|
((rsc-transformer #,(cadr f)) form use-env #,mac-env))))
|
|
|
|
|
|
|
|
(define-macro er-macro-transformer
|
|
|
|
(lambda (f mac-env)
|
|
|
|
#`(lambda (form use-env)
|
|
|
|
((er-transformer #,(cadr f)) form use-env #,mac-env))))
|
|
|
|
|
|
|
|
(define-macro ir-macro-transformer
|
|
|
|
(lambda (f mac-env)
|
|
|
|
#`(lambda (form use-env)
|
|
|
|
((ir-transformer #,(cadr f)) form use-env #,mac-env)))))
|