spell ? and ! in names as letters C takes

A Lisp predicate came out as ident_start?, which no C compiler reads.
ʔ and ꜝ are letters that look the part; ǃ looks closer, but clang flags
it as a homoglyph of `!' at every use.

Fixes #49.
This commit is contained in:
2026-10-09 16:01:32 +03:00
parent b5de28342b
commit 5a887c0654
3 changed files with 30 additions and 6 deletions

View File

@@ -102,15 +102,22 @@ forms, and what remains."
;;; hand-written vec2__length would collide, and an identifier holding
;;; `__' is reserved in C++, which a pub header may be included from.
;;; Lifted closures carry a ƛ for the same reason.
;;;
;;; zero? -> zeroʔ, clear! -> clearꜝ: a glottal stop and a raised
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
;;; looks more like `!' -- enough that clang warns at every use.
(define +name-spellings+
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ")))
(define (unkebabify sym)
(case sym
((- -- -> -= / /=) sym) ; bare operators, not names
((- -- -> -= / /= ! != ?) sym) ; bare operators, not names
(else
(string->symbol
(irregex-replace/all
"/"
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")
"ː")))))
(fold (lambda (spelling name)
(irregex-replace/all (car spelling) name (cdr spelling)))
(symbol->string sym)
+name-spellings+)))))
(define (atom-to-fmt-c atom)
(case atom

View File

@@ -13,6 +13,13 @@
(test '_this_->_member_ (unkebabify '-this-->-member-))
(test '__->>> (unkebabify '--->>>))
;; A Lisp predicate and mutator, in letters C takes. The operators
;; spelled with the same characters are not names
(test 'ident_startʔ (unkebabify 'ident-start?))
(test 'resetꜝ (unkebabify 'reset!))
(test '! (unkebabify '!))
(test '!= (unkebabify '!=))
;; Non-ASCII identifiers must survive intact. The `regex' egg's
;; string-substitute drops one trailing character per multi-byte
;; character, which renames things silently -- the C still compiles,

View File

@@ -1,5 +1,5 @@
(input)
(output "café 日本語 🍺" "café1" "naïve: 3")
(output "café 日本語 🍺" "café1" "naïve: 3" "predicate: 1 0")
(return 0)
;;; Non-ASCII string literals and identifiers.
@@ -13,12 +13,22 @@
;;; non-ASCII characters must survive unkebabify intact -- getting this
;;; wrong truncates the name silently, and the program still compiles
;;; and runs, just under a different name than the one written.
;;;
;;; `?' and `!' end Lisp predicates and mutators, and C takes neither:
;;; they become the letters ʔ and ꜝ.
(include stdio.h)
(fn zero? ((n int)) bool (return (== n 0)))
(fn clear! ((n (* int))) void (= (* n) 0))
(pub fn main () int
(puts "café 日本語 🍺")
(puts "café1")
(var naïve-count int 3)
(printf "naïve: %d\n" naïve-count)
(var n int 5)
(var before bool (zero? n))
(clear! (& n))
(printf "predicate: %d %d\n" (zero? n) before)
(return 0))