diff options
-rw-r--r-- | cpp/.traces/convert_names_handles_records | 2 | ||||
-rw-r--r-- | cpp/.traces/convert_names_transforms_record_elements | 4 | ||||
-rw-r--r-- | cpp/.traces/copy_array | 4 | ||||
-rw-r--r-- | cpp/.traces/copy_handles_nested_record_elements | 4 | ||||
-rw-r--r-- | cpp/.traces/copy_multiple_locations | 4 | ||||
-rw-r--r-- | cpp/.traces/get | 2 | ||||
-rw-r--r-- | cpp/.traces/get_address | 2 | ||||
-rw-r--r-- | cpp/.traces/get_address_indirect | 2 | ||||
-rw-r--r-- | cpp/.traces/get_handles_nested_record_elements | 2 | ||||
-rw-r--r-- | cpp/.traces/get_indirect | 2 | ||||
-rw-r--r-- | cpp/.traces/include_nonderef_properties | 2 | ||||
-rw-r--r-- | cpp/.traces/index | 2 | ||||
-rw-r--r-- | cpp/.traces/index_address | 2 | ||||
-rw-r--r-- | cpp/.traces/set_default_space | 4 | ||||
-rw-r--r-- | cpp/010vm | 6 | ||||
-rw-r--r-- | cpp/013run | 3 | ||||
-rw-r--r-- | cpp/018record | 6 | ||||
-rw-r--r-- | cpp/020array | 97 |
18 files changed, 74 insertions, 76 deletions
diff --git a/cpp/.traces/convert_names_handles_records b/cpp/.traces/convert_names_handles_records index 0b2464e9..b758b1e4 100644 --- a/cpp/.traces/convert_names_handles_records +++ b/cpp/.traces/convert_names_handles_records @@ -1,6 +1,6 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]} -parse/0: product: {name: "a", value: 0, type: 4, properties: [a: point]} +parse/0: product: {name: "a", value: 0, type: 5, properties: [a: point]} parse/0: instruction: 1 parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]} parse/0: product: {name: "b", value: 0, type: 1, properties: [b: integer]} diff --git a/cpp/.traces/convert_names_transforms_record_elements b/cpp/.traces/convert_names_transforms_record_elements index 6e31dacf..a563eecc 100644 --- a/cpp/.traces/convert_names_transforms_record_elements +++ b/cpp/.traces/convert_names_transforms_record_elements @@ -1,9 +1,9 @@ parse/0: instruction: 18 -parse/0: ingredient: {name: "0", value: 0, type: 4, properties: [0: point]} +parse/0: ingredient: {name: "0", value: 0, type: 5, properties: [0: point]} parse/0: ingredient: {name: "y", value: 0, type: 0, properties: [y: offset]} parse/0: product: {name: "a", value: 0, type: 1, properties: [a: integer]} parse/0: instruction: 18 -parse/0: ingredient: {name: "0", value: 0, type: 4, properties: [0: point]} +parse/0: ingredient: {name: "0", value: 0, type: 5, properties: [0: point]} parse/0: ingredient: {name: "x", value: 0, type: 0, properties: [x: offset]} parse/0: product: {name: "b", value: 0, type: 1, properties: [b: integer]} name/0: field y of type point is at offset 1 diff --git a/cpp/.traces/copy_array b/cpp/.traces/copy_array index 8b49bc79..3b1af366 100644 --- a/cpp/.traces/copy_array +++ b/cpp/.traces/copy_array @@ -11,8 +11,8 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} parse/0: instruction: 1 -parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]} -parse/0: product: {name: "5", value: 0, type: 6, properties: [5: integer-array]} +parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]} +parse/0: product: {name: "5", value: 0, type: 4-1, properties: [5: array:integer]} after-brace/0: recipe main after-brace/0: copy ... after-brace/0: copy ... diff --git a/cpp/.traces/copy_handles_nested_record_elements b/cpp/.traces/copy_handles_nested_record_elements index 3edfa398..e00667b7 100644 --- a/cpp/.traces/copy_handles_nested_record_elements +++ b/cpp/.traces/copy_handles_nested_record_elements @@ -8,8 +8,8 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "36", value: 0, type: 0, properties: [36: literal]} parse/0: product: {name: "14", value: 0, type: 1, properties: [14: integer]} parse/0: instruction: 1 -parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point-integer]} -parse/0: product: {name: "15", value: 0, type: 5, properties: [15: point-integer]} +parse/0: ingredient: {name: "12", value: 0, type: 6, properties: [12: point-integer]} +parse/0: product: {name: "15", value: 0, type: 6, properties: [15: point-integer]} after-brace/0: recipe main after-brace/0: copy ... after-brace/0: copy ... diff --git a/cpp/.traces/copy_multiple_locations b/cpp/.traces/copy_multiple_locations index 9a727624..5cf623e2 100644 --- a/cpp/.traces/copy_multiple_locations +++ b/cpp/.traces/copy_multiple_locations @@ -5,8 +5,8 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]} parse/0: product: {name: "2", value: 0, type: 1, properties: [2: integer]} parse/0: instruction: 1 -parse/0: ingredient: {name: "1", value: 0, type: 4, properties: [1: point]} -parse/0: product: {name: "3", value: 0, type: 4, properties: [3: point]} +parse/0: ingredient: {name: "1", value: 0, type: 5, properties: [1: point]} +parse/0: product: {name: "3", value: 0, type: 5, properties: [3: point]} after-brace/0: recipe main after-brace/0: copy ... after-brace/0: copy ... diff --git a/cpp/.traces/get b/cpp/.traces/get index 603027dc..ee6cc6a6 100644 --- a/cpp/.traces/get +++ b/cpp/.traces/get @@ -5,7 +5,7 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]} parse/0: product: {name: "13", value: 0, type: 1, properties: [13: integer]} parse/0: instruction: 18 -parse/0: ingredient: {name: "12", value: 0, type: 4, properties: [12: point]} +parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point]} parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]} parse/0: product: {name: "15", value: 0, type: 1, properties: [15: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/get_address b/cpp/.traces/get_address index 9de8754e..aaab6c0e 100644 --- a/cpp/.traces/get_address +++ b/cpp/.traces/get_address @@ -5,7 +5,7 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]} parse/0: product: {name: "13", value: 0, type: 1, properties: [13: integer]} parse/0: instruction: 19 -parse/0: ingredient: {name: "12", value: 0, type: 4, properties: [12: point]} +parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point]} parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]} parse/0: product: {name: "15", value: 0, type: 2-1, properties: [15: address:integer]} after-brace/0: recipe main diff --git a/cpp/.traces/get_address_indirect b/cpp/.traces/get_address_indirect index bc7cb2e4..ef1d9365 100644 --- a/cpp/.traces/get_address_indirect +++ b/cpp/.traces/get_address_indirect @@ -8,7 +8,7 @@ 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: 19 -parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: ]} +parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: ]} parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/get_handles_nested_record_elements b/cpp/.traces/get_handles_nested_record_elements index af104e87..eeb1558d 100644 --- a/cpp/.traces/get_handles_nested_record_elements +++ b/cpp/.traces/get_handles_nested_record_elements @@ -8,7 +8,7 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "36", value: 0, type: 0, properties: [36: literal]} parse/0: product: {name: "14", value: 0, type: 1, properties: [14: integer]} parse/0: instruction: 18 -parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point-integer]} +parse/0: ingredient: {name: "12", value: 0, type: 6, properties: [12: point-integer]} parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]} parse/0: product: {name: "15", value: 0, type: 1, properties: [15: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/get_indirect b/cpp/.traces/get_indirect index 7b319905..7624acc1 100644 --- a/cpp/.traces/get_indirect +++ b/cpp/.traces/get_indirect @@ -8,7 +8,7 @@ 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: 18 -parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: ]} +parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: ]} parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/include_nonderef_properties b/cpp/.traces/include_nonderef_properties index ee2f7593..4c0b69dd 100644 --- a/cpp/.traces/include_nonderef_properties +++ b/cpp/.traces/include_nonderef_properties @@ -8,7 +8,7 @@ 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: 18 -parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: , foo: ]} +parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: , foo: ]} parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/index b/cpp/.traces/index index 296103d2..8bb37551 100644 --- a/cpp/.traces/index +++ b/cpp/.traces/index @@ -11,7 +11,7 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} parse/0: instruction: 20 -parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]} +parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]} parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]} parse/0: product: {name: "5", value: 0, type: 1, properties: [5: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/index_address b/cpp/.traces/index_address index de36bd37..2c405958 100644 --- a/cpp/.traces/index_address +++ b/cpp/.traces/index_address @@ -11,7 +11,7 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]} parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]} parse/0: instruction: 21 -parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]} +parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]} parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]} parse/0: product: {name: "5", value: 0, type: 1, properties: [5: integer]} after-brace/0: recipe main diff --git a/cpp/.traces/set_default_space b/cpp/.traces/set_default_space index 5c9d8cad..f8d93094 100644 --- a/cpp/.traces/set_default_space +++ b/cpp/.traces/set_default_space @@ -1,9 +1,9 @@ parse/0: instruction: 1 parse/0: ingredient: {name: "10", value: 0, type: 0, properties: [10: literal]} -parse/0: product: {name: "default-space", value: 0, type: 2-0, properties: [default-space: address:space]} +parse/0: product: {name: "default-space", value: 0, type: 1-0, properties: [default-space: address:space]} parse/0: instruction: 1 parse/0: ingredient: {name: "12", value: 0, type: 0, properties: [12: literal]} -parse/0: product: {name: "1", value: 0, type: 1, properties: [1: integer]} +parse/0: product: {name: "1", value: 0, type: 3, properties: [1: integer]} name/0: assign default-space 1 after-brace/0: recipe main after-brace/0: copy ... diff --git a/cpp/010vm b/cpp/010vm index c54e5441..c3f3286d 100644 --- a/cpp/010vm +++ b/cpp/010vm @@ -86,6 +86,8 @@ void setup_types() { Type[address].name = "address"; int boolean = Type_number["boolean"] = Next_type_number++; Type[boolean].name = "boolean"; + int array = Type_number["array"] = Next_type_number++; + Type[array].name = "array"; // End Mu Types Initialization. } :(before "End Setup") @@ -101,13 +103,11 @@ void setup_types() { struct type_info { string name; bool is_record; - bool is_array; size_t size; // only if is_record; primitives and addresses have size 1 while arrays are dynamic vector<vector<type_number> > elements; // only if is_record vector<string> element_names; // only if is_record - vector<type_number> element; // only if is_array // End type_info Fields. - type_info() :is_record(false), is_array(false), size(0) {} + type_info() :is_record(false), size(0) {} }; :(before "End Globals") diff --git a/cpp/013run b/cpp/013run index 8456a7c0..e811d96d 100644 --- a/cpp/013run +++ b/cpp/013run @@ -150,8 +150,7 @@ size_t size_of(const reagent& r) { return size_of(r.types); } size_t size_of(const vector<type_number>& types) { - type_info t = Type[types[0]]; - // End size_of Cases. + // End size_of(types) Cases. return 1; } diff --git a/cpp/018record b/cpp/018record index bdde8719..dba43a91 100644 --- a/cpp/018record +++ b/cpp/018record @@ -1,5 +1,4 @@ -//: Records are compound types with multiple elements of potentially different -//: types. +//: Records contain a fixed number of elements of different types. :(before "End Mu Types Initialization") //: We'll use this record as a running example, with two integer elements int point = Type_number["point"] = Next_type_number++; @@ -47,7 +46,8 @@ recipe main [ ] +mem: storing 36 in location 17 -:(before "End size_of Cases") +:(before "End size_of(types) Cases") +type_info t = Type[types[0]]; if (t.is_record) { size_t result = 0; for (size_t i = 0; i < t.elements.size(); ++i) { 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 |