diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 9db3c92d..2281f7d0 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -449,11 +449,17 @@ forms, and what remains." (('enum (? symbol? name)) `(enum ,(atom-to-fmt-c name))) (('enum (values ...)) - `(enum ,(map atom-to-fmt-c values))) + `(enum ,(map walk-enumerator values))) (('enum name (values ...)) - `(enum ,(atom-to-fmt-c name) ,(map atom-to-fmt-c values))) + `(enum ,(atom-to-fmt-c name) ,(map walk-enumerator values))) (else (sex-error form "malformed enum" form)))) +;;; 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)))) + (define (walk-extern form) (match form (('fn . _) diff --git a/tests/codegen.scm b/tests/codegen.scm index 86a76b64..3cb08ebf 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -200,6 +200,16 @@ compiles." (emits? "(pub enum color (red green blue))" "enum color")) (test-assert "with its values" (emits? "(pub enum color (red green blue))" "red")) + ;; A value made the enumerator a pair, which went out unconverted + (test-assert "explicit values" + (emits? "(pub enum wire ((wire-varint 0) (wire-fixed64 1)))" + "wire_varint = 0, wire_fixed64 = 1")) + (test-assert "a value is an expression" + (emits? "(enum wire ((wire-last (+ WIRE-BASE 1))))" + "wire_last = WIRE_BASE + 1")) + (test-assert "and an anonymous enum takes them too" + (emits? "(fn f () void (var e (enum ((up-one 1) down)) up-one))" + "up_one = 1, down")) (test-assert "and a non-pub enum still is too" (emits? "(enum color (red green blue))" "enum color")) ;; Naming an enum as a type, rather than defining it, had no diff --git a/tests/types.scm b/tests/types.scm index 46d098a9..144e7703 100644 --- a/tests/types.scm +++ b/tests/types.scm @@ -139,6 +139,10 @@ (test "enum values are fields whose type is the enum" '((red (enum t-color)) (green (enum t-color)) (blue (enum t-color))) (map-fields 't-color (lambda (name type) (list name type)))) + (add-enum 't-wire '(enum t-wire ((t-varint 0) (t-fixed64 1) t-bytes))) + (test "an enumerator with a value is still named by its name" + '(t-varint t-fixed64 t-bytes) + (map-fields 't-wire (lambda (name type) name))) (test "a typedef has no fields to map" #f (map-fields 't-u8 (lambda (name type) name))) diff --git a/types.scm b/types.scm index 9ce5ddac..3d91404f 100644 --- a/types.scm +++ b/types.scm @@ -335,7 +335,8 @@ (else (equal? pattern type)))) ;;; Map function to each field/value of a structure/union/enum -;;; For enums, field-type is the type of the enum (since C 23) +;;; For enums, field-type is the type of the enum (since C 23), and +;;; field-name the enumerator's name, (wire-varint 0) or not ;;; (map-fields type-name ;;; (lambda (field-name field-type) ...)) ;;; @@ -351,7 +352,8 @@ ;; declared, since `enum some-typedef' is not a C type. ((enum) (let ((type (list 'enum (cadr info)))) - (map (lambda (value) (fn value type)) + (map (lambda (value) + (fn (if (pair? value) (car value) value) type)) (caddr info)))) (else #f)))))