File: misc.jl

package info (click to toggle)
librep 0.17-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,648 kB
  • ctags: 2,969
  • sloc: ansic: 32,770; lisp: 12,399; sh: 7,971; makefile: 515; sed: 93
file content (177 lines) | stat: -rw-r--r-- 4,970 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#| misc.jl -- miscellaneous scheme support

   $Id: misc.jl,v 1.14 2001/08/08 06:00:23 jsh Exp $

   Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>

   This file is part of librep.

   librep 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 2, or (at your option)
   any later version.

   librep 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 Jade; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|#

(define-structure scheme.misc

    (export procedure? apply map for-each force
	    \#make-promise call-with-current-continuation
	    call/cc dynamic-wind eval
	    scheme-report-environment null-environment
	    interaction-environment

	    call-with-input-file call-with-output-file
	    input-port? output-port? current-input-port
	    current-output-port with-input-from-file
	    with-output-to-file open-input-file
	    open-output-file close-input-port
	    close-output-port

	    read read-char peek-char eof-object?
	    write display newline write-char
	    load %load-suffixes)

    ((open rep
	   scheme.utils
	   rep.io.files
	   rep.data.datums)
     (access rep.io.streams))

;;; control features

  (define procedure? (make-predicate functionp))

  (define (map proc . lists)
    (if (null (cdr lists))
	(mapcar proc (car lists))
      (let loop ((out nil)
		 (in lists))
	(if (car in)
	    (loop (cons (apply proc (mapcar car in)) out)
		  (mapcar cdr in))
	  (nreverse out)))))

  (define (for-each proc . lists)
    (if (null (cdr lists))
	(mapc proc (car lists))
      (let loop ((in lists))
	(when (car in)
	  (apply proc (mapcar car in))
	  (loop (mapcar cdr in))))))

  (define (force promise) (promise))

  (define (\#make-promise thunk)
    (let ((result-ready nil)
	  result)
      (lambda ()
	(unless result-ready
	  (let ((x (thunk)))
	    (unless result-ready
	      (setq result-ready t)
	      (setq result x))))
	result)))

  (define (eval form . env)
    (declare (unused env))
    (rep#eval form))

  ;; XXX support these (and in the above)
  (define (scheme-report-environment version)
    (declare (unused version))
    #f)
  (define (null-environment version)
    (declare (unused version))
    #f)
  (define (interaction-environment) #f)

;;; input and output

  (define (call-with-input-file name proc)
    (let ((file (open-input-file name)))
      (prog1
	  (proc file)
	(close-file file))))

  (define (call-with-output-file name proc)
    (let ((file (open-output-file name)))
      (prog1
	  (proc file)
	(close-file file))))

  (define input-port? (make-predicate rep.io.streams#input-stream-p))
  (define output-port? (make-predicate rep.io.streams#output-stream-p))

  (define (current-input-port) standard-input)
  (define (current-output-port) standard-output)

  (define (with-input-from-file name thunk)
    (let ((standard-input (open-input-file name)))
      (prog1
	  (thunk)
	(close-file standard-input))))

  (define (with-output-to-file name thunk)
    (let ((standard-output (open-output-file name)))
      (prog1
	  (thunk)
	(close-file standard-output))))

  (define (open-input-file name) (open-file name 'read))
  (define (open-output-file name) (open-file name 'write))

  (define (close-input-port f)
    (unless (and (filep f) (null (file-binding f)))
      close-file))
  (define close-output-port close-input-port)

;;; input

  (define eof-object (make-datum nil 'scheme-eof-object))
  (define-datum-printer 'scheme-eof-object
			(lambda (x s)
			  (declare (unused x))
			  (rep.io.streams#write s "#<scheme-eof>")))

  (define (read #!optional port)
    (condition-case nil
	(rep.io.streams#read port)
      (end-of-stream eof-object)))

  (define (read-char #!optional port)
    (or (rep.io.streams#read-char (or port standard-input)) eof-object))

  (define (peek-char #!optional port)
    (or (rep.io.streams#peek-char (or port standard-input)) eof-object))

  (define eof-object? (make-predicate (lambda (obj) (eq obj eof-object))))

;;; output

  (define (write obj #!optional port)
    (rep.io.streams#format (or port standard-output) "%S" obj))

  (define (display obj #!optional port)
    (rep.io.streams#format (or port standard-output) "%s" obj))

  (define (newline #!optional port)
    (rep.io.streams#write (or port standard-output) #\newline))

  (define (write-char char #!optional port)
    (rep.io.streams#write (or port standard-output) char))

;;; system interface

  ;; load-file doesn't search path or add suffixes. So it fits r5rs
  (define load load-file)

  (define %load-suffixes '(".scm" . ".scmc")))