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/maybe_convert_named32
-rw-r--r--cpp/041name34
2 files changed, 63 insertions, 3 deletions
diff --git a/cpp/.traces/maybe_convert_named b/cpp/.traces/maybe_convert_named
new file mode 100644
index 00000000..eba8aa6a
--- /dev/null
+++ b/cpp/.traces/maybe_convert_named
@@ -0,0 +1,32 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "1", value: 0, type: 0, properties: ["1": "literal"]}
+parse/0:   product: {name: "12", value: 0, type: 1, properties: ["12": "integer"]}
+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: 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: 24
+parse/0:   ingredient: {name: "12", value: 0, type: 8, properties: ["12": "integer-or-point"]}
+parse/0:   ingredient: {name: "p", value: 0, type: 0, properties: ["p": "variant"]}
+parse/0:   product: {name: "20", value: 0, type: 2-6, properties: ["20": "address":"point"]}
+name/0: variant p of type integer-or-point has tag 1
+after-brace/0: recipe main
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: copy ...
+after-brace/0: maybe-convert ...
+run/0: instruction main/0
+run/0: ingredient 0 is 1
+mem/0: storing 1 in location 12
+run/0: instruction main/1
+run/0: ingredient 0 is 35
+mem/0: storing 35 in location 13
+run/0: instruction main/2
+run/0: ingredient 0 is 36
+mem/0: storing 36 in location 14
+run/0: instruction main/3
+run/0: ingredient 0 is 12
+run/0: ingredient 1 is p
+mem/0: storing 13 in location 20
diff --git a/cpp/041name b/cpp/041name
index 375f9854..c514ed3d 100644
--- a/cpp/041name
+++ b/cpp/041name
@@ -134,6 +134,8 @@ recipe main [
 ]
 -name: assign x 1
 
+//: Support element names for containers in 'get' and 'get-address'.
+
 //: update our running example container for the next test
 :(before "End Mu Types Initialization")
 Type[point].element_names.push_back("x");
@@ -156,9 +158,9 @@ if (inst.operation == Recipe_number["get"]
   assert(isa_literal(inst.ingredients[1]));
   if (inst.ingredients[1].name.find_first_not_of("0123456789") == string::npos) continue;
   // since first non-address in base type must be a container, we don't have to canonize
-  type_number container = skip_addresses(inst.ingredients[0].types);
-  inst.ingredients[1].set_value(find_element_name(container, inst.ingredients[1].name));
-  trace("name") << "element " << inst.ingredients[1].name << " of type " << Type[container].name << " is at offset " << inst.ingredients[1].value;
+  type_number base_type = skip_addresses(inst.ingredients[0].types);
+  inst.ingredients[1].set_value(find_element_name(base_type, inst.ingredients[1].name));
+  trace("name") << "element " << inst.ingredients[1].name << " of type " << Type[base_type].name << " is at offset " << inst.ingredients[1].value;
 }
 
 //: this test is actually illegal so can't call run
@@ -170,3 +172,29 @@ recipe main [
 ]
 +name: assign a 1
 +name: assign b 3
+
+//: Support variant names for exclusive containers in 'maybe-convert'.
+
+:(scenarios run)
+:(scenario "maybe_convert_named")
+recipe main [
+  12:integer <- copy 1:literal
+  13:integer <- copy 35:literal
+  14:integer <- copy 36:literal
+  20:address:point <- maybe-convert 12:integer-or-point, p:variant
+]
++name: variant p of type integer-or-point has tag 1
++mem: storing 13 in location 20
+
+:(after "Per-recipe Transforms")
+// convert variant names of exclusive containers
+if (inst.operation == Recipe_number["maybe-convert"]) {
+  // at least 2 args, and second arg is offset
+  assert(inst.ingredients.size() >= 2);
+  assert(isa_literal(inst.ingredients[1]));
+  if (inst.ingredients[1].name.find_first_not_of("0123456789") == string::npos) continue;
+  // since first non-address in base type must be an exclusive container, we don't have to canonize
+  type_number base_type = skip_addresses(inst.ingredients[0].types);
+  inst.ingredients[1].set_value(find_element_name(base_type, inst.ingredients[1].name));
+  trace("name") << "variant " << inst.ingredients[1].name << " of type " << Type[base_type].name << " has tag " << inst.ingredients[1].value;
+}