2015-10-30 20:00:16 +00:00
|
|
|
//:: Container definitions can contain type parameters.
|
|
|
|
//:
|
2015-11-11 05:49:05 +00:00
|
|
|
//: Extremely hacky initial implementation. We still don't support the full
|
|
|
|
//: complexity of type trees inside container definitions. So for example you
|
|
|
|
//: can't have a container element with this type:
|
2015-10-30 20:00:16 +00:00
|
|
|
//: (map (array address character) (list number))
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario size_of_shape_shifting_container)
|
2015-10-30 20:00:16 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:foo:number <- merge 12, 13
|
|
|
|
3:foo:point <- merge 14, 15, 16
|
|
|
|
]
|
|
|
|
+mem: storing 12 in location 1
|
|
|
|
+mem: storing 13 in location 2
|
|
|
|
+mem: storing 14 in location 3
|
|
|
|
+mem: storing 15 in location 4
|
|
|
|
+mem: storing 16 in location 5
|
|
|
|
|
|
|
|
:(before "End Globals")
|
|
|
|
// We'll use large type ordinals to mean "the following type of the variable".
|
|
|
|
const int START_TYPE_INGREDIENTS = 2000;
|
|
|
|
:(before "End Test Run Initialization")
|
|
|
|
assert(Next_type_ordinal < START_TYPE_INGREDIENTS);
|
|
|
|
|
|
|
|
:(before "End type_info Fields")
|
|
|
|
map<string, type_ordinal> type_ingredient_names;
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
//: Suppress unknown type checks in shape-shifting containers.
|
2015-11-05 07:44:46 +00:00
|
|
|
|
|
|
|
:(before "Check Container Field Types(info)")
|
|
|
|
if (!info.type_ingredient_names.empty()) continue;
|
|
|
|
|
2015-10-30 20:00:16 +00:00
|
|
|
:(before "End container Name Refinements")
|
|
|
|
if (name.find(':') != string::npos) {
|
|
|
|
trace(9999, "parse") << "container has type ingredients; parsing" << end();
|
|
|
|
read_type_ingredients(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
:(code)
|
|
|
|
void read_type_ingredients(string& name) {
|
|
|
|
string save_name = name;
|
|
|
|
istringstream in(save_name);
|
|
|
|
name = slurp_until(in, ':');
|
2015-11-06 21:22:16 +00:00
|
|
|
if (!contains_key(Type_ordinal, name) || get(Type_ordinal, name) == 0)
|
2015-11-06 19:06:58 +00:00
|
|
|
put(Type_ordinal, name, Next_type_ordinal++);
|
2015-11-06 19:31:37 +00:00
|
|
|
type_info& info = get_or_insert(Type, get(Type_ordinal, name));
|
2015-10-30 20:00:16 +00:00
|
|
|
long long int next_type_ordinal = START_TYPE_INGREDIENTS;
|
2015-11-17 09:21:00 +00:00
|
|
|
while (has_data(in)) {
|
2015-10-30 20:00:16 +00:00
|
|
|
string curr = slurp_until(in, ':');
|
|
|
|
if (info.type_ingredient_names.find(curr) != info.type_ingredient_names.end()) {
|
|
|
|
raise_error << "can't repeat type ingredient names in a single container definition\n" << end();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-08 06:56:06 +00:00
|
|
|
put(info.type_ingredient_names, curr, next_type_ordinal++);
|
2015-10-30 20:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:47:10 +00:00
|
|
|
:(before "End insert_container Special-cases")
|
2015-10-30 20:00:16 +00:00
|
|
|
// check for use of type ingredients
|
2016-02-07 00:47:10 +00:00
|
|
|
else if (!properties->value.empty() && properties->value.at(0) == '_') {
|
|
|
|
result->value = get(info.type_ingredient_names, properties->value);
|
2015-10-30 20:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Container Type Checks")
|
|
|
|
if (type->value >= START_TYPE_INGREDIENTS
|
2015-11-06 19:06:58 +00:00
|
|
|
&& (type->value - START_TYPE_INGREDIENTS) < SIZE(get(Type, type->value).type_ingredient_names))
|
2015-10-30 20:00:16 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
:(before "End size_of(type) Container Cases")
|
|
|
|
if (t.elements.at(i)->value >= START_TYPE_INGREDIENTS) {
|
2015-11-02 04:58:36 +00:00
|
|
|
trace(9999, "type") << "checking size of type ingredient\n" << end();
|
2015-11-04 21:14:41 +00:00
|
|
|
long long int size = size_of_type_ingredient(t.elements.at(i), type->right);
|
2015-11-08 22:46:48 +00:00
|
|
|
if (!size)
|
|
|
|
raise_error << "illegal type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
|
2015-11-08 06:56:06 +00:00
|
|
|
result += size;
|
2015-10-30 20:00:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-15 07:15:03 +00:00
|
|
|
:(scenario size_of_shape_shifting_exclusive_container)
|
|
|
|
exclusive-container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:foo:number <- merge 0/x, 34
|
|
|
|
3:foo:point <- merge 0/x, 15, 16
|
|
|
|
6:foo:point <- merge 1/y, 23
|
|
|
|
]
|
|
|
|
+mem: storing 0 in location 1
|
|
|
|
+mem: storing 34 in location 2
|
|
|
|
+mem: storing 0 in location 3
|
|
|
|
+mem: storing 15 in location 4
|
|
|
|
+mem: storing 16 in location 5
|
|
|
|
+mem: storing 1 in location 6
|
|
|
|
+mem: storing 23 in location 7
|
|
|
|
$mem: 7
|
|
|
|
|
|
|
|
:(before "End size_of(type) Exclusive Container Cases")
|
|
|
|
if (t.elements.at(i)->value >= START_TYPE_INGREDIENTS) {
|
|
|
|
trace(9999, "type") << "checking size of type ingredient\n" << end();
|
|
|
|
long long int size = size_of_type_ingredient(t.elements.at(i), type->right);
|
|
|
|
if (!size)
|
|
|
|
raise_error << "illegal type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
|
|
|
|
if (size > result) result = size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-30 20:00:16 +00:00
|
|
|
:(code)
|
2015-11-11 04:24:03 +00:00
|
|
|
// shape-shifting version of size_of
|
2015-10-31 04:12:54 +00:00
|
|
|
long long int size_of_type_ingredient(const type_tree* element_template, const type_tree* rest_of_use) {
|
|
|
|
long long int type_ingredient_index = element_template->value - START_TYPE_INGREDIENTS;
|
|
|
|
const type_tree* curr = rest_of_use;
|
2015-11-04 21:14:41 +00:00
|
|
|
if (!curr) return 0;
|
2015-10-30 20:00:16 +00:00
|
|
|
while (type_ingredient_index > 0) {
|
|
|
|
--type_ingredient_index;
|
|
|
|
curr = curr->right;
|
2015-11-04 21:14:41 +00:00
|
|
|
if (!curr) return 0;
|
2015-10-30 20:00:16 +00:00
|
|
|
}
|
|
|
|
assert(curr);
|
|
|
|
assert(!curr->left); // unimplemented
|
2015-11-17 19:28:19 +00:00
|
|
|
if (!contains_key(Type, curr->value)) return 0;
|
2015-11-06 19:06:58 +00:00
|
|
|
trace(9999, "type") << "type deduced to be " << get(Type, curr->value).name << "$" << end();
|
2015-10-30 20:00:16 +00:00
|
|
|
type_tree tmp(curr->value);
|
|
|
|
if (curr->right)
|
|
|
|
tmp.right = new type_tree(*curr->right);
|
|
|
|
return size_of(&tmp);
|
|
|
|
}
|
2015-10-31 04:12:54 +00:00
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_on_shape_shifting_container)
|
2015-10-31 04:12:54 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:foo:point <- merge 14, 15, 16
|
2015-11-02 03:38:30 +00:00
|
|
|
2:number <- get 1:foo:point, y:offset
|
2015-10-31 04:12:54 +00:00
|
|
|
]
|
|
|
|
+mem: storing 16 in location 2
|
|
|
|
|
|
|
|
:(before "End GET field Cases")
|
2015-11-08 22:46:48 +00:00
|
|
|
const type_tree* type = get(Type, base_type).elements.at(i);
|
|
|
|
if (type->value >= START_TYPE_INGREDIENTS) {
|
|
|
|
long long int size = size_of_type_ingredient(type, base.type->right);
|
|
|
|
if (!size)
|
|
|
|
raise_error << "illegal field type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
|
|
|
|
src += size;
|
2015-10-31 04:12:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_on_shape_shifting_container_2)
|
2015-11-02 03:38:30 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:foo:point <- merge 14, 15, 16
|
|
|
|
2:point <- get 1:foo:point, x:offset
|
|
|
|
]
|
|
|
|
+mem: storing 14 in location 2
|
|
|
|
+mem: storing 15 in location 3
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_on_shape_shifting_container_3)
|
2015-11-09 22:48:46 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:foo:address:point <- merge 34, 48 # unsafe
|
|
|
|
2:address:point <- get 1:foo:address:point, x:offset
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 2
|
|
|
|
|
2015-11-02 03:38:30 +00:00
|
|
|
:(before "End element_type Special-cases")
|
|
|
|
if (contains_type_ingredient(element)) {
|
2015-11-08 22:46:48 +00:00
|
|
|
if (!canonized_base.type->right)
|
|
|
|
raise_error << "illegal type '" << debug_string(canonized_base.type) << "' seems to be missing a type ingredient or three\n" << end();
|
2015-11-08 23:19:01 +00:00
|
|
|
replace_type_ingredient(element.type, canonized_base.type->right);
|
2015-11-02 03:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:(code)
|
|
|
|
bool contains_type_ingredient(const reagent& x) {
|
|
|
|
return contains_type_ingredient(x.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains_type_ingredient(const type_tree* type) {
|
|
|
|
if (!type) return false;
|
|
|
|
if (type->value >= START_TYPE_INGREDIENTS) return true;
|
|
|
|
return contains_type_ingredient(type->left) || contains_type_ingredient(type->right);
|
|
|
|
}
|
|
|
|
|
2015-11-08 23:19:01 +00:00
|
|
|
void replace_type_ingredient(type_tree* element_type, const type_tree* callsite_type) {
|
2015-11-04 21:14:41 +00:00
|
|
|
if (!callsite_type) return; // error but it's already been raised above
|
2015-11-02 03:38:30 +00:00
|
|
|
if (!element_type) return;
|
|
|
|
if (element_type->value >= START_TYPE_INGREDIENTS) {
|
2015-11-04 21:14:41 +00:00
|
|
|
if (!has_nth_type(callsite_type, element_type->value-START_TYPE_INGREDIENTS)) {
|
2015-11-08 22:46:48 +00:00
|
|
|
raise_error << "illegal type '" << debug_string(callsite_type) << "' seems to be missing a type ingredient or three\n" << end();
|
2015-11-04 21:14:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-08 23:19:01 +00:00
|
|
|
const type_tree* replacement = nth_type(callsite_type, element_type->value-START_TYPE_INGREDIENTS);
|
|
|
|
element_type->value = replacement->value;
|
2016-02-15 03:48:10 +00:00
|
|
|
assert(!element_type->left); // since value is set
|
2015-11-08 23:19:01 +00:00
|
|
|
element_type->left = replacement->left ? new type_tree(*replacement->left) : NULL;
|
2016-02-15 03:48:10 +00:00
|
|
|
assert(!element_type->right); // unsupported
|
2015-11-08 23:19:01 +00:00
|
|
|
element_type->right = replacement->right ? new type_tree(*replacement->right) : NULL;
|
2015-11-02 03:38:30 +00:00
|
|
|
}
|
2015-11-08 23:19:01 +00:00
|
|
|
replace_type_ingredient(element_type->right, callsite_type);
|
2015-11-02 03:38:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 23:19:01 +00:00
|
|
|
const type_tree* nth_type(const type_tree* base, long long int n) {
|
2015-11-02 03:38:30 +00:00
|
|
|
assert(n >= 0);
|
2015-11-08 23:19:01 +00:00
|
|
|
if (n == 0) return base;
|
2015-11-02 03:38:30 +00:00
|
|
|
return nth_type(base->right, n-1);
|
|
|
|
}
|
|
|
|
|
2015-11-08 23:19:01 +00:00
|
|
|
bool has_nth_type(const type_tree* base, long long int n) {
|
2015-11-04 21:14:41 +00:00
|
|
|
assert(n >= 0);
|
|
|
|
if (base == NULL) return false;
|
|
|
|
if (n == 0) return true;
|
|
|
|
return has_nth_type(base->right, n-1);
|
|
|
|
}
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_on_shape_shifting_container_error)
|
2015-11-04 21:14:41 +00:00
|
|
|
% Hide_errors = true;
|
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
10:foo:point <- merge 14, 15, 16
|
|
|
|
1:number <- get 10:foo, 1:offset
|
|
|
|
]
|
|
|
|
+error: illegal type 'foo' seems to be missing a type ingredient or three
|
|
|
|
|
|
|
|
//: get-address similarly
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_address_on_shape_shifting_container)
|
2015-10-31 04:12:54 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
10:foo:point <- merge 14, 15, 16
|
|
|
|
1:address:number <- get-address 10:foo:point, 1:offset
|
|
|
|
]
|
|
|
|
+mem: storing 12 in location 1
|
|
|
|
|
|
|
|
:(before "End GET_ADDRESS field Cases")
|
2015-11-08 22:46:48 +00:00
|
|
|
const type_tree* type = get(Type, base_type).elements.at(i);
|
|
|
|
if (type->value >= START_TYPE_INGREDIENTS) {
|
|
|
|
long long int size = size_of_type_ingredient(type, base.type->right);
|
|
|
|
if (!size)
|
|
|
|
raise_error << "illegal type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
|
|
|
|
result += size;
|
2015-10-31 04:12:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-11 04:24:03 +00:00
|
|
|
:(scenario get_on_shape_shifting_container_inside_shape_shifting_container)
|
2015-10-31 04:12:54 +00:00
|
|
|
container foo:_t [
|
|
|
|
x:_t
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
container bar [
|
|
|
|
x:foo:point
|
|
|
|
y:number
|
|
|
|
]
|
|
|
|
recipe main [
|
|
|
|
1:bar <- merge 14, 15, 16, 17
|
|
|
|
2:number <- get 1:bar, 1:offset
|
|
|
|
]
|
|
|
|
+mem: storing 17 in location 2
|