diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index c01c61f..2b84f11 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -12,6 +12,7 @@ sex-fmt-c matchable (chicken irregex) ; unkebabify + reader ; c-literal? srfi-1 ; lists srfi-13 ; strings types ; array-bound?, named-arg? @@ -269,6 +270,7 @@ forms, and what remains." ;; clause to be a case/default form and errors on anything else. (('switch . rest) (let-values (((comments slots clauses) (take-slots rest 1))) + (check-case-labels form clauses) (with-comments (append comments (filter comment-form? clauses)) (cons* 'switch (walk-expr (car slots)) (map walk-expr (remove comment-form? clauses)))))) @@ -326,6 +328,28 @@ forms, and what remains." (if most "" " or more"))) (car form))))))) +;;; (case (1 2) ...) is two labels, so anything but a value among them +;;; was taken for one: (case (+ 1 1) ...) came out `case +: case 1: +;;; case 1:'. A value written twice in one switch C reports too, but not +;;; where. +(define (check-case-labels form clauses) + (fold (lambda (clause seen) + (let ((parts (and (pair? clause) (memq (car clause) '(case case/fallthrough)) + (remove comment-form? (cdr clause))))) + (if (pair? parts) + (fold (lambda (value seen) + (unless (or (number? value) (char? value) (c-literal? value) + (and (symbol? value) (not (operator-arity value)))) + (sex-error form "a case label is a value" (car parts))) + (when (member value seen) + (sex-error form "case label written twice" value)) + (cons value seen)) + seen + (if (pair? (car parts)) (car parts) (list (car parts)))) + seen))) + (list) + clauses)) + ;;; #(a b c) is a brace initializer. A `:' inside one ends a type and ;;; turns the whole thing into a C99 compound literal: ;;; #(struct point : 1 2) is (struct point){1, 2}, and the type is diff --git a/tests/codegen.scm b/tests/codegen.scm index 24b816b..9beb4eb 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -35,6 +35,23 @@ (not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))") "{" "{")))) + ;; (case (1 2) ...) is two labels, so (case (+ 1 1) ...) came out as + ;; three, the first of them `case +:' + (test-group "case labels" + (test-assert "a list of them is a case each" + (followed-by? (in-fn "(switch a (case (1 B) (g a)))") "case 1:" "case B:")) + (test-assert "an operator is not one" + (reports? (in-fn "(switch a (case (+ 1 1) (g a)))") "a case label is a value")) + (test-assert "nor is an expression in the list" + (reports? (in-fn "(switch a (case (1 (- 2)) (g a)))") "a case label is a value")) + (test-assert "a value is written once" + (reports? (in-fn "(switch a (case (1 2 1) (g a)))") "case label written twice")) + (test-assert "in the whole switch" + (reports? (in-fn "(switch a (case 1 (g a)) (case/fallthrough (2 1)) (default (g b)))") + "case label written twice")) + (test-assert "each switch apart" + (emits? (in-fn "(switch a (case 1 (g a))) (switch b (case 1 (g b)))") "case 1:"))) + ;; c-switch handed its scrutinee straight to `cat', which only works ;; when it is an atom. Anything else was displayed as a raw ;; s-expression: `switch ((%. e type))'.