3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Return T if the string S contains characters which need to be
22 ;;; escaped to print the symbol name, NIL otherwise.
23 (defun escape-symbol-name-p (s &optional uppercase)
25 (dotimes (i (length s))
26 (let ((ch (char s i)))
27 (setf dots-only (and dots-only (char= ch #\.)))
28 (when (or (terminalp ch)
30 (and uppercase (not (char= ch (char (string-upcase (string ch)) 0))))
33 (return-from escape-symbol-name-p t))))
36 (defun potential-number-p (s)
41 (setf ch (and (< i n) (char s (1- (incf i)))))))
44 ((null ch) (return-from potential-number-p))
47 ((char= ch #\+) (next))
48 ((char= ch #\-) (next))
49 (t (return-from potential-number-p)))
51 (while (and ch (digit-char-p ch)) (next))
53 (return-from potential-number-p t)))
57 (while (and ch (digit-char-p ch)) (next))))
58 (when (or (char= ch #\E) (char= ch #\e)
59 (char= ch #\D) (char= ch #\d)
60 (char= ch #\F) (char= ch #\f)
61 (char= ch #\L) (char= ch #\l))
64 ((null ch) (return-from potential-number-p))
66 ((char= ch #\+) (next))
67 ((char= ch #\-) (next))
68 (t (return-from potential-number-p)))
69 (unless (and ch (digit-char-p ch))
70 (return-from potential-number-p))
71 (while (and ch (digit-char-p ch)) (next)))
74 (defun escape-token-p (string &optional uppercase)
75 (or (potential-number-p string)
76 (escape-symbol-name-p string uppercase)))
78 (defun escape-token (s &optional uppercase)
79 (if (escape-token-p s uppercase)
81 (dotimes (i (length s))
82 (let ((ch (char s i)))
83 (when (or (char= ch #\|)
85 (setf result (concat result "\\")))
86 (setf result (concat result (string ch)))))
91 (defvar *print-escape* t)
93 (defun write-to-string (form)
97 (multiple-value-bind (found-symbol status)
98 (find-symbol (symbol-name form))
99 (if (eq found-symbol form)
100 (escape-token (symbol-name form) (not (eq *package* *js-package*)))
101 (let ((package (symbol-package form))
102 (name (symbol-name form)))
105 ((eq package (find-package "KEYWORD")) "")
106 (t (escape-token (package-name package))))
108 (if (or (null package) (eq :external (second (multiple-value-list (find-symbol name package)))))
110 (escape-token name package))))))
111 ((integerp form) (integer-to-string form))
112 ((floatp form) (float-to-string form))
116 (#\newline "newline")
118 (otherwise (string form)))))
119 ((stringp form) (if *print-escape*
120 (concat "\"" (escape-string form) "\"")
123 (let ((name (oget form "fname")))
125 (concat "#<FUNCTION " name ">")
126 (concat "#<FUNCTION>"))))
129 (join-trailing (mapcar #'write-to-string (butlast form)) " ")
130 (let ((last (last form)))
131 (if (null (cdr last))
132 (write-to-string (car last))
133 (concat (write-to-string (car last)) " . " (write-to-string (cdr last)))))
136 (concat "#" (if (zerop (length form))
138 (write-to-string (vector-to-list form)))))
140 (concat "#<PACKAGE " (package-name form) ">"))
142 (concat "#<javascript object>"))))
144 (defun prin1-to-string (form)
145 (let ((*print-escape* t))
146 (write-to-string form)))
148 (defun princ-to-string (form)
149 (let ((*print-escape* nil))
150 (write-to-string form)))
152 (defun write-line (x)
154 (write-string *newline*)
158 (write-string "WARNING: ")
162 (write-line (prin1-to-string x))
165 (defun format (destination fmt &rest args)
166 (let ((len (length fmt))
171 (let ((c (char fmt i)))
173 (let ((next (char fmt (incf i))))
178 (concatf res *newline*))
180 (concatf res (format-special next (car arguments)))
182 (setq res (concat res (char-to-string c))))
190 (defun format-special (chr arg)
192 (#\S (prin1-to-string arg))
193 (#\a (princ-to-string arg))))