about summary refs log tree commit diff stats
path: root/072array.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
commitc603cd6cef43100aa83a62e15f96fd54c9fb987e (patch)
treed8631b1964a0200d170a996314f6e023686ff8de /072array.mu
parent05d3592047a76db4cc8d77102508c21ca1b86e7b (diff)
downloadmu-c603cd6cef43100aa83a62e15f96fd54c9fb987e.tar.gz
2430 - make room for more transforms
Diffstat (limited to '072array.mu')
-rw-r--r--072array.mu40
1 files changed, 40 insertions, 0 deletions
diff --git a/072array.mu b/072array.mu
new file mode 100644
index 00000000..03348c3a
--- /dev/null
+++ b/072array.mu
@@ -0,0 +1,40 @@
+scenario array-from-args [
+  run [
+    1:address:array:character <- new-array 0, 1, 2
+    2:array:character <- copy *1:address:array:character
+  ]
+  memory-should-contain [
+    2 <- 3  # array length
+    3 <- 0
+    4 <- 1
+    5 <- 2
+  ]
+]
+
+# create an array out of a list of scalar args
+recipe new-array [
+  local-scope
+  capacity:number <- copy 0
+  {
+    # while read curr-value
+    curr-value:character, exists?:boolean <- next-ingredient
+    break-unless exists?
+    capacity <- add capacity, 1
+    loop
+  }
+  result:address:array:character <- new character:type, capacity
+  rewind-ingredients
+  i:number <- copy 0
+  {
+    # while read curr-value
+    done?:boolean <- greater-or-equal i, capacity
+    break-if done?
+    curr-value:character, exists?:boolean <- next-ingredient
+    assert exists?, [error in rewinding ingredients to new-array]
+    tmp:address:character <- index-address *result, i
+    *tmp <- copy curr-value
+    i <- add i, 1
+    loop
+  }
+  reply result
+]