diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 53ece54..0ddd64d 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -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) @@ -246,6 +255,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)) diff --git a/tests/codegen.scm b/tests/codegen.scm index db3ba62..b4dc339 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -309,6 +309,22 @@ whitespace aside, THEN?" ;; A nested pointer chain used to silently lose a level: ;; (var p (* (* char))) emitted `char *p' + ;; A grouped header, C's parens carried over, died in the writer with + ;; `(caddr) bad argument type' and no location + (test-group "malformed for" + (test-assert "a grouped header is named" + (reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))") + "header is not grouped")) + (test-assert "even when the body fills the slots it left" + (reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))") + "header is not grouped")) + (test-assert "a short one gets the shape" + (reports? (in-fn "(for (var i int 0) (< i 4))") + "(for init test step body ...)")) + (test-assert "and both name the line" + (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" + "codegen.sex:2:"))) + (test-group "malformed types are rejected" (test-assert "nested pointer chain" (reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))