From 513ba3308502df6b576afd95aa20a57e0ec76d1f Mon Sep 17 00:00:00 2001
From: "Kartik K. Agaram"
//: Clean syntax to manipulate and check the screen in scenarios.
//: Instructions 'assume-screen' and 'screen-should-contain' implicitly create
-//: a variable called 'screen' that is accessible inside other 'run'
-//: instructions in the scenario. 'screen-should-contain' can check unicode
-//: characters in the fake screen
+//: a variable called 'screen' that is accessible to later instructions in the
+//: scenario. 'screen-should-contain' can check unicode characters in the fake
+//: screen
//: first make sure we don't mangle these functions in other transforms
:(before "End initialize_transform_rewrite_literal_string_to_text()")
diff --git a/html/087file.cc.html b/html/087file.cc.html
index 7a0843f5..d8af7ae0 100644
--- a/html/087file.cc.html
+++ b/html/087file.cc.html
@@ -158,8 +158,8 @@ put(Recipe_ordinal,(get(Recipe, r).name) << "first ingredient of '$write-to-file' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
break;
}
- if (!is_mu_number(inst.ingredients.at(1))) {
- raise << maybe(get(Recipe, r).name) << "second ingredient of '$write-to-file' should be a number, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
+ if (!is_mu_character(inst.ingredients.at(1))) {
+ raise << maybe(get(Recipe, r).name) << "second ingredient of '$write-to-file' should be a character, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
break;
}
break;
diff --git a/html/088file.mu.html b/html/088file.mu.html
index a8f2153c..e2dec15c 100644
--- a/html/088file.mu.html
+++ b/html/088file.mu.html
@@ -41,15 +41,12 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
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
+ file:number <- $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
+ start-running transmit-from-file file, sink
]
-def transmit file:number, sink:address:sink:character -> file:number, sink:address:sink:character [
+def transmit-from-file file:number, sink:address:sink:character -> file:number, sink:address:sink:character [
local-scope
load-ingredients
{
@@ -58,9 +55,28 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
sink <- write sink, c
loop
}
- $print [closing chan after reading file], 10/newline
sink <- close sink
- $print [returning from 'transmit'], 10/newline
+ $close-file file
+]
+
+def start-writing fs:address:filesystem, filename:address:array:character -> sink:address:sink:character, routine-id:number [
+ local-scope
+ load-ingredients
+ file:number <- $open-file-for-writing filename
+ source:address:source:character, sink:address:sink:character <- new-channel 30
+ routine-id <- start-running transmit-to-file file, source
+]
+
+def transmit-to-file file:number, source:address:source:character -> file:number, source:address:source:character [
+ local-scope
+ load-ingredients
+ {
+ c:character, done?:boolean, source <- read source
+ break-if done?
+ $write-to-file file, c
+ loop
+ }
+ $close-file file
]
+# example program: copy one file into another, character by character +# BEWARE: this will modify your file system +# before running it, put some text into /tmp/mu-x +# after running it, check /tmp/mu-y + 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 + source-file:address:source:character <- start-reading 0/real-filesystem, [/tmp/mu-x] + sink-file:address:sink:character, write-routine:number <- start-writing 0/real-filesystem, [/tmp/mu-y] { - c:character, done?:boolean, content-source <- read content-source + c:character, done?:boolean, source-file <- read source-file break-if done? - $print [Next: ], c, 10/newline + eof?:boolean <- equal c, -1 + break-if eof? + sink-file <- write sink-file, c loop } - $print [Done reading], 10/newline - # TODO: writing to file + close sink-file + # make sure to wait for the file to be actually written to disk + # (Mu practices structured concurrency: http://250bpm.com/blog:71) + wait-for-routine write-routine ]