diff --git a/semen.scm b/semen.scm index eb108a2..d5e15c9 100644 --- a/semen.scm +++ b/semen.scm @@ -252,10 +252,10 @@ ;; between name and fields are not skipped, they already confuse the ;; writer (match form - (('pub (and kind (or 'struct 'union 'enum)) + (('pub (and kind (or 'struct 'union 'enum 'sum)) (? symbol? name) (? string? doc) . rest) (values doc (copy-form-source! form `(pub ,kind ,name ,@rest)))) - (((and kind (or 'struct 'union 'enum)) + (((and kind (or 'struct 'union 'enum 'sum)) (? symbol? name) (? string? doc) . rest) (values doc (copy-form-source! form `(,kind ,name ,@rest)))) (_ (values #f form)))) @@ -2056,29 +2056,35 @@ ;;; purpose: a record would typedef shapeːcircle into the ordinary ;;; namespace, where the constructor of that name already lives. As a ;;; tag it shares nothing. -(define (process-sum form acc) - (let* ((core (if (eq? (car form) 'pub) (cdr form) form)) - (name (and (pair? (cdr core)) (cadr core))) - (declared (and (pair? (cddr core)) (caddr core)))) - (unless (and (symbol? name) (list? declared) (pair? declared)) - (sex-error form "sum needs a name and a list of variants" form)) - (let ((variants (map (lambda (v) (parse-variant form v)) declared))) - (for-each (lambda (v) - (for-each (lambda (field) (check-variant-field form name field)) - (cdr v))) - variants) - (let* ((names (map car variants)) - (twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names))) - (when twice - (sex-error form "variant is declared twice" twice))) - (when (get-sum name) - (sex-error form "sum is already defined" name)) - (add-sum! name variants) - (for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v))) - variants) - (fold match-sex-form acc - (map (lambda (f) (copy-form-source! form f)) - (sum-forms name variants)))))) +(define (process-sum written acc) + (let-values (((doc form) (extract-aggregate-docstring written))) + (let* ((core (if (eq? (car form) 'pub) (cdr form) form)) + (name (and (pair? (cdr core)) (cadr core))) + (declared (and (pair? (cddr core)) (caddr core)))) + (unless (and (symbol? name) (list? declared) (pair? declared)) + (sex-error form "sum needs a name and a list of variants" form)) + (let ((variants (map (lambda (v) (parse-variant form v)) declared))) + (for-each (lambda (v) + (for-each (lambda (field) + (check-variant-field form name field)) + (cdr v))) + variants) + (let* ((names (map car variants)) + (twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) + names))) + (when twice + (sex-error form "variant is declared twice" twice))) + (when (get-sum name) + (sex-error form "sum is already defined" name)) + (add-sum! name variants) + (for-each (lambda (v) + (add-constructor! (qualify-op name (car v)) name (car v))) + variants) + ;; the docstring goes ahead of everything the sum is made of + (fold match-sex-form + (if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc) + (map (lambda (f) (copy-form-source! form f)) + (sum-forms name variants))))))) (define (parse-variant form v) (unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v))) diff --git a/sex-modules.scm b/sex-modules.scm index 37aaf78..f03e6c5 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -335,7 +335,8 @@ (('pub 'define name . body) body) (('pub (or 'record 'struct 'union) name . rest) (field-types (find list? (remove comment-form? rest)))) - (('pub 'sum name (variants ...)) + ((or ('pub 'sum name (variants ...)) + ('pub 'sum name (? string?) (variants ...))) (append-map (lambda (v) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (remove comment-form? variants))) diff --git a/tests/codegen.scm b/tests/codegen.scm index bc047e3..5df12cf 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -493,6 +493,13 @@ whitespace aside, THEN?" ;; A string as the first body form (or after the name of a struct, ;; union or enum) is a docstring: it becomes a comment immediately ;; before the declaration, not a statement inside it. + (test-group "a sum's docstring" + ;; record, struct and enum took one, and sum read it as its variants + (test-assert "is a comment ahead of it" + (emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "/* A shape. */")) + (test-assert "and the sum is still one" + (emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "shapeǂbox"))) + (test-group "docstrings" (test-assert "appears before the function" (emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))" diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 1219813..16f55e5 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -552,6 +552,9 @@ ahead of its pub forms." (test "a private one is solved where it is" '() (interface-of "(var n _ 42)")) + (test-assert "a pub sum's variants are looked at past its docstring" + (interface-reports? "(record cell ((v int))) (pub sum s \"doc\" ((a ((c cell))) (b ())))" + "held by value")) (test-assert "held by value it is an error" (interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))" "held by value"))