Scheme from Scratch - Bootstrap v0.20 - I/O
The last major missing piece of the puzzle for a bootstrap interpreter is the ability to work with files. Scheme has several input and output primitive procedures and I’m implementing the ones I think will be useful. It is a slightly larger amount to implement than some days have been but hopefully quite straightforward for anyone who has come this far.
input:
load
read
read-char
peek-char
input-port?
open-input-file
close-input-file
eof-object?
output:
write
write-char
output-port?
open-output-file
close-output-file
error
A sample REPL session:
$ ./scheme
Welcome to Bootstrap Scheme. Use ctrl-c to exit.
> (define out (open-output-port "asdf.txt"))
ok
> (write-char #\c out)
ok
> (close-output-port out)
ok
> (load "program.scm")
program-loaded
> (error "bad move")
"bad move"
exiting
Some of these functions required refactoring in other areas of the interpreter. I refactored the C read
function to handle the end of a source file properly. I refactored the C write
function to take a stream parameter so that the Scheme write
could write to any port. I’ll backport these changes to previous versions eventually.
I’m not doing any of the the current-input-port
, call-with-input-port
, and with-input-from-file
business. If a port is not specified as an optional parameter to read
, write-char
, etc then C’s stdin
and stdout
are used.
I added the error
output form which write
s all of its arguments and then exits. This form is not required by R5RS, for example, but is useful.
There is still a little bit to do but we are oh so close.
There is a v0.20 branch on github for this version.
Previous article: Eval
Next article: Standard Library
Comments
Have something to write? Comment on this article.
Have something to write? Comment on this article.
Mine is on github now. It reuses a lot of code from my old scheme interpreter, so there’s some unused code lying around.
git://github.com/kbob/schetoo.git
Still no eval. The heap is kind of interesting, though. See comments here.
http://github.com/kbob/schetoo/blob/master/mem.h