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 (defun special-symbol-name (s &key uppercase)
23 (dotimes (i (length s))
24 (let ((ch (char s i)))
25 (setf dots-only (and dots-only (char= ch #\.)))
26 (when (or (terminalp ch)
28 (and uppercase (not (char= ch (char (string-upcase (string ch)) 0))))
31 (return-from special-symbol-name t))))
34 (defun potential-number (s)
39 (setf ch (and (< i n) (char s (1- (incf i)))))))
42 ((null ch) (return-from potential-number))
45 ((char= ch #\+) (next))
46 ((char= ch #\-) (next))
47 (t (return-from potential-number)))
49 (while (and ch (digit-char-p ch)) (next))
51 (return-from potential-number t)))
55 (while (and ch (digit-char-p ch)) (next))))
56 (when (or (char= ch #\E) (char= ch #\e)
57 (char= ch #\D) (char= ch #\d)
58 (char= ch #\F) (char= ch #\f)
59 (char= ch #\L) (char= ch #\l))
62 ((null ch) (return-from potential-number))
64 ((char= ch #\+) (next))
65 ((char= ch #\-) (next))
66 (t (return-from potential-number)))
67 (unless (and ch (digit-char-p ch))
68 (return-from potential-number))
69 (while (and ch (digit-char-p ch)) (next)))
72 (defun special-escape (s package)
73 (if (or (potential-number s)
74 (special-symbol-name s :uppercase (not (eq package (find-package "JS")))))
76 (dotimes (i (length s))
77 (let ((ch (char s i)))
78 (when (or (char= ch #\|)
80 (setf result (concat result "\\")))
81 (setf result (concat result (string ch)))))
85 (defvar *print-escape* t)
87 (defun write-to-string (form)
91 (multiple-value-bind (found-symbol status)
92 (find-symbol (symbol-name form))
93 (if (eq found-symbol form)
94 (special-escape (symbol-name form) *package*)
95 (let ((package (symbol-package form))
96 (name (symbol-name form)))
99 ((eq package (find-package "KEYWORD")) "")
100 (t (package-name package)))
102 (if (eq (cadr (multiple-value-list
107 (special-escape name package))))))
108 ((integerp form) (integer-to-string form))
109 ((floatp form) (float-to-string form))
113 (#\newline "newline")
115 (otherwise (string form)))))
116 ((stringp form) (if *print-escape*
117 (concat "\"" (escape-string form) "\"")
120 (let ((name (oget form "fname")))
122 (concat "#<FUNCTION " name ">")
123 (concat "#<FUNCTION>"))))
126 (join-trailing (mapcar #'write-to-string (butlast form)) " ")
127 (let ((last (last form)))
128 (if (null (cdr last))
129 (write-to-string (car last))
130 (concat (write-to-string (car last)) " . " (write-to-string (cdr last)))))
133 (concat "#" (if (zerop (length form))
135 (write-to-string (vector-to-list form)))))
137 (concat "#<PACKAGE " (package-name form) ">"))
139 (concat "#<javascript object>"))))
141 (defun prin1-to-string (form)
142 (let ((*print-escape* t))
143 (write-to-string form)))
145 (defun princ-to-string (form)
146 (let ((*print-escape* nil))
147 (write-to-string form)))
149 (defun write-line (x)
151 (write-string *newline*)
155 (write-string "WARNING: ")
159 (write-line (prin1-to-string x))
162 (defun format (destination fmt &rest args)
163 (let ((len (length fmt))
168 (let ((c (char fmt i)))
170 (let ((next (char fmt (incf i))))
175 (concatf res *newline*))
177 (concatf res (format-special next (car arguments)))
179 (setq res (concat res (char-to-string c))))
187 (defun format-special (chr arg)
189 (#\S (prin1-to-string arg))
190 (#\a (princ-to-string arg))))