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

View File

@@ -394,6 +394,21 @@ compiles."
(fn f ((c (closure ((int)) int))) int (return (g c)))"
"g(c)")))
;; The walker offered WALK-FN the cdr of every form as well as the
;; form, so the tail (t) of (fn g () t) read as a call to a macro
;; named t, and any function returning a type that shared a name
;; with a macro died on that macro's arity.
(test-group "a form's tail is not a form"
(test-assert "a return type may share a name with a macro"
(emits? "(defmacro (t x) 1)(typedef t int)(fn g () t (return 1))"
"t g (void)"))
(test-assert "so may a parameter's type"
(emits? "(defmacro (u x) 1)(typedef u int)(fn g ((a u)) void (return))"
"void g (u a)"))
(test-assert "and the macro still expands where it is called"
(emits? "(defmacro (t x) x)(fn g () int (return (t 7)))"
"return 7")))
;; a macro body reads types as lists, so the srfi-1 accessors are in
;; scope beside the type database
(test-group "macro list accessors"