From f59c2e3ecf7f101017f9845ab2102e88114d22f1 Mon Sep 17 00:00:00 2001 From: David Vazquez Date: Thu, 20 Dec 2012 18:51:22 +0000 Subject: [PATCH] Format strings --- lispstrack.lisp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lispstrack.lisp b/lispstrack.lisp index 1d86fb4..dfbfcad 100644 --- a/lispstrack.lisp +++ b/lispstrack.lisp @@ -355,12 +355,24 @@ ;;; 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: " @@ -553,7 +565,7 @@ ", ") ")")) -(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)) @@ -565,10 +577,10 @@ 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*)))) -- 1.7.10.4