better 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 (defun special-symbol-name (s &key uppercase)
22   (let ((dots-only t))
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)
27                   (char= ch #\:)
28                   (and uppercase (not (char= ch (char (string-upcase (string ch)) 0))))
29                   (char= ch #\\)
30                   (char= ch #\|))
31           (return-from special-symbol-name t))))
32     dots-only))
33
34 (defun potential-number (s)
35   (let ((i 0)
36         (n (length s))
37         (ch nil))
38     (flet ((next ()
39                  (setf ch (and (< i n) (char s (1- (incf i)))))))
40       (next)
41       (cond
42        ((null ch) (return-from potential-number))
43        ((digit-char-p ch))
44        ((char= ch #\.))
45        ((char= ch #\+) (next))
46        ((char= ch #\-) (next))
47        (t (return-from potential-number)))
48       (when ch
49         (while (and ch (digit-char-p ch)) (next))
50         (when (null ch)
51           (return-from potential-number t)))
52       (when (char= ch #\.)
53         (next)
54         (when ch
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))
60         (next)
61         (cond
62          ((null ch) (return-from potential-number))
63          ((digit-char-p ch))
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)))
70       (null ch))))
71
72 (defun special-escape (s package)
73   (if (or (potential-number s)
74           (special-symbol-name s :uppercase (not (eq package (find-package "JS")))))
75       (let ((result "|"))
76         (dotimes (i (length s))
77           (let ((ch (char s i)))
78             (when (or (char= ch #\|)
79                       (char= ch #\\))
80               (setf result (concat result "\\")))
81             (setf result (concat result (string ch)))))
82         (concat result "|"))
83       s))
84
85 (defvar *print-escape* t)
86
87 (defun write-to-string (form)
88   (cond
89    ((null form) "NIL")
90    ((symbolp 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)))
97             (concat (cond
98                      ((null package) "#")
99                      ((eq package (find-package "KEYWORD")) "")
100                      (t (package-name package)))
101                     ":"
102                     (if (eq (cadr (multiple-value-list
103                                    (find-symbol name)))
104                             :internal)
105                         ":"
106                         "")
107                     (special-escape name package))))))
108    ((integerp form) (integer-to-string form))
109    ((floatp form) (float-to-string form))
110    ((characterp form)
111     (concat "#\\"
112             (case form
113               (#\newline "newline")
114               (#\space "space")
115               (otherwise (string form)))))
116    ((stringp form) (if *print-escape*
117                        (concat "\"" (escape-string form) "\"")
118                        form))
119    ((functionp form)
120     (let ((name (oget form "fname")))
121       (if name
122           (concat "#<FUNCTION " name ">")
123           (concat "#<FUNCTION>"))))
124    ((listp form)
125     (concat "("
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)))))
131             ")"))
132    ((arrayp form)
133     (concat "#" (if (zerop (length form))
134                     "()"
135                     (write-to-string (vector-to-list form)))))
136    ((packagep form)
137     (concat "#<PACKAGE " (package-name form) ">"))
138    (t
139     (concat "#<javascript object>"))))
140
141 (defun prin1-to-string (form)
142   (let ((*print-escape* t))
143     (write-to-string form)))
144
145 (defun princ-to-string (form)
146   (let ((*print-escape* nil))
147     (write-to-string form)))
148
149 (defun write-line (x)
150   (write-string x)
151   (write-string *newline*)
152   x)
153
154 (defun warn (string)
155   (write-string "WARNING: ")
156   (write-line string))
157
158 (defun print (x)
159   (write-line (prin1-to-string x))
160   x)
161
162 (defun format (destination fmt &rest args)
163   (let ((len (length fmt))
164         (i 0)
165         (res "")
166         (arguments args))
167     (while (< i len)
168       (let ((c (char fmt i)))
169         (if (char= c #\~)
170             (let ((next (char fmt (incf i))))
171               (cond
172                ((char= next #\~)
173                 (concatf res "~"))
174                ((char= next #\%)
175                 (concatf res *newline*))
176                (t
177                 (concatf res (format-special next (car arguments)))
178                 (pop arguments))))
179             (setq res (concat res (char-to-string c))))
180         (incf i)))
181     (if destination
182         (progn
183           (write-string res)
184           nil)
185         res)))
186
187 (defun format-special (chr arg)
188   (case chr
189     (#\S (prin1-to-string arg))
190     (#\a (princ-to-string arg))))