initial implementation of let-syntax

This commit is contained in:
JeffBezanson 2009-03-17 21:53:55 +00:00
parent 5edb75af2c
commit 7059a471a1
3 changed files with 52 additions and 31 deletions

View File

@ -67,6 +67,22 @@
t) t)
new-s)))))) new-s))))))
; convert to proper list, i.e. remove "dots", and append
(define (append.2 l tail)
(cond ((null? l) tail)
((atom? l) (cons l tail))
(#t (cons (car l) (append.2 (cdr l) tail)))))
; transform code by calling (f expr env) on each subexpr, where
; env is a list of lexical variables in effect at that point.
(define (lexical-walk f t)
(map&fold t () f
(lambda (tree state)
(if (and (eq? (car t) 'lambda)
(pair? (cdr t)))
(append.2 (cadr t) state)
state))))
; collapse forms like (&& (&& (&& (&& a b) c) d) e) to (&& a b c d e) ; collapse forms like (&& (&& (&& (&& a b) c) d) e) to (&& a b c d e)
(define (flatten-left-op op e) (define (flatten-left-op op e)
(maptree-post (lambda (node) (maptree-post (lambda (node)

View File

@ -98,3 +98,11 @@
body))) body)))
(map (lambda (x) #f) binds))) (map (lambda (x) #f) binds)))
(define (evalhead e env)
(if (and (symbol? e)
(or (constant? e)
(and (not (memq e env))
(bound? e)
(builtin? (eval e)))))
(eval e)
e))

View File

@ -101,36 +101,35 @@
(if f (apply f (cdr e)) (if f (apply f (cdr e))
e)))) e))))
; convert to proper list, i.e. remove "dots", and append
(define (append.2 l tail)
(cond ((null? l) tail)
((atom? l) (cons l tail))
(#t (cons (car l) (append.2 (cdr l) tail)))))
(define (cadr x) (car (cdr x))) (define (cadr x) (car (cdr x)))
(define (cddr x) (cdr (cdr x)))
(define (caddr x) (car (cdr (cdr x))))
(define (macroexpand e) (define (macroexpand e) (macroexpand-in e ()))
((label mexpand
(lambda (e env f) (define (macroexpand-in e env)
(begin (if (atom? e) e
(while (and (pair? e) (let ((f (assq (car e) env)))
(not (member (car e) env)) (if f
(set! f (macrocall? e))) (macroexpand-in (apply (cadr f) (cdr e)) (caddr f))
(set! e (apply f (cdr e)))) (let ((f (macrocall? e)))
(cond ((and (pair? e) (if f
(not (eq (car e) 'quote))) (macroexpand-in (apply f (cdr e)) env)
(let ((newenv (cond ((eq (car e) 'quote) e)
(if (and (eq (car e) 'lambda) ((eq (car e) 'let-syntax)
(pair? (cdr e))) (let ((binds (cadr e))
(append.2 (cadr e) env) (body (f-body (cddr e))))
env))) (macroexpand-in
(map (lambda (x) (mexpand x newenv ())) e))) body
;((and (symbol? e) (constant? e)) (eval e)) (nconc
;((and (symbol? e) (map (lambda (bind)
; (not (member e *special-forms*)) (list (car bind)
; (not (member e env))) (cons '%top e)) (macroexpand-in (cadr bind) env)
(#t e))))) env))
e () ())) binds)
env))))
(else
(map (lambda (x) (macroexpand-in x env)) e)))))))))
(define (delete-duplicates lst) (define (delete-duplicates lst)
(if (atom? lst) (if (atom? lst)
@ -195,11 +194,9 @@
(define (caar x) (car (car x))) (define (caar x) (car (car x)))
(define (cdar x) (cdr (car x))) (define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
(define (caaar x) (car (car (car x)))) (define (caaar x) (car (car (car x))))
(define (caadr x) (car (car (cdr x)))) (define (caadr x) (car (car (cdr x))))
(define (cadar x) (car (cdr (car x)))) (define (cadar x) (car (cdr (car x))))
(define (caddr x) (car (cdr (cdr x))))
(define (cadddr x) (car (cdr (cdr (cdr x))))) (define (cadddr x) (car (cdr (cdr (cdr x)))))
(define (cdaar x) (cdr (car (car x)))) (define (cdaar x) (cdr (car (car x))))
(define (cdadr x) (cdr (car (cdr x)))) (define (cdadr x) (cdr (car (cdr x))))
@ -596,7 +593,7 @@
(lambda (e) (begin (io.discardbuffer *input-stream*) (lambda (e) (begin (io.discardbuffer *input-stream*)
(raise e)))))) (raise e))))))
(and (not (io.eof? *input-stream*)) (and (not (io.eof? *input-stream*))
(let ((V (eval v))) (let ((V (eval (expand v))))
(print V) (print V)
(set! that V) (set! that V)
#t)))) #t))))