about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/.traces/copy_array37
-rw-r--r--cpp/012run2
-rw-r--r--cpp/018address6
-rw-r--r--cpp/019array54
4 files changed, 95 insertions, 4 deletions
diff --git a/cpp/.traces/copy_array b/cpp/.traces/copy_array
new file mode 100644
index 00000000..ccdba2fc
--- /dev/null
+++ b/cpp/.traces/copy_array
@@ -0,0 +1,37 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "3", type: 0}
+parse/0:   product: {name: "1", type: 1}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "14", type: 0}
+parse/0:   product: {name: "2", type: 1}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "15", type: 0}
+parse/0:   product: {name: "3", type: 1}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "16", type: 0}
+parse/0:   product: {name: "4", type: 1}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "1", type: 5}
+parse/0:   product: {name: "5", type: 5}
+run/0: instruction 0
+run/0: ingredient 0 is 3
+mem/0: storing in location 1
+run/0: instruction 1
+run/0: ingredient 0 is 14
+mem/0: storing in location 2
+run/0: instruction 2
+run/0: ingredient 0 is 15
+mem/0: storing in location 3
+run/0: instruction 3
+run/0: ingredient 0 is 16
+mem/0: storing in location 4
+run/0: instruction 4
+run/0: ingredient 0 is 1
+mem/0: location 1 is 3
+mem/0: location 2 is 14
+mem/0: location 3 is 15
+mem/0: location 4 is 16
+mem/0: storing in location 5
+mem/0: storing in location 6
+mem/0: storing in location 7
+mem/0: storing in location 8
diff --git a/cpp/012run b/cpp/012run
index 946b01d1..194a55c5 100644
--- a/cpp/012run
+++ b/cpp/012run
@@ -75,7 +75,7 @@ int to_int(string n) {
   return result;
 }
 
-int size_of(reagent r) {
+size_t size_of(reagent r) {
   type_info t = Type[r.types[0]];
   if (!t.is_record && !t.is_array) return t.size;
   return t.size;  // TODO
diff --git a/cpp/018address b/cpp/018address
index 32214761..5e53a8ce 100644
--- a/cpp/018address
+++ b/cpp/018address
@@ -43,9 +43,9 @@ recipe main [
 void write_memory(reagent x, vector<int> data) {
   x = canonize(x);
   int base = to_int(x.name);
-  size_t size = size_of(x);
-  if (size != data.size()) raise << "size mismatch in storing to " << x.to_string();
-  for (size_t offset = 0; offset < size; ++offset) {
+  if (!Type[x.types[0]].is_array && size_of(x) != data.size())
+    raise << "size mismatch in storing to " << x.to_string();
+  for (size_t offset = 0; offset < data.size(); ++offset) {
     trace("mem") << "storing in location " << base+offset;
     Memory[base+offset] = data[offset];
   }
diff --git a/cpp/019array b/cpp/019array
new file mode 100644
index 00000000..51265826
--- /dev/null
+++ b/cpp/019array
@@ -0,0 +1,54 @@
+// Support for records.
+:(before "End Mu Types")
+// We'll use this array as a running example:
+int integer_array = Type_number["integer-array"] = Next_type_number++;
+Type[integer_array].is_array = true;
+Type[integer_array].element.push_back(integer);
+
+:(scenario copy_array)
+# Arrays can be copied around with a single instruction just like integers,
+# no matter how large they are.
+recipe main [
+  1:integer <- copy 3:literal
+  2:integer <- copy 14:literal
+  3:integer <- copy 15:literal
+  4:integer <- copy 16:literal
+  5:integer-array <- copy 1:integer-array
+]
++run: instruction 4
++run: ingredient 0 is 1
++mem: location 1 is 3
++mem: location 2 is 14
++mem: location 3 is 15
++mem: location 4 is 16
++mem: storing in location 5
++mem: storing in location 6
++mem: storing in location 7
++mem: storing in location 8
+
+:(replace{} "size_t size_of(reagent r)")
+size_t size_of(reagent r) {
+  type_info t = Type[r.types[0]];
+  if (t.is_record) {
+    int result = 0;
+    for (size_t i = 0; i < t.size; ++i) {
+      ostringstream out;
+      out << result;
+      reagent x(out.str());
+      copy(t.elements[i].begin(), t.elements[i].end(), inserter(x.types, x.types.begin()));
+      result += size_of(x);
+    }
+    return result;
+  }
+  if (t.is_array) {
+    int base = to_int(r.name);
+    if (Memory[base] == 0) return 0;
+    ostringstream out;
+    out << base+1;
+    reagent x(out.str());
+    x.types.push_back(t.element[0]);
+    return 1 + Memory[base]*size_of(x);
+  }
+  // scalar
+  return t.size;
+}