29fa9580966a3e1450e1b2cf0e3b902e41323c5a
[mes.git] / module / mes / guile.mes
1 ;;; -*-scheme-*-
2
3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of Mes.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
21 ;;; Commentary:
22
23 ;;; Code:
24
25 (define-macro (define-module module . rest) #t)
26 (define-macro (use-modules . rest) #t)
27 (define-macro (cond-expand-provide . rest) #t)
28
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)))))))
38
39 (mes-use-module (srfi srfi-16))
40
41 (define (read-string)
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")))
48     string))
49
50 (define (drain-input port) (read-string))
51
52 (define (make-string n . fill)
53   (list->string (apply make-list n fill)))
54
55 (define (object->string x . rest)
56   (with-output-to-string
57     (lambda () ((if (pair? rest) (car rest) write) x))))
58
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))
62
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")))
69   (let ((tell 0)
70         (end (string-length string)))
71     (set! peek-char
72           (lambda () (if (= tell end) (integer->char -1)
73                          (string-ref string (- tell 1)))))
74     (set! read-char
75           (lambda () (if (= tell end) (integer->char -1)
76                          (begin
77                            (set! tell (1+ tell))
78                            (string-ref string (- tell 1))))))
79     (set! unread-char
80           (lambda (c) (set! tell (1- tell)) c)))
81   (let ((r (thunk)))
82     (set! peek-char save-peek-char)
83     (set! read-char save-read-char)
84     (set! unread-char save-unread-char)
85     r))
86
87 (define (with-input-from-file file thunk)
88   (let ((port (open-input-file file)))
89     (if (= port -1)
90         (error 'no-such-file file)
91         (let* ((save (current-input-port))
92                (foo (set-current-input-port port))
93                (r (thunk)))
94           (set-current-input-port save)
95           r))))
96
97 (define (with-output-to-file file thunk)
98   (let ((port (open-output-file file)))
99     (if (= port -1)
100         (error 'cannot-open file)
101         (let* ((save (current-output-port))
102                (foo (set-current-output-port port))
103                (r (thunk)))
104           (set-current-output-port save)
105           r))))
106
107 (define (with-output-to-port port thunk)
108   (let* ((save (current-output-port))
109          (foo (set-current-output-port port))
110          (r (thunk)))
111     (set-current-output-port save)
112     r))
113
114 (define open-input-string
115   (let ((save-set-current-input-port #f)
116         (string-port #f))
117     (lambda (string)
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)
126                   (tell 0)
127                   (end (string-length string)))
128               (lambda (port)
129                 (if (not (equal? port string-port)) (save-set-current-input-port port)
130                     (begin
131                       (set! peek-char
132                             (lambda () (if (= tell end) (integer->char -1)
133                                            (string-ref string (- tell 1)))))
134                       (set! read-char
135                             (lambda () (if (= tell end) (integer->char -1)
136                                            (begin
137                                              (set! tell (1+ tell))
138                                              (string-ref string (- tell 1))))))
139                       (set! unread-char
140                             (lambda (c) (set! tell (1- tell)) c))
141                       (set! set-current-input-port
142                             (lambda (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)
148                               string-port)))))))
149       string-port)))