about summary refs log tree commit diff stats
path: root/cpp/023length
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-04-17 10:33:26 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-04-17 10:33:26 -0700
commit353a452e691a8b55e28d7b3b1d09cf294265eba0 (patch)
tree589cd448f08b9c438cc9e97e7ab316365b2dcee4 /cpp/023length
parent1848b18f02b158861008214efd19708585bfcbe5 (diff)
downloadmu-353a452e691a8b55e28d7b3b1d09cf294265eba0.tar.gz
1074
Diffstat (limited to 'cpp/023length')
-rw-r--r--cpp/023length30
1 files changed, 30 insertions, 0 deletions
diff --git a/cpp/023length b/cpp/023length
new file mode 100644
index 00000000..9d3bb714
--- /dev/null
+++ b/cpp/023length
@@ -0,0 +1,30 @@
+//: Recipe to compute the length of an array.
+
+:(scenario "array_length")
+recipe main [
+  1:integer <- copy 3:literal
+  2:integer <- copy 14:literal
+  3:integer <- copy 15:literal
+  4:integer <- copy 16:literal
+  5:integer <- length 1:array:integer
+]
++run: instruction main/4
++mem: storing 3 in location 5
+
+:(before "End Primitive Recipe Declarations")
+LENGTH,
+:(before "End Primitive Recipe Numbers")
+Recipe_number["length"] = LENGTH;
+:(before "End Primitive Recipe Implementations")
+case LENGTH: {
+  reagent x = canonize(instructions[pc].ingredients[0]);
+  if (x.types[0] != Type_number["array"]) {
+    raise << "tried to calculate length of non-array " << x.to_string() << '\n';
+    break;
+  }
+  vector<int> result;
+//?   cout << "length: " << x.value << '\n'; //? 1
+  result.push_back(Memory[x.value]);
+  write_memory(instructions[pc].products[0], result);
+  break;
+}