about summary refs log tree commit diff stats
path: root/063array.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-06-13 16:25:45 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-06-13 16:25:45 -0700
commit5e15a74f06e19e75954547cadc8bc73f9034727e (patch)
tree865531ced05be722a67dd874338c791eb4da9622 /063array.mu
parent29cc15d6b3559221e1147f1a822e10dcb22678e6 (diff)
downloadmu-5e15a74f06e19e75954547cadc8bc73f9034727e.tar.gz
3055
Diffstat (limited to '063array.mu')
-rw-r--r--063array.mu40
1 files changed, 40 insertions, 0 deletions
diff --git a/063array.mu b/063array.mu
new file mode 100644
index 00000000..8272a865
--- /dev/null
+++ b/063array.mu
@@ -0,0 +1,40 @@
+scenario array-from-args [
+  run [
+    local-scope
+    x:address:array:character <- new-array 0, 1, 2
+    10:array:character/raw <- copy *x
+  ]
+  memory-should-contain [
+    10 <- 3  # array length
+    11 <- 0
+    12 <- 1
+    13 <- 2
+  ]
+]
+
+# create an array out of a list of scalar args
+def new-array -> result:address:array:character [
+  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 <- 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]
+    *result <- put-index *result, i, curr-value
+    i <- add i, 1
+    loop
+  }
+  return result
+]