-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmangle.scm
More file actions
46 lines (42 loc) · 1.06 KB
/
mangle.scm
File metadata and controls
46 lines (42 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
;;;; simple mangling to C identifiers
;
; - taken from grass-libs, but needs to notice when non-ASCII characters appear in literals.
(define (mangle-identifier x)
(apply
string-append
"___"
(map (lambda (c)
(if (or (char-lower-case? c)
(char-numeric? c))
(string c)
(let* ((n (char->integer c))
(s (number->string n 16)))
(string-append
"_"
(if (< n 128)
(padl s 2 #\0)
(string-append "U" (padl s 5 #\0)))))))
(string->list (stringify x)))))
(define (mangle-string-constant str)
(string-append
"\""
(with-output-to-string
(lambda ()
(for-each
(lambda (c)
(let ((n (char->integer c)))
(cond ((< n 128)
(emit "\\" (padl (number->string (char->integer c) 8) 3 #\0)))
(else
(emit "\\x" (number->string (char->integer c) 16) "\"\"")))))
(string->list str))))
"\""))
(define (mangle-feature-name name)
(string-append
"FEATURE_"
(list->string
(map (lambda (c)
(case c
((#\-) #\_)
(else (char-upcase c))))
(string->list (symbol->string name))))))