type #t, an enum's constants and the bitwise names

None had a rule. A constant is typed as its enum, where C says int, so
that the enum's ops are found through it. And bit-and, bit-not and the
rest were respelled bit_and before fmt-c could see them, and called.
This commit is contained in:
2026-10-19 13:38:30 +03:00
parent 91246940e5
commit 2d7f58e15e
3 changed files with 36 additions and 4 deletions

View File

@@ -170,6 +170,10 @@ forms, and what remains."
((¤) 'vector-ref)
((include) '%include)
((static-assert) '_Static_assert)
;; fmt-c's own names for these: respelled bit_and they were calls
((bit-and bit-or bit-xor bit-not bit-or=
bitwise-and bitwise-ior bitwise-xor bitwise-not bitwise-ior=)
atom)
;; a `|' inside a symbol has to be escaped to be written in
;; a Scheme source, so we just rename it in fmt-c compatible
;; way

View File

@@ -1328,7 +1328,7 @@
((number? expr) 'double)
((string? expr) '(* const char))
((char? expr) 'char)
((memq expr '(true false)) 'bool)
((or (boolean? expr) (memq expr '(true false))) 'bool)
;; `#(T : ...)' carries its own type; a bare `#(...)' has none and
;; takes one from whatever it is being written into
((vector? expr)
@@ -1386,10 +1386,13 @@
(arithmetic-type expr env)))
((+ / %) (arithmetic-type expr env))
;; the bitwise operators join like the arithmetic ones
((^ |\||) (arithmetic-type expr env))
((^ |\|| bit-and bit-or bit-xor bitwise-and bitwise-ior bitwise-xor)
(arithmetic-type expr env))
;; a shift is the promoted left operand, not a join:
;; (<< l b), l : long -> long; (>> c b), c : char -> int
((<< >>) (promoted-type (expression-type (second expr) env)))
;; a complement is the operand promoted, as a negation is
((~ bit-not bitwise-not) (promoted-type (expression-type (second expr) env)))
;; ...and an increment is the operand unpromoted:
;; (++ c), c : char -> char
((++ --) (expression-type (second expr) env))
@@ -2177,7 +2180,16 @@
(case (car f)
((struct) (add-struct name form))
((union) (add-union name form))
((enum) (add-enum name form))))))
((enum)
(add-enum name form)
;; a constant is its enum's, which `_' reads and whose ops it
;; calls -- where C says int, and converts between them
(when (and (pair? (cddr f)) (list? (caddr f)))
(for-each (lambda (value)
(let ((constant (if (pair? value) (car value) value)))
(when (symbol? constant)
(add-name-type! constant `(enum ,name)))))
(remove comment-form? (caddr f)))))))))
;;; No function around a toplevel form, so no scope chain: what a
;;; global's initializer names comes from `get-name-type' alone.

View File

@@ -360,7 +360,14 @@ whitespace aside, THEN?"
(test-assert "c-or is still accepted"
(emits? (in-fn "(var x int (c-or a b))") "a || b"))
(test-assert "c-bit-or is still accepted"
(emits? (in-fn "(var x int (c-bit-or a b))") "a | b")))
(emits? (in-fn "(var x int (c-bit-or a b))") "a | b"))
;; the names fmt-c has cases for were respelled bit_and before it
;; saw them, and called as functions
(test-assert "and so are the named ones"
(emits? (in-fn "(var x _ (bit-and a b)) (var y _ (bit-or a b)) (var z _ (bit-xor a b))")
"int z = a ^ b"))
(test-assert "bit-not among them"
(emits? (in-fn "(var x _ (bit-not a))") "int x = ~a")))
;; A `;' comment is a form. In a macro body `comment' is a no-op
;; that swallows the comment itself. Inside a quasiquoted payload
@@ -823,6 +830,15 @@ whitespace aside, THEN?"
"a function's types are written out"))
;; a call was typed by asking the global table what a function of
;; that name returns, which a local pointer is not
;; none of these had a rule
(test-assert "#t is a bool, as true is"
(emits? (in-fn "(var t _ #t)") "bool t = 1"))
(test-assert "an enum's constant is the enum, whose ops it can call"
(emits? "(enum color (red green)) (fn f () void (var c _ green))" "enum color c = green"))
(test-assert "a bit-not is its operand, promoted"
(emits? (in-fn "(var u (unsigned int) 1) (var v _ (~ u))") "unsigned int v = ~u"))
(test-assert "a char's to an int"
(emits? (in-fn "(var c char 1) (var w _ (bit-not c))") "int w = ~c"))
(test-assert "a call through a local function pointer"
(emits? (string-append "(fn area ((w int) (h int)) int (return (* w h)))"
"(fn f () void (var p (fn ((int) (int)) int) area) (var r _ (p 3 4)))")