mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Added ER renaming example
This commit is contained in:
parent
b7d1e3c3d2
commit
4db3b681c1
1 changed files with 69 additions and 43 deletions
112
test2.scm
112
test2.scm
|
@ -5,10 +5,36 @@
|
|||
;
|
||||
;
|
||||
(import (scheme base)
|
||||
(test-lib test)
|
||||
;(test-lib test)
|
||||
(scheme eval)
|
||||
(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
|
||||
; (er-macro-transformer
|
||||
; (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*???
|
||||
;
|
||||
(define-syntax test
|
||||
(er-macro-transformer
|
||||
(lambda (expr rename compare)
|
||||
(cond ((null? (cdr expr)) #t)
|
||||
; (cond ((null? (cdr expr)))
|
||||
((null? (cddr expr)) (cadr expr))
|
||||
(else (list (rename 'if) (cadr expr)
|
||||
(cons (rename 'and) (cddr expr))
|
||||
#f))))))
|
||||
|
||||
(define-syntax test2
|
||||
(er-macro-transformer
|
||||
(lambda (expr rename compare)
|
||||
;; need some way to get these in the eval env
|
||||
;;
|
||||
;; may need to maintain an environment in the compiler and pass it
|
||||
;; along to eval somehow when macro is expanded. would this just
|
||||
;; involve changes to expand? also, does that mean macro:expand
|
||||
;; should call eval directly if a non-compiled macro is found?
|
||||
;; if that is the case, macro:expand would also need to receive
|
||||
;; the env parameter so it could pass that along to.
|
||||
;; tbd how this parameter would be combined with eval's global env,
|
||||
;; because it would need to extend it.
|
||||
;; could eval expose a function to extend the global env (or any env)?
|
||||
(test 1 2 3)
|
||||
(test 1 2 3) ; breaks
|
||||
(my-or 1 2 3) ; breaks
|
||||
(and ''test ''test2))))
|
||||
|
||||
(write (test2 1 2 3))
|
||||
(write (test 1 2 3))
|
||||
(write (my-or 1 2 3 'or))
|
||||
(write (my-or #f 2 3 'or))
|
||||
;(test 'done)
|
||||
'done
|
||||
|
||||
(define x 1)
|
||||
(write x)
|
||||
(write
|
||||
(eval 'my-or))
|
||||
(write
|
||||
(eval '(my-or x 1 2 x)))
|
||||
;(define-syntax test
|
||||
; (er-macro-transformer
|
||||
; (lambda (expr rename compare)
|
||||
; (cond ((null? (cdr expr)) #t)
|
||||
;; (cond ((null? (cdr expr)))
|
||||
; ((null? (cddr expr)) (cadr expr))
|
||||
; (else (list (rename 'if) (cadr expr)
|
||||
; (cons (rename 'and) (cddr expr))
|
||||
; #f))))))
|
||||
;
|
||||
;(define-syntax test2
|
||||
; (er-macro-transformer
|
||||
; (lambda (expr rename compare)
|
||||
; ;; need some way to get these in the eval env
|
||||
; ;;
|
||||
; ;; may need to maintain an environment in the compiler and pass it
|
||||
; ;; along to eval somehow when macro is expanded. would this just
|
||||
; ;; involve changes to expand? also, does that mean macro:expand
|
||||
; ;; should call eval directly if a non-compiled macro is found?
|
||||
; ;; if that is the case, macro:expand would also need to receive
|
||||
; ;; the env parameter so it could pass that along to.
|
||||
; ;; tbd how this parameter would be combined with eval's global env,
|
||||
; ;; because it would need to extend it.
|
||||
; ;; could eval expose a function to extend the global env (or any env)?
|
||||
; (test 1 2 3)
|
||||
; (test 1 2 3) ; breaks
|
||||
; (my-or 1 2 3) ; breaks
|
||||
; (and ''test ''test2))))
|
||||
;
|
||||
;(write (test2 1 2 3))
|
||||
;(write (test 1 2 3))
|
||||
;(write (my-or 1 2 3 'or))
|
||||
;(write (my-or #f 2 3 'or))
|
||||
;;(test 'done)
|
||||
;'done
|
||||
;
|
||||
;(define x 1)
|
||||
;(write x)
|
||||
;(write
|
||||
; (eval 'my-or))
|
||||
;(write
|
||||
; (eval '(my-or x 1 2 x)))
|
||||
|
|
Loading…
Add table
Reference in a new issue