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 lisp-escape-string (string)
24 (size (length string)))
26 (let ((ch (char string index)))
27 (when (or (char= ch #\") (char= ch #\\))
28 (setq output (concat output "\\")))
29 (when (or (char= ch #\newline))
30 (setq output (concat output "\\"))
32 (setq output (concat output (string ch))))
34 (concat "\"" output "\"")))
36 ;;; Return T if the string S contains characters which need to be
37 ;;; escaped to print the symbol name, NIL otherwise.
38 (defun escape-symbol-name-p (s)
40 (dotimes (i (length s))
41 (let ((ch (char s i)))
42 (setf dots-only (and dots-only (char= ch #\.)))
43 (when (or (terminalp ch)
47 (return-from escape-symbol-name-p t))))
50 ;;; Return T if the specified string can be read as a number
51 ;;; In case such a string is the name of a symbol then escaping
52 ;;; is required when printing to ensure correct reading.
53 (defun potential-number-p (string)
54 ;; The four rules for being a potential number are described in
55 ;; 2.3.1.1 Potential Numbers as Token
58 (dotimes (i (length string))
59 (let ((char (char string i)))
61 ;; Digits TODO: DIGIT-CHAR-P should work with the current
62 ;; radix here. If the radix is not decimal, then we have to
63 ;; make sure there is not a decimal-point in the string.
65 ;; Signs, ratios, decimal point and extension mark
66 ((find char "+-/._^"))
69 (when (and (< i (1- (length string)))
70 (alpha-char-p (char string (1+ i))))
71 ;; fail: adjacent letters are not number marker, or
72 ;; there is a decimal point in the string.
73 (return-from potential-number-p)))
75 ;; fail: there is a non-allowed character
76 (return-from potential-number-p)))))
78 ;; Second Rule. In particular string is not empty.
79 (find-if #'digit-char-p string)
81 (let ((first (char string 0)))
82 (and (not (char= first #\:))
83 (or (digit-char-p first)
84 (find first "+-._^"))))
86 (not (find (char string (1- (length string))) "+-)"))))
89 (mapcar #'potential-number-p
90 '("1b5000" "777777q" "1.7J" "-3/4+6.7J" "12/25/83" "27^19"
91 "3^4/5" "6//7" "3.1.2.6" "^-43^" "3.141_592_653_589_793_238_4"
92 "-3.7+2.6i-6.17j+19.6k"))
95 (mapcar #'potential-number-p '("/" "/5" "+" "1+" "1-" "foo+" "ab.cd" "_" "^" "^/-"))
97 (defun escape-token-p (string)
98 (or (potential-number-p string)
99 (escape-symbol-name-p string)))
101 ;;; Returns the token in a form that can be used for reading it back.
102 (defun escape-token (s)
103 (if (escape-token-p s)
105 (dotimes (i (length s))
106 (let ((ch (char s i)))
107 (when (or (char= ch #\|)
109 (setf result (concat result "\\")))
110 (setf result (concat result (string ch)))))
114 (defvar *print-escape* t)
115 (defvar *print-circle* nil)
117 ;;; FIXME: Please, rewrite this in a more organized way.
118 (defun write-to-string (form &optional known-objects object-ids)
119 (when (and (not known-objects) *print-circle*)
120 ;; To support *print-circle* some objects must be tracked for
121 ;; sharing: conses, arrays and apparently-uninterned symbols.
122 ;; These objects are placed in an array and a parallel array is
123 ;; used to mark if they're found multiple times by assining them
124 ;; an id starting from 1.
126 ;; After the tracking has been completed the printing phas can
127 ;; begin: if an object has an id > 0 then #<n>= is prefixed and
128 ;; the id is changed to negative. If an object has an id < 0 then
129 ;; #<-n># is printed instead of the object.
131 ;; The processing is O(n^2) with n = number of tracked
132 ;; objects. Hopefully it will become good enough when the new
133 ;; compiler is available.
134 (setf known-objects (make-array 100))
135 (setf object-ids (make-array 100))
140 (let ((i (position x known-objects)))
145 ;; KLUDGE: storage vectors are an internal
146 ;; object which the printer should not know
147 ;; about. Use standard vector with fill
149 (resize-storage-vector known-objects sz)
150 (resize-storage-vector object-ids sz))
151 (aset known-objects (1- (incf n)) x)
153 (unless (aref object-ids i)
154 (aset object-ids i (incf count))
158 ((and x (symbolp x) (null (symbol-package x)))
166 (dotimes (i (length x))
167 (visit (aref x i))))))))
170 (when (and *print-circle*
173 (and form (symbolp form) (null (symbol-package form)))))
174 (let* ((ix (position form known-objects))
175 (id (aref object-ids ix)))
178 (setf prefix (format nil "#~S=" id))
179 (aset object-ids ix (- id)))
181 (return-from write-to-string (format nil "#~S#" (- id)))))))
186 (let ((name (symbol-name form))
187 (package (symbol-package form)))
188 ;; Check if the symbol is accesible from the current package. It
189 ;; is true even if the symbol's home package is not the current
190 ;; package, because it could be inherited.
191 (if (eq form (find-symbol (symbol-name form)))
192 (escape-token (symbol-name form))
193 ;; Symbol is not accesible from *PACKAGE*, so let us prefix
194 ;; the symbol with the optional package or uninterned mark.
197 ((eq package (find-package "KEYWORD")) "")
198 (t (escape-token (package-name package))))
201 (eq (second (multiple-value-list
202 (find-symbol name package)))
206 (escape-token name)))))
207 ((integerp form) (integer-to-string form))
208 ((floatp form) (float-to-string form))
212 (#\newline "newline")
214 (otherwise (string form)))))
215 ((stringp form) (if *print-escape*
216 (lisp-escape-string form)
219 (let ((name (oget form "fname")))
221 (concat "#<FUNCTION " name ">")
222 (concat "#<FUNCTION>"))))
225 (join-trailing (mapcar (lambda (x)
226 (write-to-string x known-objects object-ids))
228 (let ((last (last form)))
229 (if (null (cdr last))
230 (write-to-string (car last) known-objects object-ids)
231 (concat (write-to-string (car last) known-objects object-ids)
233 (write-to-string (cdr last) known-objects object-ids))))
238 (dotimes (i (length form))
239 (setf result (concat result sep
240 (write-to-string (aref form i)
244 (concat result ")")))
246 (concat "#<PACKAGE " (package-name form) ">"))
247 (t "#<javascript object>")))))
249 (defun prin1-to-string (form)
250 (let ((*print-escape* t))
251 (write-to-string form)))
253 (defun princ-to-string (form)
254 (let ((*print-escape* nil))
255 (write-to-string form)))
257 (defun write-line (x)
259 (write-string *newline*)
263 (write-string "WARNING: ")
267 (write-line (prin1-to-string x))
270 (defun format (destination fmt &rest args)
271 (let ((len (length fmt))
276 (let ((c (char fmt i)))
278 (let ((next (char fmt (incf i))))
283 (concatf res *newline*))
287 (concatf res (format-special next (car arguments)))
289 (setq res (concat res (string c))))
297 (defun format-special (chr arg)
298 (case (char-upcase chr)
299 (#\S (prin1-to-string arg))
300 (#\A (princ-to-string arg))))