Finished (with-input-from-file) and (with-output-to-file)

This commit is contained in:
Justin Ethier 2015-06-24 20:51:28 -04:00
parent b3229da15e
commit 39cb68cfae
3 changed files with 28 additions and 33 deletions

View file

@ -17,7 +17,16 @@
(call-with-port (open-input-file string) proc)) (call-with-port (open-input-file string) proc))
(define (call-with-output-file string proc) (define (call-with-output-file string proc)
(call-with-port (open-output-file string) proc)) (call-with-port (open-output-file string) proc))
(define (with-input-from-file string thunk) #f) (define (with-input-from-file string thunk)
;; Have to do this the long way since parameterize is not available
(let ((old (current-input-port))
(new ((current-input-port '<param-convert>) (open-input-file string))))
(dynamic-wind
(lambda () (current-input-port '<param-set!> new))
thunk
(lambda ()
(close-port (current-input-port))
(current-input-port '<param-set!> old)))))
(define (with-output-to-file string thunk) (define (with-output-to-file string thunk)
;; Have to do this the long way since parameterize is not available ;; Have to do this the long way since parameterize is not available
(let ((old (current-output-port)) (let ((old (current-output-port))

View file

@ -3,38 +3,8 @@
(scheme write)) (scheme write))
; TODO: I think this compiles OK (test), but interpreter does not like it: ; TODO: I think this compiles OK (test), but interpreter does not like it:
;cyclone> ( call-with-output-file "test.txt" (lambda () #f)) ;cyclone>
(call-with-output-file "test.txt" (lambda (port) (write 'ok port)))
;Error: Unable to evaluate: ((procedure () ...) <port>) ;Error: Unable to evaluate: ((procedure () ...) <port>)
; TODO: need to get this working in compiler, then try interpreter:
;(with-output-to-file
; "test.out"
; (lambda ()
; (write 'hello)
; (display " ")
; (display 'world)))
; BEGIN test code - trying to get definition of with-output-to-file to work
(define (my-make-parameter init . o)
(let* ((converter
(if (pair? o) (car o) (lambda (x) x)))
(value (converter init)))
(lambda args
(cond
((null? args)
value)
((eq? (car args) '<param-set!>)
(set! value (cadr args)))
((eq? (car args) '<param-convert>)
converter)
(else
(error "bad parameter syntax"))))))
(define my-param
(my-make-parameter (current-output-port)));(Cyc-stdout)))
(define old (my-param))
(define new ((my-param '<param-convert>) (open-output-file "test.txt")))
; The next line seems to crash in icyc but not in compiled code (until write, at least). what's going on??
(my-param '<param-set!> new)
(write 'test (my-param))
;(write 'hello-world)
; END test code

View file

@ -1,5 +1,7 @@
(import (scheme base) (import (scheme base)
(scheme char) (scheme char)
(scheme file)
(scheme read)
(scheme write) (scheme write)
(scheme eval)) (scheme eval))
@ -263,6 +265,20 @@
(assert:equal "vector-fill!" a #(1 2 smash smash 5)) (assert:equal "vector-fill!" a #(1 2 smash smash 5))
;; END vectors ;; END vectors
;; I/O
(with-output-to-file
"test.out"
(lambda ()
(write 'hello-world)))
;(write "done with output")
(with-input-from-file
"test.out"
(lambda ()
(assert:equal "I/O with-*-file test" (read) 'hello-world)))
;(write "done with input")
;; TODO: (delete-file "test.out")
;; END I/O
; TODO: use display, output without surrounding quotes ; TODO: use display, output without surrounding quotes
(write (list *num-passed* " tests passed with no errors")) (write (list *num-passed* " tests passed with no errors"))
;; ;;