about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/.traces/copy_exclusive_container34
-rw-r--r--cpp/030container1
-rw-r--r--cpp/034exclusive_container54
3 files changed, 89 insertions, 0 deletions
diff --git a/cpp/.traces/copy_exclusive_container b/cpp/.traces/copy_exclusive_container
new file mode 100644
index 00000000..289f2442
--- /dev/null
+++ b/cpp/.traces/copy_exclusive_container
@@ -0,0 +1,34 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "1", value: 0, type: 0, properties: ["1": "literal"]}
+parse/0:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "34", value: 0, type: 0, properties: ["34": "literal"]}
+parse/0:   product: {name: "2", value: 0, type: 1, properties: ["2": "integer"]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "35", value: 0, type: 0, properties: ["35": "literal"]}
+parse/0:   product: {name: "3", value: 0, type: 1, properties: ["3": "integer"]}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "1", value: 0, type: 8, properties: ["1": "integer-or-point"]}
+parse/0:   product: {name: "4", value: 0, type: 8, properties: ["4": "integer-or-point"]}
+after-brace/0: recipe main
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: copy ...
+run/0: instruction main/0
+run/0: ingredient 0 is 1
+mem/0: storing 1 in location 1
+run/0: instruction main/1
+run/0: ingredient 0 is 34
+mem/0: storing 34 in location 2
+run/0: instruction main/2
+run/0: ingredient 0 is 35
+mem/0: storing 35 in location 3
+run/0: instruction main/3
+run/0: ingredient 0 is 1
+mem/0: location 1 is 1
+mem/0: location 2 is 34
+mem/0: location 3 is 35
+mem/0: storing 1 in location 4
+mem/0: storing 34 in location 5
+mem/0: storing 35 in location 6
diff --git a/cpp/030container b/cpp/030container
index 0e013900..7f333509 100644
--- a/cpp/030container
+++ b/cpp/030container
@@ -51,6 +51,7 @@ recipe main [
 :(before "End size_of(types) Cases")
 type_info t = Type[types[0]];
 if (t.kind == container) {
+  // size of a container is the sum of the sizes of its elements
   size_t result = 0;
   for (size_t i = 0; i < t.elements.size(); ++i) {
     result += size_of(t.elements[i]);
diff --git a/cpp/034exclusive_container b/cpp/034exclusive_container
new file mode 100644
index 00000000..2c3a603d
--- /dev/null
+++ b/cpp/034exclusive_container
@@ -0,0 +1,54 @@
+//: Exclusive containers contain exactly one of a fixed number of 'variants'
+//: of different types.
+//:
+//: They also implicitly contain a tag describing precisely which variant is
+//: currently stored in them.
+
+:(before "End Mu Types Initialization")
+//: We'll use this container as a running example, with two integer elements.
+{
+int tmp = Type_number["integer-or-point"] = Next_type_number++;
+Type[tmp].size = 2;
+Type[tmp].kind = exclusive_container;
+Type[tmp].name = "integer-or-point";
+//? cout << tmp << ": " << Type[tmp].elements.size() << '\n'; //? 1
+vector<type_number> t1;
+t1.push_back(integer);
+Type[tmp].elements.push_back(t1);
+//? cout << Type[tmp].elements.size() << '\n'; //? 1
+vector<type_number> t2;
+t2.push_back(point);
+Type[tmp].elements.push_back(t2);
+//? cout << Type[tmp].elements.size() << '\n'; //? 1
+//? cout << "point: " << point << '\n'; //? 1
+Type[tmp].element_names.push_back("i");
+Type[tmp].element_names.push_back("p");
+}
+
+:(scenario "copy_exclusive_container")
+# Copying exclusive containers copies all their contents and an extra location for the tag.
+recipe main [
+  1:integer <- copy 1:literal  # 'point' variant
+  2:integer <- copy 34:literal
+  3:integer <- copy 35:literal
+  4:integer-or-point <- copy 1:integer-or-point
+]
++mem: storing 1 in location 4
++mem: storing 34 in location 5
++mem: storing 35 in location 6
+
+:(before "End size_of(types) Cases")
+if (t.kind == exclusive_container) {
+  // size of an exclusive container is the size of its largest variant
+//?   cout << "--- " << types[0] << ' ' << t.size << '\n'; //? 1
+//?   cout << "point: " << Type_number["point"] << " " << Type[Type_number["point"]].name << " " << Type[Type_number["point"]].size << '\n'; //? 1
+//?   cout << t.name << ' ' << t.size << ' ' << t.elements.size() << '\n'; //? 1
+  size_t result = 0;
+  for (size_t i = 0; i < t.size; ++i) {
+    size_t tmp = size_of(t.elements[i]);
+//?     cout << i << ": " << t.elements[i][0] << ' ' << tmp << ' ' << result << '\n'; //? 1
+    if (tmp > result) result = tmp;
+  }
+  // ...+1 for its tag.
+  return result+1;
+}