dillog
@gmail.com writes:
> On Apr 3, 3:49 pm, Pascal Bourguignon <p
@informatimago.com> wrote:
>> dillog
@gmail.com writes:
>> > hi
>> > how to rewrite "advance" scheme syntax with "old" scheme syntax then
>> > print out?
>> > define-syntax can recognize new syntax, but how to print them out or
>> > pp them out?
>> > I need to rewrite in situation like: (cadr x) => (car (cdr x))
>> > (define (f x) ...) => (define (lambda (x) ... etc.
>> > I need to do this because my simple interpreter can't handle them
>> > right now.
>> > I have a lots of test code to rewrite. thanks.
>> You could add a simple Q&D macro system to your interpreter.
> What's Q&D macro?
Quick & Dirty.
(define macros '()) ; an a list of macro definitions.
(define (define-macro name expander) (set! macros (cons (cons name expander) macros)))
(define (find-macro name) (let ((a (assoc name macros)))
(if a (cdr a) #f)))
And add to your eval:
(define (eval form ...)
(cond
...
((find-macro (car form)) (eval ((find-macro (car form)) form) ...))
...))
So now you can start to write macros such as:
(define-macro 'if (lambda (form)
(let ((test (second form))
(then (third form))
(else (fourth form)))
`(cond (,test ,then) (else ,else)))))
People here will cry, because there's no hygine, etc, that's why I
qualified it of quick and dirty. But it can help.
Now, of course, for define expanding to define it might be difficult.
You'd need a primitive define, let's name it %define (change your
eval), and then you can write:
(define-macro define (lambda (form)
(if (list? (second form))
; (define (f . args) . body)
`(%define ,(caadr form) (lambda ,(cdadr form) . ,(cddr form)))
`(%define . ,(cdr form)))))
and be happy.
--
__Pascal Bourguignon__
http://www.informatimago.com
http://pjb.ogamita.org