From 4bdc0e57a72c5ca5ae65f4204a538dfb15333c63 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 13:38:30 +0300 Subject: [PATCH] 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. --- fmt-c-writer.scm | 4 ++++ semen.scm | 18 +++++++++++++++--- tests/codegen.scm | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index dae2ca4d..fc044671 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -166,6 +166,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 diff --git a/semen.scm b/semen.scm index aa7eed5d..b7ff1e31 100644 --- a/semen.scm +++ b/semen.scm @@ -1320,7 +1320,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) @@ -1378,10 +1378,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)) @@ -2169,7 +2172,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. diff --git a/tests/codegen.scm b/tests/codegen.scm index 8704a4ac..9a0ff8a2 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -290,7 +290,14 @@ (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 @@ -766,6 +773,15 @@ "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)))")