:(before "End Mu Types Initialization")
{
type_ordinal tmp = put(Type_ordinal, "number-or-point", Next_type_ordinal++);
get_or_insert(Type, tmp);
get(Type, tmp).kind = EXCLUSIVE_CONTAINER;
get(Type, tmp).name = "number-or-point";
get(Type, tmp).elements.push_back(reagent("i:number"));
get(Type, tmp).elements.push_back(reagent("p:point"));
}
:(scenario copy_exclusive_container)
def main [
1:number <- copy 1
2:number <- copy 34
3:number <- copy 35
4:number-or-point <- copy 1:number-or-point/unsafe
]
+mem: storing 1 in location 4
+mem: storing 34 in location 5
+mem: storing 35 in location 6
:(before "End size_of(type) Cases")
if (t.kind == EXCLUSIVE_CONTAINER) {
return get(Container_metadata, type).size;
}
:(before "End compute_container_sizes Cases")
if (info.kind == EXCLUSIVE_CONTAINER) {
container_metadata metadata;
int size = 0;
for (int i = 0; i < SIZE(info.elements); ++i) {
reagent element = info.elements.at(i);
compute_container_sizes(element.type, pending_metadata);
int variant_size = size_of(element);
if (variant_size > size) size = variant_size;
}
metadata.size = size+1;
Container_metadata.push_back(pair<type_tree*, container_metadata>(new type_tree(*type), metadata));
}
:(before "End Mu Types Initialization")
put(Type_ordinal, "variant", 0);
:(scenario maybe_convert)
def main [
12:number <- copy 1
13:number <- copy 35
14:number <- copy 36
20:point, 22:boolean <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
+mem: storing 1 in location 22
+mem: storing 35 in location 20
+mem: storing 36 in location 21
:(scenario maybe_convert_fail)
def main [
12:number <- copy 1
13:number <- copy 35
14:number <- copy 36
20:number, 21:boolean <- maybe-convert 12:number-or-point/unsafe, 0:variant
]
+mem: storing 0 in location 21
:(before "End Primitive Recipe Declarations")
MAYBE_CONVERT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "maybe-convert", MAYBE_CONVERT);
:(before "End Primitive Recipe Checks")
case MAYBE_CONVERT: {
const recipe& caller = get(Recipe, r);
if (SIZE(inst.ingredients) != 2) {
raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
break;
}
reagent base = inst.ingredients.at(0);
if (!base.type || !base.type->value || get(Type, base.type->value).kind != EXCLUSIVE_CONTAINER) {
raise << maybe(caller.name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got '" << base.original_string << "'\n" << end();
break;
}
if (!is_literal(inst.ingredients.at(1))) {
raise << maybe(caller.name) << "second ingredient of 'maybe-convert' should have type 'variant', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
break;
}
if (inst.products.empty()) break;
if (SIZE(inst.products) != 2) {
raise << maybe(caller.name) << "'maybe-convert' expects exactly 2 products in '" << to_original_string(inst) << "'\n" << end();
break;
}
reagent product = inst.products.at(0);
reagent& offset = inst.ingredients.at(1);
populate_value(offset);
if (offset.value >= SIZE(get(Type, base.type->value).elements)) {
raise << maybe(caller.name) << "invalid tag " << offset.value << " in '" << to_original_string(inst) << '\n' << end();
break;
}
const reagent& variant = variant_type(base, offset.value);
if (!types_coercible(product, variant)) {
raise << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << to_string(variant.type) << " but '" << product.name << "' has type " << to_string(product.type) << '\n' << end();
break;
}
reagent status = inst.products.at(1);
if (!is_mu_boolean(status)) {
raise << maybe(get(Recipe, r).name) << "second product yielded by 'maybe-convert' should be a boolean, but tried to write to '" << inst.products.at(1).original_string << "'\n" << end();
break;
}
break;
}
:(before "End Primitive Recipe Implementations")
case MAYBE_CONVERT: {
reagent base = current_instruction().ingredients.at(0);
int base_address = base.value;
if (base_address == 0) {
raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
break;
}
int tag = current_instruction().ingredients.at(1).value;
reagent product = current_instruction().products.at(0);
reagent status = current_instruction().products.at(1);
if (tag == static_cast<int>(get_or_insert(Memory, base_address))) {
const reagent& variant = variant_type(base, tag);
trace(9999, "mem") << "storing 1 in location " << status.value << end();
put(Memory, status.value, 1);
for (int i = 0; i < size_of(variant); ++i) {
double val = get_or_insert(Memory, base_address+1+i);
trace(9999, "mem") << "storing " << no_scientific(val) << " in location " << product.value+i << end();
put(Memory, product.value+i, val);
}
}
else {
trace(9999, "mem") << "storing 0 in location " << status.value << end();
put(Memory, status.value, 0);
}
goto finish_instruction;
}
:(code)
const reagent variant_type(const reagent& base, int tag) {
return variant_type(base.type, tag);
}
const reagent variant_type(const type_tree* type, int tag) {
assert(tag >= 0);
assert(contains_key(Type, type->value));
assert(!get(Type, type->value).name.empty());
const type_info& info = get(Type, type->value);
assert(info.kind == EXCLUSIVE_CONTAINER);
reagent element = info.elements.at(tag);
return element;
}
:(scenario maybe_convert_product_type_mismatch)
% Hide_errors = true;
def main [
12:number <- copy 1
13:number <- copy 35
14:number <- copy 36
20:number, 21:boolean <- maybe-convert 12:number-or-point/unsafe, 1:variant
]
+error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to point but '20' has type number
:(scenario exclusive_container)
exclusive-container foo [
x:number
y:number
]
+parse: --- defining exclusive-container foo
+parse: element: {x: "number"}
+parse: element: {y: "number"}
:(before "End Command Handlers")
else if (command == "exclusive-container") {
insert_container(command, EXCLUSIVE_CONTAINER, in);
}
:(scenario exclusive_container_contains_array)
exclusive-container foo [
x:array:number:3
]
$error: 0
:(scenario exclusive_container_disallows_dynamic_array_element)
% Hide_errors = true;
exclusive-container foo [
x:array:number
]
+error: container 'foo' cannot determine size of element 'x'
:(scenario lift_to_exclusive_container)
exclusive-container foo [
x:number
y:number
]
def main [
1:number <- copy 34
2:foo <- merge 0/x, 1:number
4:foo <- merge 1/y, 1:number
]
+mem: storing 0 in location 2
+mem: storing 34 in location 3
+mem: storing 1 in location 4
+mem: storing 34 in location 5
:(scenario merge_handles_exclusive_container)
exclusive-container foo [
x:number
y:bar
]
container bar [
z:number
]
def main [
1:foo <- merge 0/x, 34
]
+mem: storing 0 in location 1
+mem: storing 34 in location 2
$error: 0
:(scenario merge_requires_literal_tag_for_exclusive_container)
% Hide_errors = true;
exclusive-container foo [
x:number
y:bar
]
container bar [
z:number
]
def main [
local-scope
1:number <- copy 0
2:foo <- merge 1:number, 34
]
+error: main: ingredient 0 of 'merge' should be a literal, for the tag of exclusive-container 'foo'
:(before "End valid_merge Cases")
case EXCLUSIVE_CONTAINER: {
assert(state.data.top().container_element_index == 0);
trace(9999, "transform") << "checking exclusive container " << to_string(container) << " vs ingredient " << ingredient_index << end();
if (!is_literal(ingredients.at(ingredient_index))) {
raise << maybe(caller.name) << "ingredient " << ingredient_index << " of 'merge' should be a literal, for the tag of exclusive-container '" << container_info.name << "'\n" << end();
return;
}
reagent ingredient = ingredients.at(ingredient_index);
populate_value(ingredient);
if (ingredient.value >= SIZE(container_info.elements)) {
raise << maybe(caller.name) << "invalid tag at " << ingredient_index << " for '" << container_info.name << "' in '" << to_original_string(inst) << "'\n" << end();
return;
}
const reagent& variant = variant_type(container, ingredient.value);
trace(9999, "transform") << "tag: " << ingredient.value << end();
state.data.pop();
state.data.push(merge_check_point(variant, 0));
++ingredient_index;
break;
}
:(scenario merge_check_container_containing_exclusive_container)
container foo [
x:number
y:bar
]
exclusive-container bar [
x:number
y:number
]
def main [
1:foo <- merge 23, 1/y, 34
]
+mem: storing 23 in location 1
+mem: storing 1 in location 2
+mem: storing 34 in location 3
$error: 0
:(scenario merge_check_container_containing_exclusive_container_2)
% Hide_errors = true;
container foo [
x:number
y:bar
]
exclusive-container bar [
x:number
y:number
]
def main [
1:foo <- merge 23, 1/y, 34, 35
]
+error: main: too many ingredients in '1:foo <- merge 23, 1/y, 34, 35'
:(scenario merge_check_exclusive_container_containing_container)
exclusive-container foo [
x:number
y:bar
]
container bar [
x:number
y:number
]
def main [
1:foo <- merge 1/y, 23, 34
]
+mem: storing 1 in location 1
+mem: storing 23 in location 2
+mem: storing 34 in location 3
$error: 0
:(scenario merge_check_exclusive_container_containing_container_2)
exclusive-container foo [
x:number
y:bar
]
container bar [
x:number
y:number
]
def main [
1:foo <- merge 0/x, 23
]
$error: 0
:(scenario merge_check_exclusive_container_containing_container_3)
% Hide_errors = true;
exclusive-container foo [
x:number
y:bar
]
container bar [
x:number
y:number
]
def main [
1:foo <- merge 1/y, 23
]
+error: main: too few ingredients in '1:foo <- merge 1/y, 23'
:(scenario merge_check_exclusive_container_containing_container_4)
exclusive-container foo [
x:number
y:bar
]
container bar [
a:number
b:number
]
def main [
1:bar <- merge 23, 24
3:foo <- merge 1/y, 1:bar
]
$error: 0
:(before "End size_mismatch(x) Cases")
if (current_step_index() < SIZE(Current_routine->steps())
&& current_instruction().operation == MERGE
&& !current_instruction().products.empty()
&& current_instruction().products.at(0).type) {
reagent x = current_instruction().products.at(0);
if (get(Type, x.type->value).kind == EXCLUSIVE_CONTAINER)
return size_of(x) < SIZE(data);
}
:(scenario merge_exclusive_container_with_mismatched_sizes)
container foo [
x:number
y:number
]
exclusive-container bar [
x:number
y:foo
]
def main [
1:number <- copy 34
2:number <- copy 35
3:bar <- merge 0/x, 1:number
6:bar <- merge 1/foo, 1:number, 2:number
]
+mem: storing 0 in location 3
+mem: storing 34 in location 4
+mem: storing 1 in location 6
+mem: storing 34 in location 7
+mem: storing 35 in location 8