3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017,2018 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
25 (mes-use-module (mes scm))
26 ;;(mes-use-module (mes srfi srfi-1))
28 (define (srfi-1:member x lst eq)
30 (if (eq x (car lst)) lst
31 (srfi-1:member x (cdr lst) eq))))
33 (define (next-xassq x a)
34 (and=> (srfi-1:member x a (lambda (x e) (eq? x (cdr e))))
35 (lambda (a) (xassq x (cdr a)))))
37 (define (next-xassq2 x a)
38 (and=> (srfi-1:member x a (lambda (x e) (eq? x (cdr e))))
40 (and=> (srfi-1:member x (cdr a) (lambda (x e) (eq? x (cdr e))))
41 (lambda (a) (xassq x (cdr a)))))))
43 (define (display x . rest)
44 (let* ((port (if (null? rest) (current-output-port) (car rest)))
45 (write? (and (pair? rest) (pair? (cdr rest)) (cadr rest))))
47 (define-macro (cut f slot n1)
48 `(lambda (slot) (,f slot ,n1)))
50 (define-macro (cut2 f slot n1 n2)
51 `(lambda (slot) (,f slot ,n1 ,n2)))
53 (define (display-char x port write?)
54 (cond ((and write? (or (eq? x #\") (eq? x #\\)))
57 ((and write? (eq? x #\newline))
59 (write-char #\n port))
60 (#t (write-char x port))))
62 (define (d x cont? sep)
63 (for-each (cut write-char <> port) (string->list sep))
66 (display "#<eof>" port))
68 (if (not write?) (write-char x port)
69 (let ((name (and=> (assq x '((#\nul . nul)
71 (#\backspace . backspace)
81 (if name (display name port)
82 (write-char x port)))))
84 (display "#<procedure " port)
85 (let ((name (and=> (next-xassq2 x (current-module)) car)))
88 (display (cadr (core:cdr x)) port)
91 (display "#<continuation " port)
92 (display (core:car x) port)
95 (display "#<macro " port)
96 (display (core:cdr x) port)
99 (display (number->string x) port))
101 (if (not cont?) (write-char #\( port))
102 (cond ((eq? (car x) '*circular*)
103 (display "*circ* . #-1#)" port))
104 ((eq? (car x) '*closure*)
105 (display "*closure* . #-1#)" port))
107 (display (car x) port write?)
108 (if (pair? (cdr x)) (d (cdr x) #t " ")
109 (if (and (cdr x) (not (null? (cdr x))))
112 (display (cdr x) port write?))))))
113 (if (not cont?) (write-char #\) port)))
114 ((or (keyword? x) (special? x) (string? x) (symbol? x))
115 (if (and (string? x) write?) (write-char #\" port))
116 (if (keyword? x) (display "#:" port))
117 (for-each (cut2 display-char <> port write?) (string->list x))
118 (if (and (string? x) write?) (write-char #\" port)))
121 (for-each (lambda (i)
122 (let ((x (vector-ref x i)))
125 (display (if (= i 0) "" " ") port)
126 (display "#(...)" port))
127 (d x #f (if (= i 0) "" " ")))))
128 (iota (vector-length x)))
131 (display "#<procedure " port)
132 (display (core:car x) port)
146 (display "TODO type=") (display (cell:type-name x)) (newline)))
150 (define (write-char x . rest)
151 (apply write-byte (cons (char->integer x) rest)))
153 (define (write x . rest)
154 (let ((port (if (null? rest) (current-output-port) (car rest))))
155 (display x port #t)))
157 (define (newline . rest)
158 (apply display (cons "\n" rest)))
160 (define (with-output-to-string thunk)
161 (define save-write-byte write-byte)
165 (let ((out? (or (null? rest) (eq? (car rest) (current-output-port)))))
166 (if (not out?) (apply save-write-byte (cons x rest))
168 (set! stdout (append stdout (list (integer->char x))))
171 (let ((r (apply string stdout)))
172 (set! write-byte save-write-byte)
175 (define (simple-format destination format . rest)
176 (let ((port (if (boolean? destination) (current-output-port) destination))
177 (lst (string->list format)))
178 (define (simple-format lst args)
181 (if (not (eq? c #\~)) (begin (write-char (car lst) port)
182 (simple-format (cdr lst) args))
183 (let ((c (cadr lst)))
185 ((#\A) (display (car args) port))
186 ((#\a) (display (car args) port))
187 ((#\S) (write (car args) port))
188 ((#\s) (write (car args) port))
189 (else (display (car args) port)))
190 (simple-format (cddr lst) (cdr args)))))))
192 (if destination (simple-format lst rest)
193 (with-output-to-string
194 (lambda () (simple-format lst rest))))))
196 (define format simple-format)