about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-04-18 10:47:35 -0700
committerKartik K. Agaram <vc@akkartik.com>2017-04-18 10:47:35 -0700
commitd6ae00788a2910e16313e8d83906dd4a52cabd43 (patch)
tree74abe815d9d66a84ea4a0af39076d864dc64dedd
parent5fadf6f371aa34208588b91a86657d8d5e2c6979 (diff)
downloadmu-d6ae00788a2910e16313e8d83906dd4a52cabd43.tar.gz
3828 - make buffers shape-shifting (generic)
-rw-r--r--061text.mu33
-rw-r--r--064list.mu6
-rw-r--r--075channel.mu4
-rw-r--r--088file.mu4
4 files changed, 23 insertions, 24 deletions
diff --git a/061text.mu b/061text.mu
index 770d74fc..58a7b230 100644
--- a/061text.mu
+++ b/061text.mu
@@ -123,28 +123,27 @@ scenario text-equal-common-lengths-but-distinct [
 ]
 
 # A new type to help incrementally construct texts.
-# todo: make this shape-shifting.
-container buffer [
+container buffer:_elem [
   length:num
-  data:text
+  data:&:@:_elem
 ]
 
-def new-buffer capacity:num -> result:&:buffer [
+def new-buffer capacity:num -> result:&:buffer:_elem [
   local-scope
   load-ingredients
-  result <- new buffer:type
+  result <- new {(buffer _elem): type}
   *result <- put *result, length:offset, 0
   {
     break-if capacity
     # capacity not provided
     capacity <- copy 10
   }
-  data:text <- new character:type, capacity
+  data:&:@:_elem <- new _elem:type, capacity
   *result <- put *result, data:offset, data
   return result
 ]
 
-def grow-buffer buf:&:buffer -> buf:&:buffer [
+def grow-buffer buf:&:buffer:_elem -> buf:&:buffer:_elem [
   local-scope
   load-ingredients
   # double buffer size
@@ -165,7 +164,7 @@ def grow-buffer buf:&:buffer -> buf:&:buffer [
   }
 ]
 
-def buffer-full? in:&:buffer -> result:bool [
+def buffer-full? in:&:buffer:_elem -> result:bool [
   local-scope
   load-ingredients
   len:num <- get *in, length:offset
@@ -175,7 +174,7 @@ def buffer-full? in:&:buffer -> result:bool [
 ]
 
 # most broadly applicable definition of append to a buffer: just call to-text
-def append buf:&:buffer, x:_elem -> buf:&:buffer [
+def append buf:&:buffer:char, x:_elem -> buf:&:buffer:char [
   local-scope
   load-ingredients
   text:text <- to-text x
@@ -191,7 +190,7 @@ def append buf:&:buffer, x:_elem -> buf:&:buffer [
   }
 ]
 
-def append buf:&:buffer, c:char -> buf:&:buffer [
+def append buf:&:buffer:char, c:char -> buf:&:buffer:char [
   local-scope
   load-ingredients
   len:num <- get *buf, length:offset
@@ -217,7 +216,7 @@ def append buf:&:buffer, c:char -> buf:&:buffer [
   *buf <- put *buf, length:offset, len
 ]
 
-def append buf:&:buffer, t:text -> buf:&:buffer [
+def append buf:&:buffer:char, t:text -> buf:&:buffer:char [
   local-scope
   load-ingredients
   len:num <- length *t
@@ -234,7 +233,7 @@ def append buf:&:buffer, t:text -> buf:&:buffer [
 
 scenario append-to-empty-buffer [
   local-scope
-  x:&:buffer <- new-buffer
+  x:&:buffer:char <- new-buffer
   run [
     c:char <- copy 97/a
     x <- append x, c
@@ -252,7 +251,7 @@ scenario append-to-empty-buffer [
 
 scenario append-to-buffer [
   local-scope
-  x:&:buffer <- new-buffer
+  x:&:buffer:char <- new-buffer
   c:char <- copy 97/a
   x <- append x, c
   run [
@@ -274,7 +273,7 @@ scenario append-to-buffer [
 
 scenario append-grows-buffer [
   local-scope
-  x:&:buffer <- new-buffer 3
+  x:&:buffer:char <- new-buffer 3
   s1:text <- get *x, data:offset
   x <- append x, [abc]  # buffer is now full
   s2:text <- get *x, data:offset
@@ -311,7 +310,7 @@ scenario append-grows-buffer [
 
 scenario buffer-append-handles-backspace [
   local-scope
-  x:&:buffer <- new-buffer
+  x:&:buffer:char <- new-buffer
   x <- append x, [ab]
   run [
     c:char <- copy 8/backspace
@@ -326,7 +325,7 @@ scenario buffer-append-handles-backspace [
   ]
 ]
 
-def buffer-to-array in:&:buffer -> result:text [
+def buffer-to-array in:&:buffer:_elem -> result:&:@:_elem [
   local-scope
   load-ingredients
   {
@@ -360,7 +359,7 @@ def buffer-to-array in:&:buffer -> result:text [
 def append first:text -> result:text [
   local-scope
   load-ingredients
-  buf:&:buffer <- new-buffer 30
+  buf:&:buffer:char <- new-buffer 30
   # append first ingredient
   {
     break-unless first
diff --git a/064list.mu b/064list.mu
index eb6e0dc9..c26b302a 100644
--- a/064list.mu
+++ b/064list.mu
@@ -293,7 +293,7 @@ scenario reverse-list [
 def to-text in:&:list:_elem -> result:text [
   local-scope
   load-ingredients
-  buf:&:buffer <- new-buffer 80
+  buf:&:buffer:char <- new-buffer 80
   buf <- to-buffer in, buf
   result <- buffer-to-array buf
 ]
@@ -302,12 +302,12 @@ def to-text in:&:list:_elem -> result:text [
 def to-text-line in:&:list:_elem -> result:text [
   local-scope
   load-ingredients
-  buf:&:buffer <- new-buffer 80
+  buf:&:buffer:char <- new-buffer 80
   buf <- to-buffer in, buf, 6  # max elements to display
   result <- buffer-to-array buf
 ]
 
-def to-buffer in:&:list:_elem, buf:&:buffer -> buf:&:buffer [
+def to-buffer in:&:list:_elem, buf:&:buffer:char -> buf:&:buffer:char [
   local-scope
   load-ingredients
   {
diff --git a/075channel.mu b/075channel.mu
index c1d2d5f3..da5ca7e2 100644
--- a/075channel.mu
+++ b/075channel.mu
@@ -391,7 +391,7 @@ def buffer-lines in:&:source:char, buffered-out:&:sink:char -> buffered-out:&:si
   # repeat forever
   eof?:bool <- copy 0/false
   {
-    line:&:buffer <- new-buffer 30
+    line:&:buffer:char <- new-buffer 30
     # read characters from 'in' until newline, copy into line
     {
       +next-character
@@ -487,7 +487,7 @@ F buffer-lines-blocks-until-newline: channel should contain data after writing n
 def drain source:&:source:char -> result:text, source:&:source:char [
   local-scope
   load-ingredients
-  buf:&:buffer <- new-buffer 30
+  buf:&:buffer:char <- new-buffer 30
   {
     c:char, done?:bool <- read source
     break-if done?
diff --git a/088file.mu b/088file.mu
index 69e72933..f851acae 100644
--- a/088file.mu
+++ b/088file.mu
@@ -40,7 +40,7 @@ def slurp resources:&:resources, filename:text -> contents:text, error?:bool [
   load-ingredients
   source:&:source:char, error?:bool <- start-reading resources, filename
   return-if error?, 0/contents
-  buf:&:buffer <- new-buffer 30/capacity
+  buf:&:buffer:char <- new-buffer 30/capacity
   {
     c:char, done?:bool, source <- read source
     break-if done?
@@ -165,7 +165,7 @@ def transmit-to-fake-resource resources:&:resources, filename:text, source:&:sou
   lock:location <- get-location *resources, lock:offset
   wait-for-reset-then-set lock
   # compute new file contents
-  buf:&:buffer <- new-buffer 30
+  buf:&:buffer:char <- new-buffer 30
   {
     c:char, done?:bool, source <- read source
     break-if done?