walk the cdr of a form as a list, not as a form

The walker offered walk-fn every cdr as well as every form, so the
tail (t) of (fn g () t) read as a call to a macro named t: any
function whose return type or parameter type shared a name with a
macro expanded that tail and died on the macro's arity.
This commit is contained in:
2026-10-03 23:33:32 +03:00
parent bbdedb9777
commit 4a649e5bf4
2 changed files with 33 additions and 10 deletions

View File

@@ -29,6 +29,7 @@
;;; multiple forms, e.g. lambdas collected from a function may result
;;; in auxiliary structures and functions.
(define (process raw-sex-forms)
(reset-declarations!)
(process-rec raw-sex-forms (list)))
(define (process-rec forms acc)
@@ -160,16 +161,23 @@
(define (walk-form form walk-fn env)
(if (atom? form) form
(let ((new-form (walk-fn form env)))
(cond ((not (eq? form new-form))
(walk-form new-form walk-fn env))
(else
(let ((new-car (walk-form (car new-form) walk-fn env))
(new-cdr (walk-form (cdr new-form) walk-fn env)))
(cond ((and (pair? new-car)
(eq? (car new-car) walk-embed-result))
(append (cdr new-car) new-cdr))
(else
(recons new-form new-car new-cdr)))))))))
(if (not (eq? form new-form))
(walk-form new-form walk-fn env)
(walk-rest new-form walk-fn env)))))
;;; The cdr of a form is a list of forms and not itself one, so it is
;;; walked without being offered to WALK-FN: otherwise the tail of
;;; (fn g () t) is the list (t), which reads as a call to a macro
;;; named t -- and a function whose return type shares a name with a
;;; macro expanded that tail and died on the arity.
(define (walk-rest form walk-fn env)
(if (atom? form) form
(let ((new-car (walk-form (car form) walk-fn env))
(new-cdr (walk-rest (cdr form) walk-fn env)))
(if (and (pair? new-car)
(eq? (car new-car) walk-embed-result))
(append (cdr new-car) new-cdr)
(recons form new-car new-cdr)))))
;;; Typdef