about summary refs log tree commit diff stats
path: root/077stream.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-03-14 01:00:48 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-03-14 01:00:48 -0700
commit2badd89a58b9666563746d71069abf16f05709ea (patch)
tree779c9feb243fc8d0f33051cd8323fd23f912f373 /077stream.mu
parent8b095f802129f8c328a3a4dc3de4443890d34d59 (diff)
downloadmu-2badd89a58b9666563746d71069abf16f05709ea.tar.gz
2778 - fix all layers
Diffstat (limited to '077stream.mu')
-rw-r--r--077stream.mu41
1 files changed, 41 insertions, 0 deletions
diff --git a/077stream.mu b/077stream.mu
new file mode 100644
index 00000000..c3af2ddf
--- /dev/null
+++ b/077stream.mu
@@ -0,0 +1,41 @@
+# new type to help incrementally read texts (arrays of characters)
+container stream [
+  index:number
+  data:address:shared:array:character
+]
+
+def new-stream s:address:shared:array:character -> result:address:shared:stream [
+  local-scope
+  load-ingredients
+  result <- new stream:type
+  i:address:number <- get-address *result, index:offset
+  *i <- copy 0
+  d:address:address:shared:array:character <- get-address *result, data:offset
+  *d <- copy s
+]
+
+def rewind-stream in:address:shared:stream -> in:address:shared:stream [
+  local-scope
+  load-ingredients
+  x:address:number <- get-address *in, index:offset
+  *x <- copy 0
+]
+
+def read-line in:address:shared:stream -> result:address:shared:array:character, in:address:shared:stream [
+  local-scope
+  load-ingredients
+  idx:address:number <- get-address *in, index:offset
+  s:address:shared:array:character <- get *in, data:offset
+  next-idx:number <- find-next s, 10/newline, *idx
+  result <- copy-range s, *idx, next-idx
+  *idx <- add next-idx, 1  # skip newline
+]
+
+def end-of-stream? in:address:shared:stream -> result:boolean [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address:shared:array:character <- get *in, data:offset
+  len:number <- length *s
+  result <- greater-or-equal idx, len
+]