forked from alex-eg/sex
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:
@@ -574,8 +574,14 @@ forms, and what remains."
|
|||||||
;;; red -> red, (wire-varint 0) -> wire_varint = 0
|
;;; red -> red, (wire-varint 0) -> wire_varint = 0
|
||||||
(define (walk-enumerator value)
|
(define (walk-enumerator value)
|
||||||
(match value
|
(match value
|
||||||
((name expr) (list (atom-to-fmt-c name) (walk-expr expr)))
|
(((? enumerator-name? name) expr) (list (atom-to-fmt-c name) (walk-expr expr)))
|
||||||
(_ (atom-to-fmt-c value))))
|
((? 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)
|
(define (walk-extern form)
|
||||||
(match form
|
(match form
|
||||||
|
|||||||
@@ -246,6 +246,14 @@
|
|||||||
"enum color m = red"))
|
"enum color m = red"))
|
||||||
(test-assert "a malformed enum is rejected with its location"
|
(test-assert "a malformed enum is rejected with its location"
|
||||||
(reports? "(enum)" "codegen.sex:1:"))
|
(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,
|
;; 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
|
;; so this emitted `enum m { up, down }' with no variable at all
|
||||||
(test-assert "an anonymous enum keeps the variable"
|
(test-assert "an anonymous enum keeps the variable"
|
||||||
|
|||||||
Reference in New Issue
Block a user