refuse a variant named like a C keyword
One with fields is a member of the sum's union, and C takes no member named int; one without is held to the same, as one rule.
This commit is contained in:
12
semen.scm
12
semen.scm
@@ -2093,8 +2093,20 @@
|
||||
(define (parse-variant form v)
|
||||
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
|
||||
(sex-error form "a variant is (name (fields ...))" v))
|
||||
;; one with fields is a member of the sum's union, and C takes no
|
||||
;; member named `int'; one without is held to the same, as one rule
|
||||
(when (memq (car v) +c-keywords+)
|
||||
(sex-error form "a variant cannot be named like a C keyword" (car v)))
|
||||
(cons (car v) (normalize-fields (cadr v))))
|
||||
|
||||
;;; C11's, and the ones <stdbool.h>, in every unit, makes macros of
|
||||
(define +c-keywords+
|
||||
'(auto break case char const continue default do double else enum extern
|
||||
float for goto if inline int long register restrict return short signed
|
||||
sizeof static struct switch typedef union unsigned void volatile while
|
||||
_Alignas _Alignof _Atomic _Bool _Complex _Generic _Imaginary _Noreturn
|
||||
_Static_assert _Thread_local bool true false))
|
||||
|
||||
;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does.
|
||||
;;; An array field is spelled out element by element wherever it is
|
||||
;;; copied, so its length has to be one sexc can count.
|
||||
|
||||
@@ -506,6 +506,14 @@ ahead of its pub forms."
|
||||
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
||||
"t.tag == shapeǂcircle"))
|
||||
;; a pointer to one was refused, where `.' reads through one
|
||||
;; a variant with fields is a member of the sum's union, which C
|
||||
;; would not take named `int'
|
||||
(test-assert "a variant named like a C keyword is refused"
|
||||
(reports? "(sum tok ((int ((v int))) (other ())))" "a variant cannot be named like a C keyword"))
|
||||
(test-assert "one without fields too, to keep one rule"
|
||||
(reports? "(sum tok ((default ()) (other ())))" "a variant cannot be named like a C keyword"))
|
||||
(test-assert "and stdbool's names count"
|
||||
(reports? "(sum b ((true ()) (false ())))" "a variant cannot be named like a C keyword"))
|
||||
(test-assert "a pointer to a sum is matched as the sum"
|
||||
(emits? (sum-src "(var p (* shape) (& s))"
|
||||
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||
|
||||
Reference in New Issue
Block a user