keep the writer's own words to the head of a form

They were its own wherever they stood, so a name spelled like one --
(var array int), a parameter `define' -- came out %array, %define.
This commit is contained in:
2026-10-19 13:38:30 +03:00
parent fb26b92fdc
commit 6ad8f8f56b
2 changed files with 41 additions and 15 deletions

View File

@@ -177,6 +177,12 @@ forms, and what remains."
(unkebabify atom)
atom))))
;;; A name as C spells it. Only at the head of a form are some words
;;; the writer's own: (array ...) is one, (var array int) declares
;;; `array', where it came out `%array'
(define (name-to-fmt-c atom)
(if (symbol? atom) (unkebabify atom) atom))
(define (maybe-unwrap-type type)
(if (and (list? type)
(= 1 (length type)))
@@ -221,7 +227,7 @@ forms, and what remains."
(match form
((? vector?) (walk-initializer (vector->list form)))
((? atom?)
(atom-to-fmt-c form))
(name-to-fmt-c form))
;; (comment "text") -> /* text */. `%comment' is the fmt-c directive.
(('comment . text) (walk-comment text))
;; (dot-access obj field ...) -> obj.field... member access. `%.'
@@ -236,7 +242,7 @@ forms, and what remains."
(('sizeof ('%type type)) `(%sizeof-type ,(walk-type type)))
;; semen has already put the target first, the way C writes it
(('typedef type name)
`(typedef ,(walk-type type) ,(atom-to-fmt-c name)))
`(typedef ,(walk-type type) ,(name-to-fmt-c name)))
;; An expression has no room for a statement, so a comment in a
;; cast is dropped rather than relocated.
(('cast . rest)
@@ -294,7 +300,10 @@ forms, and what remains."
;; C's comma where it is a value
(cons '%comma (map (lambda (operand) (walk-expr (list (car form) operand)))
(cdr form)))
(map walk-expr form))))))
(cons (if (symbol? (car form))
(atom-to-fmt-c (car form))
(walk-expr (car form)))
(map walk-expr (cdr form))))))))
(define +comparisons+ '(< > <= >= == !=))
@@ -383,7 +392,7 @@ forms, and what remains."
(((? designator? d) value . rest)
(loop rest
(cons (list '%designate
(atom-to-fmt-c (designator-field d))
(name-to-fmt-c (designator-field d))
(walk-expr value))
acc)))
;; [3] 1: an element named by its index. A subscript has two
@@ -414,7 +423,7 @@ forms, and what remains."
(let ((form (cons (car form) (remove comment-form? (cdr form)))))
`(%var
,(walk-type (third form))
,(atom-to-fmt-c (second form))
,(name-to-fmt-c (second form))
.
,(if (null? (drop form 3))
(list)
@@ -485,7 +494,7 @@ forms, and what remains."
;; (fn ...) is spelled as the pointer it already is in C
(sex-error type "a fn type is a function pointer already: write (fn ...), not (* (fn ...))" type)
(sex-error type "a pointer to an array is not supported" type))))
(if (atom? type) (atom-to-fmt-c type)
(if (atom? type) (name-to-fmt-c type)
(type-words (list-join (reverse (list-split type '*)) '(*)))))
;;; A type's words, flat, except that an aggregate written out in full is
@@ -495,7 +504,7 @@ forms, and what remains."
(cond ((inline-aggregate? item) (list (walk-type item)))
((pair? item) (type-words item))
((null? item) (list))
(else (list (atom-to-fmt-c item)))))
(else (list (name-to-fmt-c item)))))
items))
;;; (struct ((c int))), (union u (...)), (enum (red green)): a body, not
@@ -508,7 +517,7 @@ forms, and what remains."
(('fn name args ret-type . maybe-body)
`(%fun
,(walk-type ret-type)
,(atom-to-fmt-c name)
,(name-to-fmt-c name)
,(walk-arglist args)
.
,(let ((body (walk-body maybe-body)))
@@ -559,7 +568,7 @@ forms, and what remains."
(define (process-struct-fields fields)
(map (fn
(let ((type (walk-type (last x))))
(cons type (map atom-to-fmt-c (drop-right x 1)))))
(cons type (map name-to-fmt-c (drop-right x 1)))))
(remove comment-form? fields)))
(define (walk-struct form)
@@ -568,9 +577,9 @@ forms, and what remains."
`(,type ,(process-struct-fields fields)
. ,(tree-map atom-to-fmt-c attrs)))
((type name) ; simple 'struct whatever', like in variable def
`(,type ,(atom-to-fmt-c name)))
`(,type ,(name-to-fmt-c name)))
((type name (fields ...) . attrs)
`(,type ,(atom-to-fmt-c name)
`(,type ,(name-to-fmt-c name)
,(process-struct-fields fields)
. ,(tree-map atom-to-fmt-c attrs)))
(else (sex-error form "malformed aggregate definition" form))))
@@ -582,18 +591,18 @@ forms, and what remains."
;; before the anonymous case, since `(enum (red green))' is also a
;; two-element form
(('enum (? symbol? name))
`(enum ,(atom-to-fmt-c name)))
`(enum ,(name-to-fmt-c name)))
(('enum (values ...))
`(enum ,(map walk-enumerator values)))
(('enum name (values ...))
`(enum ,(atom-to-fmt-c name) ,(map walk-enumerator values)))
`(enum ,(name-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
(((? enumerator-name? name) expr) (list (atom-to-fmt-c name) (walk-expr expr)))
((? enumerator-name?) (atom-to-fmt-c value))
(((? enumerator-name? name) expr) (list (name-to-fmt-c name) (walk-expr expr)))
((? enumerator-name?) (name-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))))

View File

@@ -954,6 +954,23 @@
(emits? "(fn take ((f (fn ((int)) (fn () void)))) void (return))"
"take (void (*(*f)(int))())")))
;; the writer's own words were its own wherever they stood, so a
;; name spelled like one came out `%array'
(test-group "names the writer has words for"
(test-assert "as variables"
(emits? (in-fn "(var array int 1) (var define int 2) (var include int 3) (g array define include)")
"g(array, define, include)"))
(test-assert "as parameters"
(emits? "(fn sum ((array (* int)) (pointer int)) int (return (+ [array 0] pointer)))"
"sum (int * array, int pointer)"))
(test-assert "as fields"
(emits? "(struct s ((prototype int) (attribute int)))" "int prototype;"))
(test-assert "as a typedef's name, and used as a type"
(emits? "(typedef array int) (fn f () void (var a array 1))" " array a = 1;"))
(test-assert "while at a form's head they are the writer's still"
(emits? "(include stdio.h) (fn f () void (static-assert (== 1 1) \"x\"))"
"_Static_assert(1 == 1, \"x\")")))
;; Operands past what an operator takes were written as they came:
;; (< a b c) as C's own chain, which compares a boolean with c
(test-group "operator arity"