Added ER renaming example

This commit is contained in:
Justin Ethier 2015-08-26 19:15:45 -04:00
parent b7d1e3c3d2
commit 4db3b681c1

112
test2.scm
View file

@ -5,10 +5,36 @@
; ;
; ;
(import (scheme base) (import (scheme base)
(test-lib test) ;(test-lib test)
(scheme eval) (scheme eval)
(scheme write)) (scheme write))
(define-syntax swap!
(er-macro-transformer
(lambda (form rename compare?)
(let (
(x (cadr form))
(y (caddr form))
(%tmp (rename 'tmp))
(%let (rename 'let))
(%set! (rename 'set!))
)
`(,%let ((,%tmp ,x))
(,%set! ,x ,y)
(,%set! ,y ,%tmp))))))
(define x 'x)
(define y 'y)
(write `(,x ,y))
(swap! x y)
(write `(,x ,y))
(define tmp 'tmp)
(define y 'y)
(write `(,tmp ,y))
(swap! tmp y)
(write `(,tmp ,y))
;(define-syntax test ;(define-syntax test
; (er-macro-transformer ; (er-macro-transformer
; (lambda (expr rename compare) ; (lambda (expr rename compare)
@ -18,45 +44,45 @@
; ;
; WTF is the macro unable to be evaluated when the same code works as part of *defined-macros*??? ; WTF is the macro unable to be evaluated when the same code works as part of *defined-macros*???
; ;
(define-syntax test ;(define-syntax test
(er-macro-transformer ; (er-macro-transformer
(lambda (expr rename compare) ; (lambda (expr rename compare)
(cond ((null? (cdr expr)) #t) ; (cond ((null? (cdr expr)) #t)
; (cond ((null? (cdr expr))) ;; (cond ((null? (cdr expr)))
((null? (cddr expr)) (cadr expr)) ; ((null? (cddr expr)) (cadr expr))
(else (list (rename 'if) (cadr expr) ; (else (list (rename 'if) (cadr expr)
(cons (rename 'and) (cddr expr)) ; (cons (rename 'and) (cddr expr))
#f)))))) ; #f))))))
;
(define-syntax test2 ;(define-syntax test2
(er-macro-transformer ; (er-macro-transformer
(lambda (expr rename compare) ; (lambda (expr rename compare)
;; need some way to get these in the eval env ; ;; need some way to get these in the eval env
;; ; ;;
;; may need to maintain an environment in the compiler and pass it ; ;; may need to maintain an environment in the compiler and pass it
;; along to eval somehow when macro is expanded. would this just ; ;; along to eval somehow when macro is expanded. would this just
;; involve changes to expand? also, does that mean macro:expand ; ;; involve changes to expand? also, does that mean macro:expand
;; should call eval directly if a non-compiled macro is found? ; ;; should call eval directly if a non-compiled macro is found?
;; if that is the case, macro:expand would also need to receive ; ;; if that is the case, macro:expand would also need to receive
;; the env parameter so it could pass that along to. ; ;; the env parameter so it could pass that along to.
;; tbd how this parameter would be combined with eval's global env, ; ;; tbd how this parameter would be combined with eval's global env,
;; because it would need to extend it. ; ;; because it would need to extend it.
;; could eval expose a function to extend the global env (or any env)? ; ;; could eval expose a function to extend the global env (or any env)?
(test 1 2 3) ; (test 1 2 3)
(test 1 2 3) ; breaks ; (test 1 2 3) ; breaks
(my-or 1 2 3) ; breaks ; (my-or 1 2 3) ; breaks
(and ''test ''test2)))) ; (and ''test ''test2))))
;
(write (test2 1 2 3)) ;(write (test2 1 2 3))
(write (test 1 2 3)) ;(write (test 1 2 3))
(write (my-or 1 2 3 'or)) ;(write (my-or 1 2 3 'or))
(write (my-or #f 2 3 'or)) ;(write (my-or #f 2 3 'or))
;(test 'done) ;;(test 'done)
'done ;'done
;
(define x 1) ;(define x 1)
(write x) ;(write x)
(write ;(write
(eval 'my-or)) ; (eval 'my-or))
(write ;(write
(eval '(my-or x 1 2 x))) ; (eval '(my-or x 1 2 x)))