- ((and (listp x) (eq (car x) 'lambda))
- '(lambda repeat (eval)))
- (t
- ;; FIXME: In an ideal world we would do something similar to
- ;; COMPILER-ERROR here, replacing the form within the walker
- ;; with an error-signalling form. This is slightly less
- ;; pretty, but informative non the less. Best is the enemy of
- ;; good, etc.
- (error "Illegal function call in method body:~% ~S"
- context))))
+ ((and (listp x) (eq (car x) 'lambda))
+ '(lambda repeat (eval)))
+ (t
+ ;; FIXME: In an ideal world we would do something similar to
+ ;; COMPILER-ERROR here, replacing the form within the walker
+ ;; with an error-signalling form. This is slightly less
+ ;; pretty, but informative non the less. Best is the enemy of
+ ;; good, etc.
+ (error "Illegal function call in method body:~% ~S"
+ context))))
- (cond
- (walk-no-more-p newform)
- ((not (eq form newform))
- (walk-form-internal newform context env))
- ((not (consp newform))
- (let ((symmac (car (variable-symbol-macro-p newform env))))
- (if symmac
- (let ((newnewform (walk-form-internal (cddr symmac)
- context
- env)))
- (if (eq newnewform (cddr symmac))
- (if *walk-form-expand-macros-p* newnewform newform)
- newnewform))
- newform)))
- (t
- (let* ((fn (car newform))
- (template (get-walker-template fn newform)))
- (if template
- (if (symbolp template)
- (funcall template newform context env)
- (walk-template newform template context env))
- (multiple-value-bind (newnewform macrop)
- (walker-environment-bind
- (new-env env :walk-form newform)
- (sb-xc:macroexpand-1 newform new-env))
- (cond
- (macrop
- (let ((newnewnewform (walk-form-internal newnewform
- context
- env)))
- (if (eq newnewnewform newnewform)
- (if *walk-form-expand-macros-p* newnewform newform)
- newnewnewform)))
- ((and (symbolp fn)
- (not (fboundp fn))
- (special-operator-p fn))
- ;; This shouldn't happen, since this walker is now
- ;; maintained as part of SBCL, so it should know
- ;; about all the special forms that SBCL knows
- ;; about.
- (bug "unexpected special form ~S" fn))
- (t
- ;; Otherwise, walk the form as if it's just a
- ;; standard function call using a template for
- ;; standard function call.
- (walk-template
- newnewform '(call repeat (eval)) context env))))))))))))
+ (cond
+ (walk-no-more-p newform)
+ ((not (eq form newform))
+ (walk-form-internal newform context env))
+ ((not (consp newform))
+ (let ((symmac (car (variable-symbol-macro-p newform env))))
+ (if symmac
+ (let ((newnewform (walk-form-internal (cddr symmac)
+ context
+ env)))
+ (if (eq newnewform (cddr symmac))
+ (if *walk-form-expand-macros-p* newnewform newform)
+ newnewform))
+ newform)))
+ (t
+ (let* ((fn (car newform))
+ (template (get-walker-template fn newform)))
+ (if template
+ (if (symbolp template)
+ (funcall template newform context env)
+ (walk-template newform template context env))
+ (multiple-value-bind (newnewform macrop)
+ (walker-environment-bind
+ (new-env env :walk-form newform)
+ (sb-xc:macroexpand-1 newform new-env))
+ (cond
+ (macrop
+ (let ((newnewnewform (walk-form-internal newnewform
+ context
+ env)))
+ (if (eq newnewnewform newnewform)
+ (if *walk-form-expand-macros-p* newnewform newform)
+ newnewnewform)))
+ ((and (symbolp fn)
+ (not (fboundp fn))
+ (special-operator-p fn))
+ ;; This shouldn't happen, since this walker is now
+ ;; maintained as part of SBCL, so it should know
+ ;; about all the special forms that SBCL knows
+ ;; about.
+ (bug "unexpected special form ~S" fn))
+ (t
+ ;; Otherwise, walk the form as if it's just a
+ ;; standard function call using a template for
+ ;; standard function call.
+ (walk-template
+ newnewform '(call repeat (eval)) context env))))))))))))
- (repeat
- (walk-template-handle-repeat form
- (cdr template)
- ;; For the case where nothing
- ;; happens after the repeat
- ;; optimize away the call to
- ;; LENGTH.
- (if (null (cddr template))
- ()
- (nthcdr (- (length form)
- (length
- (cddr template)))
- form))
- context
- env))
- (if
- (walk-template form
- (if (if (listp (cadr template))
- (eval (cadr template))
- (funcall (cadr template) form))
- (caddr template)
- (cadddr template))
- context
- env))
- (remote
- (walk-template form (cadr template) context env))
- (otherwise
- (cond ((atom form) form)
- (t (recons form
- (walk-template
- (car form) (car template) context env)
- (walk-template
- (cdr form) (cdr template) context env))))))))
+ (repeat
+ (walk-template-handle-repeat form
+ (cdr template)
+ ;; For the case where nothing
+ ;; happens after the repeat
+ ;; optimize away the call to
+ ;; LENGTH.
+ (if (null (cddr template))
+ ()
+ (nthcdr (- (length form)
+ (length
+ (cddr template)))
+ form))
+ context
+ env))
+ (if
+ (walk-template form
+ (if (if (listp (cadr template))
+ (eval (cadr template))
+ (funcall (cadr template) form))
+ (caddr template)
+ (cadddr template))
+ context
+ env))
+ (remote
+ (walk-template form (cadr template) context env))
+ (otherwise
+ (cond ((atom form) form)
+ (t (recons form
+ (walk-template
+ (car form) (car template) context env)
+ (walk-template
+ (cdr form) (cdr template) context env))))))))
- ((eq form stop-form)
- (if (null repeat-template)
- (walk-template stop-form (cdr template) context env)
- (error "while handling code walker REPEAT:
- ~%ran into STOP while still in REPEAT template")))
- ((null repeat-template)
- (walk-template-handle-repeat-1
- form template (car template) stop-form context env))
- (t
- (recons form
- (walk-template (car form) (car repeat-template) context env)
- (walk-template-handle-repeat-1 (cdr form)
- template
- (cdr repeat-template)
- stop-form
- context
- env)))))
+ ((eq form stop-form)
+ (if (null repeat-template)
+ (walk-template stop-form (cdr template) context env)
+ (error "while handling code walker REPEAT:
+ ~%ran into STOP while still in REPEAT template")))
+ ((null repeat-template)
+ (walk-template-handle-repeat-1
+ form template (car template) stop-form context env))
+ (t
+ (recons form
+ (walk-template (car form) (car repeat-template) context env)
+ (walk-template-handle-repeat-1 (cdr form)
+ template
+ (cdr repeat-template)
+ stop-form
+ context
+ env)))))
- &optional doc-string-p declarations old-body
- &aux (form (car body)) macrop new-form)
- (cond ((and (stringp form) ;might be a doc string
- (cdr body) ;isn't the returned value
- (null doc-string-p) ;no doc string yet
- (null declarations)) ;no declarations yet
- (recons body
- form
- (walk-declarations (cdr body) fn env t)))
- ((and (listp form) (eq (car form) 'declare))
- ;; We got ourselves a real live declaration. Record it, look
- ;; for more.
- (dolist (declaration (cdr form))
- (let ((type (car declaration))
- (name (cadr declaration))
- (args (cddr declaration)))
- (if (member type *var-declarations*)
- (note-declaration `(,type
- ,(or (var-lexical-p name env) name)
- ,.args)
- env)
- (note-declaration declaration env))
- (push declaration declarations)))
- (recons body
- form
- (walk-declarations
- (cdr body) fn env doc-string-p declarations)))
- ((and form
- (listp form)
- (null (get-walker-template (car form) form))
- (progn
- (multiple-value-setq (new-form macrop)
- (sb-xc:macroexpand-1 form env))
- macrop))
- ;; This form was a call to a macro. Maybe it expanded
- ;; into a declare? Recurse to find out.
- (walk-declarations (recons body new-form (cdr body))
- fn env doc-string-p declarations
- (or old-body body)))
- (t
- ;; Now that we have walked and recorded the declarations,
- ;; call the function our caller provided to expand the body.
- ;; We call that function rather than passing the real-body
- ;; back, because we are RECONSING up the new body.
- (funcall fn (or old-body body) env))))
+ &optional doc-string-p declarations old-body
+ &aux (form (car body)) macrop new-form)
+ (cond ((and (stringp form) ;might be a doc string
+ (cdr body) ;isn't the returned value
+ (null doc-string-p) ;no doc string yet
+ (null declarations)) ;no declarations yet
+ (recons body
+ form
+ (walk-declarations (cdr body) fn env t)))
+ ((and (listp form) (eq (car form) 'declare))
+ ;; We got ourselves a real live declaration. Record it, look
+ ;; for more.
+ (dolist (declaration (cdr form))
+ (let ((type (car declaration))
+ (name (cadr declaration))
+ (args (cddr declaration)))
+ (if (member type *var-declarations*)
+ (note-declaration `(,type
+ ,(or (var-lexical-p name env) name)
+ ,.args)
+ env)
+ (note-declaration declaration env))
+ (push declaration declarations)))
+ (recons body
+ form
+ (walk-declarations
+ (cdr body) fn env doc-string-p declarations)))
+ ((and form
+ (listp form)
+ (null (get-walker-template (car form) form))
+ (progn
+ (multiple-value-setq (new-form macrop)
+ (sb-xc:macroexpand-1 form env))
+ macrop))
+ ;; This form was a call to a macro. Maybe it expanded
+ ;; into a declare? Recurse to find out.
+ (walk-declarations (recons body new-form (cdr body))
+ fn env doc-string-p declarations
+ (or old-body body)))
+ (t
+ ;; Now that we have walked and recorded the declarations,
+ ;; call the function our caller provided to expand the body.
+ ;; We call that function rather than passing the real-body
+ ;; back, because we are RECONSING up the new body.
+ (funcall fn (or old-body body) env))))
- ((symbolp (setq arg (car arglist)))
- (or (member arg lambda-list-keywords)
- (note-lexical-binding arg env))
- (recons arglist
- arg
- (walk-arglist (cdr arglist)
- context
- env
- (and destructuringp
- (not (member arg
- lambda-list-keywords))))))
- ((consp arg)
- (prog1 (recons arglist
- (if destructuringp
- (walk-arglist arg context env destructuringp)
- (relist* arg
- (car arg)
- (walk-form-internal (cadr arg) :eval env)
- (cddr arg)))
- (walk-arglist (cdr arglist) context env nil))
- (if (symbolp (car arg))
- (note-lexical-binding (car arg) env)
- (note-lexical-binding (cadar arg) env))
- (or (null (cddr arg))
- (not (symbolp (caddr arg)))
- (note-lexical-binding (caddr arg) env))))
- (t
- (error "can't understand something in the arglist ~S" arglist))))
+ ((symbolp (setq arg (car arglist)))
+ (or (member arg lambda-list-keywords)
+ (note-lexical-binding arg env))
+ (recons arglist
+ arg
+ (walk-arglist (cdr arglist)
+ context
+ env
+ (and destructuringp
+ (not (member arg
+ lambda-list-keywords))))))
+ ((consp arg)
+ (prog1 (recons arglist
+ (if destructuringp
+ (walk-arglist arg context env destructuringp)
+ (relist* arg
+ (car arg)
+ (walk-form-internal (cadr arg) :eval env)
+ (cddr arg)))
+ (walk-arglist (cdr arglist) context env nil))
+ (if (symbolp (car arg))
+ (note-lexical-binding (car arg) env)
+ (note-lexical-binding (cadar arg) env))
+ (or (null (cddr arg))
+ (not (symbolp (caddr arg)))
+ (note-lexical-binding (caddr arg) env))))
+ (t
+ (error "can't understand something in the arglist ~S" arglist))))
- (variable-symbol-macro-p var env))
- vars)
- (let* ((temps (mapcar (lambda (var)
- (declare (ignore var))
- (gensym))
- vars))
- (sets (mapcar (lambda (var temp) `(setq ,var ,temp))
- vars
- temps))
- (expanded `(multiple-value-bind ,temps ,(caddr form)
- ,@sets))
- (walked (walk-form-internal expanded context env)))
- (if (eq walked expanded)
- form
- walked))
- (walk-template form '(nil (repeat (set)) eval) context env))))
+ (variable-symbol-macro-p var env))
+ vars)
+ (let* ((temps (mapcar (lambda (var)
+ (declare (ignore var))
+ (gensym))
+ vars))
+ (sets (mapcar (lambda (var temp) `(setq ,var ,temp))
+ vars
+ temps))
+ (expanded `(multiple-value-bind ,temps ,(caddr form)
+ ,@sets))
+ (walked (walk-form-internal expanded context env)))
+ (if (eq walked expanded)
+ form
+ walked))
+ (walk-template form '(nil (repeat (set)) eval) context env))))
- (recons bindings
- (if (symbolp binding)
- (prog1 binding
- (note-lexical-binding binding new-env))
- (prog1 (relist* binding
- (car binding)
- (walk-form-internal (cadr binding)
- context
- (if sequentialp
- new-env
- old-env))
- ;; Save cddr for DO/DO*; it is
- ;; the next value form. Don't
- ;; walk it now, though.
- (cddr binding))
- (note-lexical-binding (car binding) new-env)))
- (walk-bindings-1 (cdr bindings)
- old-env
- new-env
- context
- sequentialp)))))
+ (recons bindings
+ (if (symbolp binding)
+ (prog1 binding
+ (note-lexical-binding binding new-env))
+ (prog1 (relist* binding
+ (car binding)
+ (walk-form-internal (cadr binding)
+ context
+ (if sequentialp
+ new-env
+ old-env))
+ ;; Save cddr for DO/DO*; it is
+ ;; the next value form. Don't
+ ;; walk it now, though.
+ (cddr binding))
+ (note-lexical-binding (car binding) new-env)))
+ (walk-bindings-1 (cdr bindings)
+ old-env
+ new-env
+ context
+ sequentialp)))))
- (tail (cdr form)))
- (loop (when (null tail) (return (nreverse rforms)))
- (let ((var (pop tail)) (val (pop tail)))
- (push `(setq ,var ,val) rforms)))))
- (walked (walk-repeat-eval expanded env)))
- (if (eq expanded walked)
- form
- `(progn ,@walked)))
+ (tail (cdr form)))
+ (loop (when (null tail) (return (nreverse rforms)))
+ (let ((var (pop tail)) (val (pop tail)))
+ (push `(setq ,var ,val) rforms)))))
+ (walked (walk-repeat-eval expanded env)))
+ (if (eq expanded walked)
+ form
+ `(progn ,@walked)))
- (val (caddr form))
- (symmac (car (variable-symbol-macro-p var env))))
- (if symmac
- (let* ((expanded `(setf ,(cddr symmac) ,val))
- (walked (walk-form-internal expanded context env)))
- (if (eq expanded walked)
- form
- walked))
- (relist form 'setq
- (walk-form-internal var :set env)
- (walk-form-internal val :eval env))))))
+ (val (caddr form))
+ (symmac (car (variable-symbol-macro-p var env))))
+ (if symmac
+ (let* ((expanded `(setf ,(cddr symmac) ,val))
+ (walked (walk-form-internal expanded context env)))
+ (if (eq expanded walked)
+ form
+ walked))
+ (relist form 'setq
+ (walk-form-internal var :set env)
+ (walk-form-internal val :eval env))))))
- (walker-environment-bind (macro-env
- nil
- :walk-function (env-walk-function old-env))
- (labels ((walk-definitions (definitions)
- (and definitions
- (let ((definition (car definitions)))
- (recons definitions
- (relist* definition
- (car definition)
- (walk-arglist (cadr definition)
- context
- macro-env
- t)
- (walk-declarations (cddr definition)
- #'walk-repeat-eval
- macro-env))
- (walk-definitions (cdr definitions)))))))
- (with-new-definition-in-environment (new-env old-env form)
- (relist* form
- (car form)
- (walk-definitions (cadr form))
- (walk-declarations (cddr form)
- #'walk-repeat-eval
- new-env))))))
+ (walker-environment-bind (old-env old-env)
+ (walker-environment-bind (macro-env
+ nil
+ :walk-function (env-walk-function old-env))
+ (labels ((walk-definitions (definitions)
+ (and definitions
+ (let ((definition (car definitions)))
+ (recons definitions
+ (relist* definition
+ (car definition)
+ (walk-arglist (cadr definition)
+ context
+ macro-env
+ t)
+ (walk-declarations (cddr definition)
+ #'walk-repeat-eval
+ macro-env))
+ (walk-definitions (cdr definitions)))))))
+ (with-new-definition-in-environment (new-env old-env form)
+ (relist* form
+ (car form)
+ (walk-definitions (cadr form))
+ (walk-declarations (cddr form)
+ #'walk-repeat-eval
+ new-env)))))))
- (car form)
- (recons (cdr form)
- (walk-definitions (cadr form))
- (walk-declarations (cddr form)
- #'walk-repeat-eval
- new-env))))))
+ (car form)
+ (recons (cdr form)
+ (walk-definitions (cadr form))
+ (with-new-definition-in-environment (new-env old-env form)
+ (walk-declarations (cddr form)
+ #'walk-repeat-eval
+ new-env)))))))
+
+(defun walk-labels (form context old-env)
+ (walker-environment-bind (old-env old-env)
+ (with-new-definition-in-environment (new-env old-env form)
+ (labels ((walk-definitions (definitions)
+ (if (null definitions)
+ ()
+ (recons definitions
+ (walk-lambda (car definitions) context new-env)
+ (walk-definitions (cdr definitions))))))
+ (recons form
+ (car form)
+ (recons (cdr form)
+ (walk-definitions (cadr form))
+ (walk-declarations (cddr form)
+ #'walk-repeat-eval
+ new-env)))))))