about summary refs log tree commit diff stats
path: root/cpp/020array
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-03-26 21:47:29 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-03-26 21:47:29 -0700
commit7e9c69251b700dbdec47dee47f37ba1194ad88e9 (patch)
tree9ba03cb52d5318f3f66de65887520ce92fc30345 /cpp/020array
parentf608504a44b9b47682f336f5b6357b8993d9bb0d (diff)
downloadmu-7e9c69251b700dbdec47dee47f37ba1194ad88e9.tar.gz
983 - arc 'integer-array' => c++ 'array:integer'
Diffstat (limited to 'cpp/020array')
-rw-r--r--cpp/020array97
1 files changed, 48 insertions, 49 deletions
diff --git a/cpp/020array b/cpp/020array
index 5e086ebc..f6f2c0c5 100644
--- a/cpp/020array
+++ b/cpp/020array
@@ -1,25 +1,13 @@
-//: Support for arrays.
-:(before "End Mu Types Initialization")
-//: 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);
-Type[integer_array].name = "integer-array";
-
-//: update size mismatch check
-:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<int> data)")
-if (!Type[x.types[0]].is_array && size_of(x) != data.size())
-
-//: Arrays can be copied around with a single instruction just like integers,
-//: no matter how large they are.
-
+//: Arrays contain a variable number of elements of the same type.
 :(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
+  5:array:integer <- copy 1:array:integer
 ]
 +run: instruction main/4
 +run: ingredient 0 is 1
@@ -32,13 +20,35 @@ recipe main [
 +mem: storing 15 in location 7
 +mem: storing 16 in location 8
 
+//: disable the size mismatch check since the destination array need not be initialized
+:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<int> data)")
+if (x.types[0] != Type_number["array"] && size_of(x) != data.size())
 :(after "size_t size_of(const reagent& r)")
-  const type_info& t = Type[r.types[0]];
-  if (t.is_array) {
-    int base = r.value;
-    return 1 + Memory[base]*size_of(t.element);
+  static const int ARRAY = Type_number["array"];
+  if (r.types[0] == ARRAY) {
+    assert(r.types.size() > 1);
+    // skip the 'array' type to get at the element type
+    return 1 + Memory[r.value]*size_of(array_element(r.types));
   }
 
+//: array elements are accessed using 'index'
+:(scenario "index")
+recipe main [
+  1:integer <- copy 3:literal
+  2:integer <- copy 14:literal
+  3:integer <- copy 15:literal
+  4:integer <- copy 16:literal
+  5:integer <- index 1:array:integer, 0:literal
+]
++run: instruction main/4
++run: ingredient 0 is 1
++run: ingredient 1 is 0
++run: address to copy is 2
++run: its type is 1
++mem: location 2 is 14
++run: product 0 is 14
++mem: storing 14 in location 5
+
 :(before "End Globals")
 // Operator to look at elements of arrays.
 const int INDEX = 20;
@@ -48,42 +58,44 @@ assert(Next_recipe_number == INDEX);
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case INDEX: {
+  static const int ARRAY = Type_number["array"];
   trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
   reagent base = canonize(instructions[pc].ingredients[0]);
   int base_address = base.value;
-  int base_type = base.types[0];
-  assert(Type[base_type].is_array);
+  assert(base.types[0] == ARRAY);
   trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
   size_t offset = instructions[pc].ingredients[1].value;
-  int src = base_address + 1 + offset*size_of(Type[base_type].element);
+  vector<type_number> element_type = array_element(base.types);
+  int src = base_address + 1 + offset*size_of(element_type);
   trace("run") << "address to copy is " << src;
-  int src_type = Type[base_type].element[0];
-  trace("run") << "its type is " << src_type;
+  trace("run") << "its type is " << element_type[0];
   reagent tmp;
   tmp.set_value(src);
-  tmp.types.push_back(src_type);
+  copy(element_type.begin(), element_type.end(), inserter(tmp.types, tmp.types.begin()));
   vector<int> result(read_memory(tmp));
   trace("run") << "product 0 is " << result[0];
   write_memory(instructions[pc].products[0], result);
   break;
 }
 
-:(scenario "index")
+:(code)
+vector<type_number> array_element(const vector<type_number>& types) {
+  return vector<type_number>(++types.begin(), types.end());
+}
+
+:(scenario "index_address")
 recipe main [
   1:integer <- copy 3:literal
   2:integer <- copy 14:literal
   3:integer <- copy 15:literal
   4:integer <- copy 16:literal
-  5:integer <- index 1:integer-array, 0:literal
+  5:integer <- index-address 1:array:integer, 0:literal
 ]
 +run: instruction main/4
 +run: ingredient 0 is 1
 +run: ingredient 1 is 0
 +run: address to copy is 2
-+run: its type is 1
-+mem: location 2 is 14
-+run: product 0 is 14
-+mem: storing 14 in location 5
++mem: storing 2 in location 5
 
 :(before "End Globals")
 // To write to fields of records, you need their address.
@@ -94,14 +106,15 @@ assert(Next_recipe_number == INDEX_ADDRESS);
 Next_recipe_number++;
 :(before "End Primitive Recipe Implementations")
 case INDEX_ADDRESS: {
+  static const int ARRAY = Type_number["array"];
   trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
   reagent base = canonize(instructions[pc].ingredients[0]);
   int base_address = base.value;
-  int base_type = base.types[0];
-  assert(Type[base_type].is_array);
+  assert(base.types[0] == ARRAY);
   trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
   size_t offset = instructions[pc].ingredients[1].value;
-  int src = base_address + 1 + offset*size_of(Type[base_type].element);
+  vector<type_number> element_type = array_element(base.types);
+  int src = base_address + 1 + offset*size_of(element_type);
   trace("run") << "address to copy is " << src;
   vector<int> result;
   result.push_back(src);
@@ -109,17 +122,3 @@ case INDEX_ADDRESS: {
   write_memory(instructions[pc].products[0], result);
   break;
 }
-
-:(scenario "index_address")
-recipe main [
-  1:integer <- copy 3:literal
-  2:integer <- copy 14:literal
-  3:integer <- copy 15:literal
-  4:integer <- copy 16:literal
-  5:integer <- index-address 1:integer-array, 0:literal
-]
-+run: instruction main/4
-+run: ingredient 0 is 1
-+run: ingredient 1 is 0
-+run: address to copy is 2
-+mem: storing 2 in location 5