3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017 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 (define-macro (define-module module . rest) #t)
26 (define-macro (use-modules . rest) #t)
27 (define-macro (cond-expand-provide . rest) #t)
29 (define-macro (include-from-path file)
30 (let loop ((path (cons %moduledir (string-split (or (getenv "GUILE_LOAD_PATH") "") #\:))))
31 (if (getenv "MES_DEBUG")
32 ;;(format (current-error-port) "include-from-path: ~s [PATH:~s]\n" file path)
33 (core:display-error (string-append "include-from-path: " file " [PATH:" (string-join path ":") "]\n")))
34 (if (null? path) (error "include-from-path: not found: " file)
35 (let ((file (string-append (car path) "/" file)))
36 (if (access? file R_OK) `(load ,file)
37 (loop (cdr path)))))))
39 (mes-use-module (srfi srfi-16))
42 (define (read-string c)
43 (if (eq? c #\*eof*) '()
44 (cons c (read-string (read-char)))))
45 (let ((string (list->string (read-string (read-char)))))
46 (if (getenv "MES_DEBUG")
47 (core:display-error (string-append "drained: `" string "'\n")))
50 (define (drain-input port) (read-string))
52 (define (make-string n . fill)
53 (list->string (apply make-list n fill)))
55 (define (object->string x . rest)
56 (with-output-to-string
57 (lambda () ((if (pair? rest) (car rest) write) x))))
59 (define (port-filename p) "<stdin>")
60 (define (port-line p) 0)
61 (define (simple-format port format . rest) (map (lambda (x) (display x port)) rest))
63 (define (with-input-from-string string thunk)
64 (define save-peek-char peek-char)
65 (define save-read-char read-char)
66 (define save-unread-char unread-char)
67 (if (getenv "MES_DEBUG")
68 (core:display-error (string-append "with-input-from-string: `" string "'\n")))
70 (end (string-length string)))
72 (lambda () (if (= tell end) (integer->char -1)
73 (string-ref string (- tell 1)))))
75 (lambda () (if (= tell end) (integer->char -1)
78 (string-ref string (- tell 1))))))
80 (lambda (c) (set! tell (1- tell)) c)))
82 (set! peek-char save-peek-char)
83 (set! read-char save-read-char)
84 (set! unread-char save-unread-char)
87 (define (with-input-from-file file thunk)
88 (let ((port (open-input-file file)))
90 (error 'no-such-file file)
91 (let* ((save (current-input-port))
92 (foo (set-current-input-port port))
94 (set-current-input-port save)
97 (define (with-output-to-file file thunk)
98 (let ((port (open-output-file file)))
100 (error 'cannot-open file)
101 (let* ((save (current-output-port))
102 (foo (set-current-output-port port))
104 (set-current-output-port save)
107 (define (with-output-to-port port thunk)
108 (let* ((save (current-output-port))
109 (foo (set-current-output-port port))
111 (set-current-output-port save)
114 (define open-input-string
115 (let ((save-set-current-input-port #f)
118 (if (getenv "MES_DEBUG")
119 (core:display-error (string-append "open-input-string: `" string "'\n")))
120 (set! save-set-current-input-port set-current-input-port)
121 (set! string-port (cons '*string-port* (gensym)))
122 (set! set-current-input-port
123 (let ((save-peek-char peek-char)
124 (save-read-char read-char)
125 (save-unread-char unread-char)
127 (end (string-length string)))
129 (if (not (equal? port string-port)) (save-set-current-input-port port)
132 (lambda () (if (= tell end) (integer->char -1)
133 (string-ref string (- tell 1)))))
135 (lambda () (if (= tell end) (integer->char -1)
137 (set! tell (1+ tell))
138 (string-ref string (- tell 1))))))
140 (lambda (c) (set! tell (1- tell)) c))
141 (set! set-current-input-port
143 (save-set-current-input-port port)
144 (set! peek-char save-peek-char)
145 (set! read-char save-read-char)
146 (set! unread-char save-unread-char)
147 (set! set-current-input-port save-set-current-input-port)