about summary refs log tree commit diff stats
path: root/destructive-file-operations.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-08-13 23:10:25 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-08-13 23:10:25 -0700
commit734eef7c0ee9d4626aec7a44b5dbf1d1d92f010f (patch)
tree716400ead036613cb2bb51b13bd7350a016cd812 /destructive-file-operations.mu
parentdff1abb2e10ca2dd064a505dfb3d67635f680a9a (diff)
downloadmu-734eef7c0ee9d4626aec7a44b5dbf1d1d92f010f.tar.gz
3183 - cleanup
- New plan
Primitives:
  $open-file-for-reading
  $open-file-for-writing
  $read-from-file
  $write-to-file
  $close-file

The '$' prefix indicates that none of these are intended to be used
directly since they rely on type-system-busting numbers. Also that they
are just temporary hacks depending on primitives provided by the host
system. A putative 'Mu machine' would have very different primitives.

Testable interfaces:
- start-reading: starts a routine to read from a file and returns the
  source where the contents will become available.
- start-writing: starts a routine to write to a file and returns the
  sink where the contents can be provided.

Both operate on the real file-system if the first 'filesystem'
ingredient is 0.

Once you start them up you can read/write/close the channel as usual.
Diffstat (limited to 'destructive-file-operations.mu')
-rw-r--r--destructive-file-operations.mu18
1 files changed, 18 insertions, 0 deletions
diff --git a/destructive-file-operations.mu b/destructive-file-operations.mu
new file mode 100644
index 00000000..d521b774
--- /dev/null
+++ b/destructive-file-operations.mu
@@ -0,0 +1,18 @@
+# example program: read a character from one file and write it to another
+# BEWARE: this will modify your file system
+# before running it, put a character into /tmp/mu-x
+# after running it, check /tmp/mu-y
+
+def main [
+  local-scope
+  f:number/file <- $open-file-for-reading [/tmp/mu-x]
+  $print [file to read from: ], f, 10/newline
+  c:character <- $read-from-file f
+  $print [copying ], c, 10/newline
+  f <- $close-file f
+  $print [file after closing: ], f, 10/newline
+  f <- $open-file-for-writing [/tmp/mu-y]
+  $print [file to write to: ], f, 10/newline
+  $write-to-file f, c
+  f <- $close-file f
+]