From d009e158803956c76adbf8f58a62884c3e7affb3 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 26 Jan 2016 23:47:23 -0800 Subject: 2605 --- html/033exclusive_container.cc.html | 54 ++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'html/033exclusive_container.cc.html') diff --git a/html/033exclusive_container.cc.html b/html/033exclusive_container.cc.html index a4fc191a..f3e258c8 100644 --- a/html/033exclusive_container.cc.html +++ b/html/033exclusive_container.cc.html @@ -60,7 +60,7 @@ recipe main [ 1:number <- copy 1 # 'point' variant 2:number <- copy 34 3:number <- copy 35 - 4:number-or-point <- copy 1:number-or-point/raw # unsafe + 4:number-or-point <- copy 1:number-or-point/unsafe ] +mem: storing 1 in location 4 +mem: storing 34 in location 5 @@ -94,7 +94,7 @@ recipe main [ 12:number <- copy 1 13:number <- copy 35 14:number <- copy 36 - 20:address:point <- maybe-convert 12:number-or-point/raw, 1:variant # unsafe + 20:address:point <- maybe-convert 12:number-or-point/unsafe, 1:variant ] +mem: storing 13 in location 20 @@ -103,7 +103,7 @@ recipe main [ 12:number <- copy 1 13:number <- copy 35 14:number <- copy 36 - 20:address:point <- maybe-convert 12:number-or-point/raw, 0:variant # unsafe + 20:address:number <- maybe-convert 12:number-or-point/unsafe, 0:variant ] +mem: storing 0 in location 20 @@ -113,18 +113,36 @@ MAYBE_CONVERT, 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_error << maybe(get(Recipe, r).name) << "'maybe-convert' expects exactly 2 ingredients in '" << inst.to_string() << "'\n" << end(); + raise_error << maybe(caller.name) << "'maybe-convert' expects exactly 2 ingredients in '" << inst.to_string() << "'\n" << end(); break; } reagent base = inst.ingredients.at(0); canonize_type(base); if (!base.type || !base.type->value || get(Type, base.type->value).kind != EXCLUSIVE_CONTAINER) { - raise_error << maybe(get(Recipe, r).name) << "first ingredient of 'maybe-convert' should be an exclusive-container, but got " << base.original_string << '\n' << end(); + raise_error << 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_error << maybe(get(Recipe, r).name) << "second ingredient of 'maybe-convert' should have type 'variant', but got " << inst.ingredients.at(1).original_string << '\n' << end(); + raise_error << 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; + reagent product = inst.products.at(0); + if (!canonize_type(product)) break; + const reagent& offset = inst.ingredients.at(1); + long long int tag = 0; + if (is_integer(offset.name)) { + tag = to_integer(offset.name); + } + else { + tag = offset.value; + } + reagent variant = variant_type(base, tag); + variant.type = new type_tree(get(Type_ordinal, "address"), variant.type); + if (!types_coercible(product, variant)) { + raise_error << maybe(caller.name) << "'maybe-convert " << base.original_string << ", " << inst.ingredients.at(1).original_string << "' should write to " << debug_string(variant.type) << " but " << product.name << " has type " << debug_string(product.type) << '\n' << end(); break; } break; @@ -151,6 +169,30 @@ case MAYBE_CONVERT: { break; } +:(code) +const reagent variant_type(const reagent& canonized_base, long long int tag) { + assert(tag >= 0); + assert(contains_key(Type, canonized_base.type->value)); + assert(!get(Type, canonized_base.type->value).name.empty()); + const type_info& info = get(Type, canonized_base.type->value); + assert(info.kind == EXCLUSIVE_CONTAINER); + reagent element; + element.type = new type_tree(*info.elements.at(tag)); + // End variant_type Special-cases + return element; +} + +:(scenario maybe_convert_product_type_mismatch) +% Hide_errors = true; +recipe main [ + 12:number <- copy 1 + 13:number <- copy 35 + 14:number <- copy 36 + 20:address:number <- maybe-convert 12:number-or-point/unsafe, 1:variant +] ++error: main: 'maybe-convert 12:number-or-point/unsafe, 1:variant' should write to <address : <point : <>>> but 20 has type <address : <number : <>>> + + //:: Allow exclusive containers to be defined in mu code. :(scenario exclusive_container) -- cgit 1.4.1-2-gfad0