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:
@@ -181,6 +181,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)))
|
||||
@@ -222,15 +228,18 @@ forms, and what remains."
|
||||
(else (loop (cdr fs) (cons (car fs) acc))))))
|
||||
|
||||
(define (walk-generic-toplevel form)
|
||||
(cond ((atom? form) (atom-to-fmt-c form))
|
||||
((list? form) (map walk-generic-toplevel form))
|
||||
(cond ((atom? form) (name-to-fmt-c form))
|
||||
((list? form) (cons (if (symbol? (car form))
|
||||
(atom-to-fmt-c (car form))
|
||||
(walk-generic-toplevel (car form)))
|
||||
(map walk-generic-toplevel (cdr form))))
|
||||
(else (sex-error form "malformed form" form))))
|
||||
|
||||
(define (walk-expr form)
|
||||
(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. `%.'
|
||||
@@ -245,7 +254,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)
|
||||
@@ -303,7 +312,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+ '(< > <= >= == !=))
|
||||
|
||||
@@ -392,7 +404,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
|
||||
@@ -423,7 +435,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)
|
||||
@@ -494,7 +506,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
|
||||
@@ -504,7 +516,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
|
||||
@@ -517,7 +529,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)))
|
||||
@@ -568,7 +580,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)
|
||||
@@ -577,9 +589,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))))
|
||||
@@ -591,18 +603,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))))
|
||||
|
||||
@@ -1011,6 +1011,23 @@ whitespace aside, THEN?"
|
||||
(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\")")))
|
||||
|
||||
;; c-struct/aux wrote the tag and the first `*' after it, and
|
||||
;; dropped the rest of the chain
|
||||
(test-group "a pointer chain to a struct"
|
||||
|
||||
Reference in New Issue
Block a user