about summary refs log tree commit diff stats
path: root/066stream.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-07-27 21:58:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-07-27 21:58:47 -0700
commitb462361dbedb1ebaa76fdeddacca562e9d86a956 (patch)
treea249a242c0dce38e8922490dd2b944c5f3f2a30f /066stream.mu
parent6d1e3dbaf203daa2edccfe7da1d49422e463e3d7 (diff)
downloadmu-b462361dbedb1ebaa76fdeddacca562e9d86a956.tar.gz
3154 - reorg before making 'random' more testable
Diffstat (limited to '066stream.mu')
-rw-r--r--066stream.mu64
1 files changed, 64 insertions, 0 deletions
diff --git a/066stream.mu b/066stream.mu
new file mode 100644
index 00000000..ce0b1788
--- /dev/null
+++ b/066stream.mu
@@ -0,0 +1,64 @@
+# new type to help incrementally read texts (arrays of characters)
+container stream [
+  index:number
+  data:address:array:character
+]
+
+def new-stream s:address:array:character -> result:address:stream [
+  local-scope
+  load-ingredients
+  result <- new stream:type
+  *result <- put *result, index:offset, 0
+  *result <- put *result, data:offset, s
+]
+
+def rewind in:address:stream -> in:address:stream [
+  local-scope
+  load-ingredients
+  *in <- put *in, index:offset, 0
+]
+
+def read in:address:stream -> result:character, in:address:stream [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address:array:character <- get *in, data:offset
+  len:number <- length *s
+  at-end?:boolean <- greater-or-equal idx len
+  reply-if at-end?, 0/nul, in
+  result <- index *s, idx
+  idx <- add idx, 1
+  *in <- put *in, index:offset, idx
+]
+
+def peek in:address:stream -> result:character [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address:array:character <- get *in, data:offset
+  len:number <- length *s
+  at-end?:boolean <- greater-or-equal idx len
+  reply-if at-end?, 0/nul
+  result <- index *s, idx
+]
+
+def read-line in:address:stream -> result:address:array:character, in:address:stream [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address: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
+  # write back
+  *in <- put *in, index:offset, idx
+]
+
+def end-of-stream? in:address:stream -> result:boolean [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address:array:character <- get *in, data:offset
+  len:number <- length *s
+  result <- greater-or-equal idx, len
+]