Format strings
authorDavid Vazquez <davazp@gmail.com>
Thu, 20 Dec 2012 18:51:22 +0000 (18:51 +0000)
committerDavid Vazquez <davazp@gmail.com>
Thu, 20 Dec 2012 18:51:22 +0000 (18:51 +0000)
lispstrack.lisp

index 1d86fb4..dfbfcad 100644 (file)
 
 ;;; Literals
 
+(defun escape-string (string)
+  (let ((output "")
+        (index 0)
+        (size (length string)))
+    (while (< index size)
+      (let ((ch (char string index)))
+        (when (or (char= ch #\") (char= ch #\\))
+          (setq output (concat output "\\")))
+        (setq output (concat output (string ch))))
+      (incf index))
+    output))
+
 (defun literal->js (sexp)
   (cond
     ((null sexp) "false")
     ((integerp sexp) (integer-to-string sexp))
-    ((stringp sexp) (concat "\"" sexp "\""))
-    ((symbolp sexp) (concat "{name: \"" (symbol-name sexp) "\"}"))
+    ((stringp sexp) (concat "\"" (escape-string sexp) "\""))
+    ((symbolp sexp) (concat "{name: \"" (escape-string (symbol-name sexp)) "\"}"))
     ((consp sexp) (concat "{car: "
                           (literal->js (car sexp))
                           ", cdr: "
                 ", ")
           ")"))
 
-(define-transformation apply (func &rest args)
+(define-compilation apply (func &rest args)
   (if (null args)
       (concat "(" (ls-compile func env fenv) ")()")
       (let ((args (butlast args))
                                              args)
                                      ", ")
                 "];" *newline*
-                "var tail = (" (ls-compile last env fenv) ");"
+                "var tail = (" (ls-compile last env fenv) ");" *newline*
                 "while (tail != false){" *newline*
                 "    args.push(tail[0]);" *newline*
-                "    args = args.slice(1);"
+                "    args = args.slice(1);" *newline*
                 "}" *newline*
                 "return f.apply(this, args);" *newline*
                 "}" *newline*))))