about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--010vm.cc32
-rw-r--r--036refcount.cc48
2 files changed, 47 insertions, 33 deletions
diff --git a/010vm.cc b/010vm.cc
index 92384f02..b35d39d8 100644
--- a/010vm.cc
+++ b/010vm.cc
@@ -366,10 +366,9 @@ reagent::reagent(const reagent& other) {
   value = other.value;
   initialized = other.initialized;
   for (int i = 0;  i < SIZE(other.properties);  ++i) {
-    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first,
-                                                    other.properties.at(i).second ? new string_tree(*other.properties.at(i).second) : NULL));
+    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first, copy(other.properties.at(i).second)));
   }
-  type = other.type ? new type_tree(*other.type) : NULL;
+  type = copy(other.type);
   // End reagent Copy Constructor
 }
 
@@ -377,8 +376,8 @@ type_tree::type_tree(const type_tree& original) {
   atom = original.atom;
   name = original.name;
   value = original.value;
-  left = original.left ? new type_tree(*original.left) : NULL;
-  right = original.right ? new type_tree(*original.right) : NULL;
+  left = copy(original.left);
+  right = copy(original.right);
 }
 
 type_tree& type_tree::operator=(const type_tree& original) {
@@ -386,9 +385,9 @@ type_tree& type_tree::operator=(const type_tree& original) {
   name = original.name;
   value = original.value;
   if (left) delete left;
-  left = original.left ? new type_tree(*original.left) : NULL;
+  left = copy(original.left);
   if (right) delete right;
-  right = original.right ? new type_tree(*original.right) : NULL;
+  right = copy(original.right);
   return *this;
 }
 
@@ -471,8 +470,8 @@ void test_compare_list_with_smaller_left_but_larger_right_identical_types() {
 string_tree::string_tree(const string_tree& original) {
   atom = original.atom;
   value = original.value;
-  left = original.left ? new string_tree(*original.left) : NULL;
-  right = original.right ? new string_tree(*original.right) : NULL;
+  left = copy(original.left);
+  right = copy(original.right);
 }
 
 reagent& reagent::operator=(const reagent& other) {
@@ -481,13 +480,12 @@ reagent& reagent::operator=(const reagent& other) {
     if (properties.at(i).second) delete properties.at(i).second;
   properties.clear();
   for (int i = 0;  i < SIZE(other.properties);  ++i)
-    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first,
-                                                    other.properties.at(i).second ? new string_tree(*other.properties.at(i).second) : NULL));
+    properties.push_back(pair<string, string_tree*>(other.properties.at(i).first, copy(other.properties.at(i).second)));
   name = other.name;
   value = other.value;
   initialized = other.initialized;
   if (type) delete type;
-  type = other.type ? new type_tree(*other.type) : NULL;
+  type = copy(other.type);
   // End reagent Copy Operator
   return *this;
 }
@@ -563,6 +561,16 @@ string_tree* property(const reagent& r, const string& name) {
   return NULL;
 }
 
+string_tree* copy(const string_tree* x) {
+  if (x == NULL) return NULL;
+  return new string_tree(*x);
+}
+
+type_tree* copy(const type_tree* x) {
+  if (x == NULL) return NULL;
+  return new type_tree(*x);
+}
+
 :(before "End Globals")
 extern const string Ignore(",");  // commas are ignored in Mu except within [] strings
 :(code)
diff --git a/036refcount.cc b/036refcount.cc
index b8b5a84a..6fd0cfc2 100644
--- a/036refcount.cc
+++ b/036refcount.cc
@@ -211,32 +211,38 @@ def main [
 +run: {3: "foo"} <- copy {2: ("address" "foo"), "lookup": ()}
 +mem: incrementing refcount of 1000: 2 -> 3
 
-:(after "End type_tree Definition")
+:(before "End type_tree Definition")
 struct address_element_info {
   int offset;  // where inside a container type (after flattening nested containers!) the address lies
   const type_tree* payload_type;  // all the information we need to compute sizes of items inside an address inside a container. Doesn't need to be a full-scale reagent, since an address inside a container can never be an array, and arrays are the only type that need to know their location to compute their size.
-  address_element_info(int o, const type_tree* p) {
-    offset = o;
-    payload_type = p;
-  }
-  address_element_info(const address_element_info& other) {
-    offset = other.offset;
-    payload_type = other.payload_type ? new type_tree(*other.payload_type) : NULL;
-  }
-  ~address_element_info() {
-    if (payload_type) {
-      delete payload_type;
-      payload_type = NULL;
-    }
-  }
-  address_element_info& operator=(const address_element_info& other) {
-    offset = other.offset;
-    if (payload_type) delete payload_type;
-    payload_type = other.payload_type ? new type_tree(*other.payload_type) : NULL;
-    return *this;
-  }
+  address_element_info(int o, const type_tree* p);
+  address_element_info(const address_element_info& other);
+  ~address_element_info();
+  address_element_info& operator=(const address_element_info& other);
 };
+:(code)
+address_element_info::address_element_info(int o, const type_tree* p) {
+  offset = o;
+  payload_type = p;
+}
+address_element_info::address_element_info(const address_element_info& other) {
+  offset = other.offset;
+  payload_type = copy(other.payload_type);
+}
+address_element_info::~address_element_info() {
+  if (payload_type) {
+    delete payload_type;
+    payload_type = NULL;
+  }
+}
+address_element_info& address_element_info::operator=(const address_element_info& other) {
+  offset = other.offset;
+  if (payload_type) delete payload_type;
+  payload_type = copy(other.payload_type);
+  return *this;
+}
 
+:(before "End type_tree Definition")
 // For exclusive containers we might sometimes have an address at some offset
 // if some other offset has a specific tag. This struct encapsulates such
 // guards.