reindent
[jscl.git] / src / print.lisp
1 ;;; print.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; This program 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 ;; This program 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 this program.  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    ((stringp form) (if *print-escape*
40                        (concat "\"" (escape-string form) "\"")
41                      form))
42    ((functionp form)
43     (let ((name (oget form "fname")))
44       (if name
45           (concat "#<FUNCTION " name ">")
46         (concat "#<FUNCTION>"))))
47    ((listp form)
48     (concat "("
49             (join-trailing (mapcar #'write-to-string (butlast form)) " ")
50             (let ((last (last form)))
51               (if (null (cdr last))
52                   (write-to-string (car last))
53                 (concat (write-to-string (car last)) " . " (write-to-string (cdr last)))))
54             ")"))
55    ((arrayp form)
56     (concat "#" (if (zerop (length form))
57                     "()"
58                   (write-to-string (vector-to-list form)))))
59    ((packagep form)
60     (concat "#<PACKAGE " (package-name form) ">"))
61    (t
62     (concat "#<javascript object>"))))
63
64 (defun prin1-to-string (form)
65   (let ((*print-escape* t))
66     (write-to-string form)))
67
68 (defun princ-to-string (form)
69   (let ((*print-escape* nil))
70     (write-to-string form)))
71
72 (defun write-line (x)
73   (write-string x)
74   (write-string *newline*)
75   x)
76
77 (defun warn (string)
78   (write-string "WARNING: ")
79   (write-line string))
80
81 (defun print (x)
82   (write-line (prin1-to-string x))
83   x)
84
85 (defun format (destination fmt &rest args)
86   (let ((len (length fmt))
87         (i 0)
88         (res "")
89         (arguments args))
90     (while (< i len)
91       (let ((c (char fmt i)))
92         (if (char= c #\~)
93             (let ((next (char fmt (incf i))))
94               (cond
95                ((char= next #\~)
96                 (setq res (concat res "~")))
97                ((char= next #\%)
98                 (setq res (concat res *newline*)))
99                (t
100                 (setq res (concat res (format-special next (car arguments))))
101                 (setq arguments (cdr arguments)))))
102           (setq res (concat res (char-to-string c))))
103         (incf i)))
104     (if destination
105         (progn
106           (write-string res)
107           nil)
108       res)))
109
110
111 (defun format-special (chr arg)
112   (case chr
113     (#\S (prin1-to-string arg))
114     (#\a (princ-to-string arg))))