refuse a malformed enumerator

Anything in the list but a name or (name value) was taken apart as if
it were the pair: (! 1 2) came out ! = 1.
This commit is contained in:
2026-10-17 17:35:48 +03:00
parent 62ca848de8
commit 87676b24fb
2 changed files with 16 additions and 2 deletions

View File

@@ -583,8 +583,14 @@ forms, and what remains."
;;; red -> red, (wire-varint 0) -> wire_varint = 0
(define (walk-enumerator value)
(match value
((name expr) (list (atom-to-fmt-c name) (walk-expr expr)))
(_ (atom-to-fmt-c value))))
(((? enumerator-name? name) expr) (list (atom-to-fmt-c name) (walk-expr expr)))
((? enumerator-name?) (atom-to-fmt-c value))
;; anything else was taken apart as if it were the pair: (! 1 2)
;; came out `! = 1'
(_ (sex-error value "an enumerator is a name, or (name value)" value))))
(define (enumerator-name? x)
(and (symbol? x) (not (operator-arity x))))
(define (walk-extern form)
(match form

View File

@@ -316,6 +316,14 @@ whitespace aside, THEN?"
"enum color m = red"))
(test-assert "a malformed enum is rejected with its location"
(reports? "(enum)" "codegen.sex:1:"))
;; anything else in the list was the name its head was and the
;; value its second element: (! 1 2) came out `! = 1'
(test-assert "an enumerator is a name, or a name and a value"
(reports? "(enum color (red (! 1 2)))" "an enumerator is a name, or (name value)"))
(test-assert "and an operator is no name"
(reports? "(enum color (red (! 1)))" "an enumerator is a name, or (name value)"))
(test-assert "nor a value with no name"
(reports? "(enum color (red 7))" "an enumerator is a name, or (name value)"))
;; c-type handed the declarator's name to c-enum as the enum tag,
;; so this emitted `enum m { up, down }' with no variable at all
(test-assert "an anonymous enum keeps the variable"