about summary refs log tree commit diff stats
path: root/cpp/062array.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-04-28 14:29:19 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-04-28 14:29:19 -0700
commit9636c7ae2417a32831123c4f4ae3900c0a0bea16 (patch)
tree8812de468519831e03bdcdefbab845e3630b1831 /cpp/062array.mu
parent5f6cf0629d36a15845b34cbb0fa45df931f92e0b (diff)
downloadmu-9636c7ae2417a32831123c4f4ae3900c0a0bea16.tar.gz
1214
Diffstat (limited to 'cpp/062array.mu')
-rw-r--r--cpp/062array.mu40
1 files changed, 40 insertions, 0 deletions
diff --git a/cpp/062array.mu b/cpp/062array.mu
new file mode 100644
index 00000000..3afecf5c
--- /dev/null
+++ b/cpp/062array.mu
@@ -0,0 +1,40 @@
+scenario array-from-args [
+  run [
+    1:address:array:location <- init-array 0:literal, 1:literal, 2:literal
+    2:array:location <- copy 1:address:array:location/deref
+  ]
+  memory should contain [
+    2 <- 3  # array length
+    3 <- 0
+    4 <- 1
+    5 <- 2
+  ]
+]
+
+# create an array out of a list of scalar args
+recipe init-array [
+  default-space:address:array:location <- new location:type, 30:literal
+  capacity:integer <- copy 0:literal
+  {
+    # while read curr-value
+    curr-value:location, exists?:boolean <- next-ingredient
+    break-unless exists?:boolean
+    capacity:integer <- add capacity:integer, 1:literal
+    loop
+  }
+  result:address:array:location <- new location:type, capacity:integer
+  rewind-ingredients
+  i:integer <- copy 0:literal
+  {
+    # while read curr-value
+    done?:boolean <- greater-or-equal i:integer, capacity:integer
+    break-if done?:boolean
+    curr-value:location, exists?:boolean <- next-ingredient
+    assert exists?:boolean, [error in rewinding ingredients to init-array]
+    tmp:address:location <- index-address result:address:array:location/deref, i:integer
+    tmp:address:location/deref <- copy curr-value:location
+    i:integer <- add i:integer, 1:literal
+    loop
+  }
+  reply result:address:array:location
+]