2016-12-18 14:48:49 +00:00
|
|
|
;;; -*-scheme-*-
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
2018-01-01 14:53:13 +00:00
|
|
|
;;; Copyright © 2016,2017,2018 Jan Nieuwenhuizen <janneke@gnu.org>
|
2016-12-18 14:48:49 +00:00
|
|
|
;;;
|
|
|
|
;;; This file is part of Mes.
|
|
|
|
;;;
|
|
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Mes is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define-macro (define-module module . rest) #t)
|
|
|
|
(define-macro (use-modules . rest) #t)
|
2017-05-06 21:47:45 +01:00
|
|
|
(define-macro (cond-expand-provide . rest) #t)
|
|
|
|
|
2017-05-07 07:23:20 +01:00
|
|
|
(define-macro (include-from-path file)
|
|
|
|
(let loop ((path (cons %moduledir (string-split (or (getenv "GUILE_LOAD_PATH") "") #\:))))
|
2017-12-09 19:10:57 +00:00
|
|
|
(if (getenv "MES_DEBUG")
|
|
|
|
;;(format (current-error-port) "include-from-path: ~s [PATH:~s]\n" file path)
|
|
|
|
(core:display-error (string-append "include-from-path: " file " [PATH:" (string-join path ":") "]\n")))
|
2017-05-07 07:23:20 +01:00
|
|
|
(if (null? path) (error "include-from-path: not found: " file)
|
|
|
|
(let ((file (string-append (car path) "/" file)))
|
|
|
|
(if (access? file R_OK) `(load ,file)
|
|
|
|
(loop (cdr path)))))))
|
|
|
|
|
2018-01-01 14:53:13 +00:00
|
|
|
(mes-use-module (mes catch))
|
|
|
|
(mes-use-module (mes posix))
|
2017-05-06 21:47:45 +01:00
|
|
|
(mes-use-module (srfi srfi-16))
|
2018-01-01 14:53:13 +00:00
|
|
|
(mes-use-module (mes display))
|
2016-12-18 14:48:49 +00:00
|
|
|
|
2018-01-06 06:58:23 +00:00
|
|
|
(if #t ;;(not (defined? 'read-string))
|
|
|
|
(define (read-string)
|
|
|
|
(define (read-string c)
|
|
|
|
(if (eq? c #\*eof*) '()
|
|
|
|
(cons c (read-string (read-char)))))
|
|
|
|
(let ((string (list->string (read-string (read-char)))))
|
|
|
|
(if (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "drained: `" string "'\n")))
|
|
|
|
string)))
|
2017-12-09 19:10:57 +00:00
|
|
|
|
|
|
|
(define (drain-input port) (read-string))
|
2017-01-03 22:46:44 +00:00
|
|
|
|
2016-12-25 23:17:21 +00:00
|
|
|
(define (make-string n . fill)
|
|
|
|
(list->string (apply make-list n fill)))
|
|
|
|
|
|
|
|
(define (object->string x . rest)
|
|
|
|
(with-output-to-string
|
|
|
|
(lambda () ((if (pair? rest) (car rest) write) x))))
|
|
|
|
|
2016-12-18 14:48:49 +00:00
|
|
|
(define (port-filename p) "<stdin>")
|
|
|
|
(define (port-line p) 0)
|
|
|
|
|
|
|
|
(define (with-input-from-string string thunk)
|
|
|
|
(define save-peek-char peek-char)
|
|
|
|
(define save-read-char read-char)
|
|
|
|
(define save-unread-char unread-char)
|
2017-12-09 19:10:57 +00:00
|
|
|
(if (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "with-input-from-string: `" string "'\n")))
|
2016-12-18 14:48:49 +00:00
|
|
|
(let ((tell 0)
|
|
|
|
(end (string-length string)))
|
|
|
|
(set! peek-char
|
2018-01-04 17:35:26 +00:00
|
|
|
(lambda ()
|
|
|
|
(if (= tell end) (integer->char -1)
|
|
|
|
(string-ref string tell))))
|
2016-12-18 14:48:49 +00:00
|
|
|
(set! read-char
|
|
|
|
(lambda () (if (= tell end) (integer->char -1)
|
|
|
|
(begin
|
|
|
|
(set! tell (1+ tell))
|
|
|
|
(string-ref string (- tell 1))))))
|
|
|
|
(set! unread-char
|
|
|
|
(lambda (c) (set! tell (1- tell)) c)))
|
|
|
|
(let ((r (thunk)))
|
|
|
|
(set! peek-char save-peek-char)
|
|
|
|
(set! read-char save-read-char)
|
|
|
|
(set! unread-char save-unread-char)
|
|
|
|
r))
|
|
|
|
|
|
|
|
(define (with-input-from-file file thunk)
|
|
|
|
(let ((port (open-input-file file)))
|
|
|
|
(if (= port -1)
|
2017-04-02 16:01:22 +01:00
|
|
|
(error 'no-such-file file)
|
2016-12-18 14:48:49 +00:00
|
|
|
(let* ((save (current-input-port))
|
|
|
|
(foo (set-current-input-port port))
|
|
|
|
(r (thunk)))
|
|
|
|
(set-current-input-port save)
|
|
|
|
r))))
|
2017-04-01 11:51:35 +01:00
|
|
|
|
2017-05-19 05:56:47 +01:00
|
|
|
(define (with-output-to-file file thunk)
|
|
|
|
(let ((port (open-output-file file)))
|
|
|
|
(if (= port -1)
|
|
|
|
(error 'cannot-open file)
|
|
|
|
(let* ((save (current-output-port))
|
|
|
|
(foo (set-current-output-port port))
|
|
|
|
(r (thunk)))
|
|
|
|
(set-current-output-port save)
|
|
|
|
r))))
|
|
|
|
|
|
|
|
(define (with-output-to-port port thunk)
|
|
|
|
(let* ((save (current-output-port))
|
|
|
|
(foo (set-current-output-port port))
|
|
|
|
(r (thunk)))
|
|
|
|
(set-current-output-port save)
|
|
|
|
r))
|
|
|
|
|
2018-01-02 19:41:59 +00:00
|
|
|
(define core:open-input-file open-input-file)
|
|
|
|
(define (open-input-file file)
|
|
|
|
(let ((port (core:open-input-file file)))
|
|
|
|
(when (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "open-input-file: `" file "'\n"))
|
|
|
|
(core:display-error "port=")
|
|
|
|
(core:display-error port)
|
|
|
|
(core:display-error "\n"))
|
|
|
|
port))
|
|
|
|
|
2017-04-01 11:51:35 +01:00
|
|
|
(define open-input-string
|
|
|
|
(let ((save-set-current-input-port #f)
|
|
|
|
(string-port #f))
|
|
|
|
(lambda (string)
|
2017-12-09 19:10:57 +00:00
|
|
|
(if (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "open-input-string: `" string "'\n")))
|
2017-04-01 11:51:35 +01:00
|
|
|
(set! save-set-current-input-port set-current-input-port)
|
|
|
|
(set! string-port (cons '*string-port* (gensym)))
|
|
|
|
(set! set-current-input-port
|
|
|
|
(let ((save-peek-char peek-char)
|
|
|
|
(save-read-char read-char)
|
|
|
|
(save-unread-char unread-char)
|
|
|
|
(tell 0)
|
|
|
|
(end (string-length string)))
|
|
|
|
(lambda (port)
|
2018-01-02 19:41:59 +00:00
|
|
|
(when (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "open-input-string: `" string "' save-set-current-input-port port="))
|
|
|
|
(core:display-error port)
|
|
|
|
(core:display-error "\n"))
|
2017-04-01 11:51:35 +01:00
|
|
|
(if (not (equal? port string-port)) (save-set-current-input-port port)
|
|
|
|
(begin
|
2018-01-02 19:41:59 +00:00
|
|
|
(set! tell 0)
|
2017-04-01 11:51:35 +01:00
|
|
|
(set! peek-char
|
|
|
|
(lambda () (if (= tell end) (integer->char -1)
|
2018-01-04 18:32:35 +00:00
|
|
|
(string-ref string tell))))
|
2017-04-01 11:51:35 +01:00
|
|
|
(set! read-char
|
|
|
|
(lambda () (if (= tell end) (integer->char -1)
|
|
|
|
(begin
|
|
|
|
(set! tell (1+ tell))
|
|
|
|
(string-ref string (- tell 1))))))
|
|
|
|
(set! unread-char
|
|
|
|
(lambda (c) (set! tell (1- tell)) c))
|
|
|
|
(set! set-current-input-port
|
|
|
|
(lambda (port)
|
2018-01-02 19:41:59 +00:00
|
|
|
(when (getenv "MES_DEBUG")
|
|
|
|
(core:display-error (string-append "open-input-string: `" string "' set-current-input-port port="))
|
|
|
|
(core:display-error port)
|
|
|
|
(core:display-error "\n"))
|
2017-04-01 11:51:35 +01:00
|
|
|
(save-set-current-input-port port)
|
|
|
|
(set! peek-char save-peek-char)
|
|
|
|
(set! read-char save-read-char)
|
|
|
|
(set! unread-char save-unread-char)
|
|
|
|
(set! set-current-input-port save-set-current-input-port)
|
|
|
|
string-port)))))))
|
|
|
|
string-port)))
|
2018-01-01 15:40:23 +00:00
|
|
|
|
|
|
|
(define (dirname file-name)
|
|
|
|
(let ((lst (filter (negate string-null?) (string-split file-name #\/))))
|
|
|
|
(if (<= (length lst) 1) "."
|
|
|
|
(string-join (list-head lst (1- (length lst))) "/"))))
|