cwebber pushed a commit to branch compile-to-js-merge
in repository guile.
commit 41023d5b4cd322d51319ba8e9bbd1a7f3cfb7091
Author: Ian Price <[email protected]>
AuthorDate: Mon Jun 8 00:17:22 2015 +0100
Mangle js identifiers
---
module/language/js-il/compile-javascript.scm | 37 ++++++++++++++++++++++++++--
1 file changed, 35 insertions(+), 2 deletions(-)
diff --git a/module/language/js-il/compile-javascript.scm
b/module/language/js-il/compile-javascript.scm
index 3d50bcc..19e8eb7 100644
--- a/module/language/js-il/compile-javascript.scm
+++ b/module/language/js-il/compile-javascript.scm
@@ -14,8 +14,41 @@
(define (name->id name)
(make-id (rename name)))
-(define (rename name)
- (format #f "kont_~a" name))
+(define (rename id)
+ (cond ((and (integer? id) (>= id 0))
+ (format #f "k_~a " id))
+ ((symbol? id)
+ (js-id (symbol->string id)))
+ ((string? id)
+ (js-id id))
+ (else
+ (throw 'bad-id id))))
+
+(define (js-id name)
+ (call-with-output-string
+ (lambda (port)
+ (display "k_" port)
+ (string-for-each
+ (lambda (c)
+ (if (or (and (char<=? #\a c) (char<=? c #\z))
+ (and (char<=? #\A c) (char<=? c #\Z))
+ (and (char<=? #\0 c) (char<=? c #\9)))
+ (display c port)
+ (case c
+ ((#\-) (display "_h" port))
+ ((#\_) (display "_u" port))
+ ((#\?) (display "_p" port))
+ ((#\!) (display "_x" port))
+ ((#\<) (display "_l" port))
+ ((#\>) (display "_g" port))
+ ((#\=) (display "_e" port))
+ ((#\*) (display "_s" port))
+ ((#\+) (display "_a" port))
+ ((#\\) (display "_b" port))
+ ((#\/) (display "_f" port))
+ (else
+ (throw 'bad-id-char c)))))
+ name))))
(define (bind-rest-args rest num-drop)
(define (ref i l)