1
0
forked from alex-eg/sex

reject a for whose header is grouped

C's parens carried over, ((var i int 0) (< i 4) (++ i)), left the
writer short of slots, and it died in caddr with no location; with a
long enough body it would have compiled the body into the header.

Fixes #42.
This commit is contained in:
2026-10-09 17:26:03 +03:00
parent 9057cbfcc6
commit b8098f1508
2 changed files with 26 additions and 0 deletions

View File

@@ -94,6 +94,15 @@ forms, and what remains."
form
`(%begin ,@(map walk-expr comments) ,form)))
;;; (for init test step body ...). C's parens around the header, carried
;;; over, leave a call of a `var' in init's slot and the body in test's
(define (check-for-header form slots)
(cond ((and (pair? slots) (pair? (car slots)) (pair? (caar slots))
(eq? 'var (car (caar slots))))
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
((< (length slots) 3)
(sex-error form "malformed for: write (for init test step body ...)"))))
;;; A case body that declares is a block, so two cases can each declare
;;; a `bits', and a declaration can follow the label before C23
(define (walk-case-body body)
@@ -245,6 +254,7 @@ forms, and what remains."
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
(('for . rest)
(let-values (((comments slots body) (take-slots rest 3)))
(check-for-header form slots)
(with-comments comments
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
(walk-expr (caddr slots))