diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-08-16 17:13:26 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-08-16 17:13:26 -0700 |
commit | da925d0697d61db6d265fd78cab3d1adf214950c (patch) | |
tree | 24fcbd533c7c4fb1393b02497b62964178094fe7 | |
parent | 78a12c9d706f6197d3710ece04e9bd8d4eebe713 (diff) | |
download | mu-da925d0697d61db6d265fd78cab3d1adf214950c.tar.gz |
3203 - testable interface for reading a file
This commit was written by Stephen Malina. Thanks also to Stephen for running into the bug of commit 3202.
-rw-r--r-- | 088file.mu | 31 | ||||
-rw-r--r-- | filesystem.mu | 16 |
2 files changed, 47 insertions, 0 deletions
diff --git a/088file.mu b/088file.mu new file mode 100644 index 00000000..25263fa9 --- /dev/null +++ b/088file.mu @@ -0,0 +1,31 @@ +# Wrappers around file-system primitives that take a 'filesystem' object and +# are thus easier to test. + +container filesystem [ + {data: (address table (address array character) (address array character))} +] + +def start-reading fs:address:filesystem, filename:address:array:character -> contents:address:source:character [ + local-scope + load-ingredients + x:number/file <- $open-file-for-reading filename + contents:address:source:character, sink:address:sink:character <- new-channel 30 + $print [sink: ], sink, 10/newline + chan:address:channel:character <- get *sink, chan:offset + $print [chan in start-reading: ], chan, 10/newline + start-running transmit x, sink +] + +def transmit file:number, sink:address:sink:character -> file:number, sink:address:sink:character [ + local-scope + load-ingredients + { + c:character <- $read-from-file file + break-unless c + sink <- write sink, c + loop + } + $print [closing chan after reading file], 10/newline + sink <- close sink + $print [returning from 'transmit'], 10/newline +] diff --git a/filesystem.mu b/filesystem.mu new file mode 100644 index 00000000..7aa919e6 --- /dev/null +++ b/filesystem.mu @@ -0,0 +1,16 @@ +def main [ + local-scope + $print [file to read from: ], [/tmp/mu-fs] + # initialize filesystem + fs:address:filesystem <- copy 0/real-filesystem + content-source:address:source:character <- start-reading fs, [/tmp/mu-fs] + # read from channel until exhausted and print out characters + { + c:character, done?:boolean, content-source <- read content-source + break-if done? + $print [Next: ], c, 10/newline + loop + } + $print [Done reading], 10/newline + # TODO: writing to file +] |