Merge branch 'origin-master' into origin-format
[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 (defvar *print-escape* t)
22
23 (defun write-to-string (form)
24   (cond
25     ((symbolp form)
26      (multiple-value-bind (symbol foundp)
27          (find-symbol (symbol-name form) *package*)
28        (if (and foundp (eq symbol form))
29            (symbol-name form)
30            (let ((package (symbol-package form))
31                  (name (symbol-name form)))
32              (concat (cond
33                        ((null package) "#")
34                        ((eq package (find-package "KEYWORD")) "")
35                        (t (package-name package)))
36                      ":" name)))))
37     ((integerp form) (integer-to-string form))
38     ((floatp form) (float-to-string form))
39     ((characterp form)
40      (concat "#\\"
41              (case form
42                (#\newline "newline")
43                (#\space "space")
44                (otherwise (string form)))))
45     ((stringp form) (concat "\"" (escape-string form) "\""))
46     ((functionp form)
47      (let ((name (oget form "fname")))
48        (if name
49            (concat "#<FUNCTION " name ">")
50            (concat "#<FUNCTION>"))))
51     ((listp form)
52      (concat "("
53              (join-trailing (mapcar #'write-to-string (butlast form)) " ")
54              (let ((last (last form)))
55                (if (null (cdr last))
56                    (write-to-string (car last))
57                    (concat (write-to-string (car last)) " . " (write-to-string (cdr last)))))
58              ")"))
59     ((arrayp form)
60      (concat "#" (if (zerop (length form))
61                      "()"
62                      (write-to-string (vector-to-list form)))))
63     ((packagep form)
64      (concat "#<PACKAGE " (package-name form) ">"))
65     (t
66      (concat "#<javascript object>"))))
67
68 (defun prin1-to-string (form)
69   (let ((*print-escape* t))
70     (write-to-string form)))
71
72 (defun princ-to-string (form)
73   (let ((*print-escape* nil))
74     (write-to-string form)))
75
76 (defun write-line (x)
77   (write-string x)
78   (write-string *newline*)
79   x)
80
81 (defun warn (string)
82   (write-string "WARNING: ")
83   (write-line string))
84
85 (defun print (x)
86   (write-line (prin1-to-string x))
87   x)
88
89 (defun format (destination fmt &rest args)
90   (let ((len (length fmt))
91         (i 0)
92         (res "")
93         (arguments args))
94     (while (< i len)
95       (let ((c (char fmt i)))
96         (if (char= c #\~)
97             (let ((next (char fmt (incf i))))
98               (cond
99                ((char= next #\~)
100                 (setq res (concat res "~")))
101                ((char= next #\%)
102                 (setq res (concat res *newline*)))
103                (t
104                 (setq res (concat res (format-special next (car arguments))))
105                 (setq arguments (cdr arguments)))))
106           (setq res (concat res (char-to-string c))))
107         (incf i)))
108     (if destination
109         (progn
110           (write-string res)
111           nil)
112       res)))
113
114
115 (defun format-special (chr arg)
116   (case chr
117     (#\S (prin1-to-string arg))
118     (#\a (princ-to-string arg))))