improved symbol printing
[jscl.git] / src / print.lisp
1 ;;; print.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
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.
10 ;;
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.
15 ;;
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/>.
18
19 ;;; Printer
20
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)
24   (let ((dots-only t))
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)
29                   (char= ch #\:)
30                   (and uppercase (not (char= ch (char (string-upcase (string ch)) 0))))
31                   (char= ch #\\)
32                   (char= ch #\|))
33           (return-from escape-symbol-name-p t))))
34     dots-only))
35
36 ;;; Return T if the specified string can be read as a number
37 ;;; In case such a string is the name of a symbol then escaping
38 ;;; is required when printing to ensure correct reading.
39 (defun potential-number-p (s)
40   (let ((i 0)
41         (n (length s))
42         (ch nil))
43     (flet ((next ()
44                  (setf ch (and (< i n) (char s (1- (incf i)))))))
45       (next)
46       (cond
47        ((null ch) (return-from potential-number-p))
48        ((digit-char-p ch))
49        ((char= ch #\.))
50        ((char= ch #\+) (next))
51        ((char= ch #\-) (next))
52        (t (return-from potential-number-p)))
53       (when ch
54         (while (and ch (digit-char-p ch)) (next))
55         (when (null ch)
56           (return-from potential-number-p t)))
57       (when (char= ch #\.)
58         (next)
59         (when ch
60           (while (and ch (digit-char-p ch)) (next))))
61       (when (or (char= ch #\E) (char= ch #\e)
62                 (char= ch #\D) (char= ch #\d)
63                 (char= ch #\F) (char= ch #\f)
64                 (char= ch #\L) (char= ch #\l))
65         (next)
66         (cond
67          ((null ch) (return-from potential-number-p))
68          ((digit-char-p ch))
69          ((char= ch #\+) (next))
70          ((char= ch #\-) (next))
71          (t (return-from potential-number-p)))
72         (unless (and ch (digit-char-p ch))
73           (return-from potential-number-p))
74         (while (and ch (digit-char-p ch)) (next)))
75       (null ch))))
76
77 (defun escape-token-p (string &optional uppercase)
78   (or (potential-number-p string)
79       (escape-symbol-name-p string uppercase)))
80
81 ;;; Returns the token in a form that can be used for
82 ;;; reading it back as a symbol in the specified package.
83 (defun escape-token (s package)
84   (if (escape-token-p s (not (eq package (find-package "JS"))))
85       (let ((result "|"))
86         (dotimes (i (length s))
87           (let ((ch (char s i)))
88             (when (or (char= ch #\|)
89                       (char= ch #\\))
90               (setf result (concat result "\\")))
91             (setf result (concat result (string ch)))))
92         (concat result "|"))
93       s))
94
95 (defvar *print-escape* t)
96
97 (defun write-to-string (form)
98   (cond
99     ((null form) "NIL")
100     ((symbolp form)
101      (multiple-value-bind (found-symbol status)
102          (find-symbol (symbol-name form))
103        (if (eq found-symbol form)
104            (escape-token (symbol-name form) *package*)
105            (let ((package (symbol-package form))
106                  (name (symbol-name form)))
107              (concat (cond
108                        ((null package) "#")
109                        ((eq package (find-package "KEYWORD")) "")
110                        (t (package-name package)))
111                      ":"
112                      (if (and package
113                               (eq (second (multiple-value-list
114                                               (find-symbol name package)))
115                                   :internal))
116                          ":"
117                          "")
118                      (escape-token name package))))))
119     ((integerp form) (integer-to-string form))
120     ((floatp form) (float-to-string form))
121     ((characterp form)
122      (concat "#\\"
123              (case form
124                (#\newline "newline")
125                (#\space "space")
126                (otherwise (string form)))))
127     ((stringp form) (if *print-escape*
128                         (concat "\"" (escape-string form) "\"")
129                         form))
130     ((functionp form)
131      (let ((name (oget form "fname")))
132        (if name
133            (concat "#<FUNCTION " name ">")
134            (concat "#<FUNCTION>"))))
135     ((listp form)
136      (concat "("
137              (join-trailing (mapcar #'write-to-string (butlast form)) " ")
138              (let ((last (last form)))
139                (if (null (cdr last))
140                    (write-to-string (car last))
141                    (concat (write-to-string (car last)) " . " (write-to-string (cdr last)))))
142              ")"))
143     ((arrayp form)
144      (concat "#" (if (zerop (length form))
145                      "()"
146                      (write-to-string (vector-to-list form)))))
147     ((packagep form)
148      (concat "#<PACKAGE " (package-name form) ">"))
149     (t
150      (concat "#<javascript object>"))))
151
152 (defun prin1-to-string (form)
153   (let ((*print-escape* t))
154     (write-to-string form)))
155
156 (defun princ-to-string (form)
157   (let ((*print-escape* nil))
158     (write-to-string form)))
159
160 (defun write-line (x)
161   (write-string x)
162   (write-string *newline*)
163   x)
164
165 (defun warn (string)
166   (write-string "WARNING: ")
167   (write-line string))
168
169 (defun print (x)
170   (write-line (prin1-to-string x))
171   x)
172
173 (defun format (destination fmt &rest args)
174   (let ((len (length fmt))
175         (i 0)
176         (res "")
177         (arguments args))
178     (while (< i len)
179       (let ((c (char fmt i)))
180         (if (char= c #\~)
181             (let ((next (char fmt (incf i))))
182               (cond
183                ((char= next #\~)
184                 (concatf res "~"))
185                ((char= next #\%)
186                 (concatf res *newline*))
187                (t
188                 (concatf res (format-special next (car arguments)))
189                 (pop arguments))))
190             (setq res (concat res (char-to-string c))))
191         (incf i)))
192     (if destination
193         (progn
194           (write-string res)
195           nil)
196         res)))
197
198 (defun format-special (chr arg)
199   (case chr
200     (#\S (prin1-to-string arg))
201     (#\a (princ-to-string arg))))