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/>.
19 (/debug "loading print.lisp!")
23 (defun lisp-escape-string (string)
26 (size (length string)))
28 (let ((ch (char string index)))
29 (when (or (char= ch #\") (char= ch #\\))
30 (setq output (concat output "\\")))
31 (when (or (char= ch #\newline))
32 (setq output (concat output "\\"))
34 (setq output (concat output (string ch))))
36 (concat "\"" output "\"")))
38 ;;; Return T if the string S contains characters which need to be
39 ;;; escaped to print the symbol name, NIL otherwise.
40 (defun escape-symbol-name-p (s)
42 (dotimes (i (length s))
43 (let ((ch (char s i)))
44 (setf dots-only (and dots-only (char= ch #\.)))
45 (when (or (terminalp ch)
49 (return-from escape-symbol-name-p t))))
52 ;;; Return T if the specified string can be read as a number
53 ;;; In case such a string is the name of a symbol then escaping
54 ;;; is required when printing to ensure correct reading.
55 (defun potential-number-p (string)
56 ;; The four rules for being a potential number are described in
57 ;; 2.3.1.1 Potential Numbers as Token
60 (dotimes (i (length string))
61 (let ((char (char string i)))
63 ;; Digits TODO: DIGIT-CHAR-P should work with the current
64 ;; radix here. If the radix is not decimal, then we have to
65 ;; make sure there is not a decimal-point in the string.
67 ;; Signs, ratios, decimal point and extension mark
68 ((find char "+-/._^"))
71 (when (and (< i (1- (length string)))
72 (alpha-char-p (char string (1+ i))))
73 ;; fail: adjacent letters are not number marker, or
74 ;; there is a decimal point in the string.
75 (return-from potential-number-p)))
77 ;; fail: there is a non-allowed character
78 (return-from potential-number-p)))))
80 ;; Second Rule. In particular string is not empty.
81 (find-if #'digit-char-p string)
83 (let ((first (char string 0)))
84 (and (not (char= first #\:))
85 (or (digit-char-p first)
86 (find first "+-._^"))))
88 (not (find (char string (1- (length string))) "+-)"))))
91 (mapcar #'potential-number-p
92 '("1b5000" "777777q" "1.7J" "-3/4+6.7J" "12/25/83" "27^19"
93 "3^4/5" "6//7" "3.1.2.6" "^-43^" "3.141_592_653_589_793_238_4"
94 "-3.7+2.6i-6.17j+19.6k"))
97 (mapcar #'potential-number-p '("/" "/5" "+" "1+" "1-" "foo+" "ab.cd" "_" "^" "^/-"))
99 (defun escape-token-p (string)
100 (or (potential-number-p string)
101 (escape-symbol-name-p string)))
103 ;;; Returns the token in a form that can be used for reading it back.
104 (defun escape-token (s)
105 (if (escape-token-p s)
107 (dotimes (i (length s))
108 (let ((ch (char s i)))
109 (when (or (char= ch #\|)
111 (setf result (concat result "\\")))
112 (setf result (concat result (string ch)))))
116 (defvar *print-escape* t)
117 (defvar *print-circle* nil)
119 ;;; FIXME: Please, rewrite this in a more organized way.
120 (defun write-to-string (form &optional known-objects object-ids)
121 (when (and (not known-objects) *print-circle*)
122 ;; To support *print-circle* some objects must be tracked for
123 ;; sharing: conses, arrays and apparently-uninterned symbols.
124 ;; These objects are placed in an array and a parallel array is
125 ;; used to mark if they're found multiple times by assining them
126 ;; an id starting from 1.
128 ;; After the tracking has been completed the printing phas can
129 ;; begin: if an object has an id > 0 then #<n>= is prefixed and
130 ;; the id is changed to negative. If an object has an id < 0 then
131 ;; #<-n># is printed instead of the object.
133 ;; The processing is O(n^2) with n = number of tracked
134 ;; objects. Hopefully it will become good enough when the new
135 ;; compiler is available.
136 (setf known-objects (make-array 100))
137 (setf object-ids (make-array 100))
142 (let ((i (position x known-objects)))
147 ;; KLUDGE: storage vectors are an internal
148 ;; object which the printer should not know
149 ;; about. Use standard vector with fill
151 (resize-storage-vector known-objects sz)
152 (resize-storage-vector object-ids sz))
153 (aset known-objects (1- (incf n)) x)
155 (unless (aref object-ids i)
156 (aset object-ids i (incf count))
160 ((and x (symbolp x) (null (symbol-package x)))
168 (dotimes (i (length x))
169 (visit (aref x i))))))))
172 (when (and *print-circle*
175 (and form (symbolp form) (null (symbol-package form)))))
176 (let* ((ix (position form known-objects))
177 (id (aref object-ids ix)))
180 (setf prefix (format nil "#~S=" id))
181 (aset object-ids ix (- id)))
183 (return-from write-to-string (format nil "#~S#" (- id)))))))
188 (let ((name (symbol-name form))
189 (package (symbol-package form)))
190 ;; Check if the symbol is accesible from the current package. It
191 ;; is true even if the symbol's home package is not the current
192 ;; package, because it could be inherited.
193 (if (eq form (find-symbol (symbol-name form)))
194 (escape-token (symbol-name form))
195 ;; Symbol is not accesible from *PACKAGE*, so let us prefix
196 ;; the symbol with the optional package or uninterned mark.
199 ((eq package (find-package "KEYWORD")) "")
200 (t (escape-token (package-name package))))
203 (eq (second (multiple-value-list
204 (find-symbol name package)))
208 (escape-token name)))))
209 ((integerp form) (integer-to-string form))
210 ((floatp form) (float-to-string form))
214 (#\newline "newline")
216 (otherwise (string form)))))
217 ((stringp form) (if *print-escape*
218 (lisp-escape-string form)
221 (let ((name (oget form "fname")))
223 (concat "#<FUNCTION " name ">")
224 (concat "#<FUNCTION>"))))
227 (join-trailing (mapcar (lambda (x)
228 (write-to-string x known-objects object-ids))
230 (let ((last (last form)))
231 (if (null (cdr last))
232 (write-to-string (car last) known-objects object-ids)
233 (concat (write-to-string (car last) known-objects object-ids)
235 (write-to-string (cdr last) known-objects object-ids))))
240 (dotimes (i (length form))
241 (setf result (concat result sep
242 (write-to-string (aref form i)
246 (concat result ")")))
248 (concat "#<PACKAGE " (package-name form) ">"))
249 (t "#<javascript object>")))))
251 (defun prin1-to-string (form)
252 (let ((*print-escape* t))
253 (write-to-string form)))
255 (defun princ-to-string (form)
256 (let ((*print-escape* nil))
257 (write-to-string form)))
259 (defun write-line (x)
261 (write-string *newline*)
265 (write-string "WARNING: ")
269 (write-line (prin1-to-string x))
272 (defun format (destination fmt &rest args)
273 (let ((len (length fmt))
278 (let ((c (char fmt i)))
280 (let ((next (char fmt (incf i))))
285 (concatf res *newline*))
289 (concatf res (format-special next (car arguments)))
291 (setq res (concat res (string c))))
299 (defun format-special (chr arg)
300 (case (char-upcase chr)
301 (#\S (prin1-to-string arg))
302 (#\A (princ-to-string arg))))