diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 158f30a..a337250 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -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 diff --git a/tests/basic.scm b/tests/basic.scm index 3456f0b..f03157d 100644 --- a/tests/basic.scm +++ b/tests/basic.scm @@ -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, diff --git a/tests/sex-programs/unicode.sex b/tests/sex-programs/unicode.sex index 93fa7d3..a89543c 100644 --- a/tests/sex-programs/unicode.sex +++ b/tests/sex-programs/unicode.sex @@ -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))