diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 18:16:08 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 18:18:32 -0700 |
commit | 69f3fb0a7a9cf653209af44ab60a1e25830bc9a7 (patch) | |
tree | 9e6650c9e3e6526be0fe11bc70dc311d1f268e54 /cpp/034exclusive_container | |
parent | 30ab02977063217dcf16028d897a3e8cb0d9cff9 (diff) | |
download | mu-69f3fb0a7a9cf653209af44ab60a1e25830bc9a7.tar.gz |
1083 - start of a sum type for mu
Diffstat (limited to 'cpp/034exclusive_container')
-rw-r--r-- | cpp/034exclusive_container | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cpp/034exclusive_container b/cpp/034exclusive_container new file mode 100644 index 00000000..2c3a603d --- /dev/null +++ b/cpp/034exclusive_container @@ -0,0 +1,54 @@ +//: Exclusive containers contain exactly one of a fixed number of 'variants' +//: of different types. +//: +//: They also implicitly contain a tag describing precisely which variant is +//: currently stored in them. + +:(before "End Mu Types Initialization") +//: We'll use this container as a running example, with two integer elements. +{ +int tmp = Type_number["integer-or-point"] = Next_type_number++; +Type[tmp].size = 2; +Type[tmp].kind = exclusive_container; +Type[tmp].name = "integer-or-point"; +//? cout << tmp << ": " << Type[tmp].elements.size() << '\n'; //? 1 +vector<type_number> t1; +t1.push_back(integer); +Type[tmp].elements.push_back(t1); +//? cout << Type[tmp].elements.size() << '\n'; //? 1 +vector<type_number> t2; +t2.push_back(point); +Type[tmp].elements.push_back(t2); +//? cout << Type[tmp].elements.size() << '\n'; //? 1 +//? cout << "point: " << point << '\n'; //? 1 +Type[tmp].element_names.push_back("i"); +Type[tmp].element_names.push_back("p"); +} + +:(scenario "copy_exclusive_container") +# Copying exclusive containers copies all their contents and an extra location for the tag. +recipe main [ + 1:integer <- copy 1:literal # 'point' variant + 2:integer <- copy 34:literal + 3:integer <- copy 35:literal + 4:integer-or-point <- copy 1:integer-or-point +] ++mem: storing 1 in location 4 ++mem: storing 34 in location 5 ++mem: storing 35 in location 6 + +:(before "End size_of(types) Cases") +if (t.kind == exclusive_container) { + // size of an exclusive container is the size of its largest variant +//? cout << "--- " << types[0] << ' ' << t.size << '\n'; //? 1 +//? cout << "point: " << Type_number["point"] << " " << Type[Type_number["point"]].name << " " << Type[Type_number["point"]].size << '\n'; //? 1 +//? cout << t.name << ' ' << t.size << ' ' << t.elements.size() << '\n'; //? 1 + size_t result = 0; + for (size_t i = 0; i < t.size; ++i) { + size_t tmp = size_of(t.elements[i]); +//? cout << i << ": " << t.elements[i][0] << ' ' << tmp << ' ' << result << '\n'; //? 1 + if (tmp > result) result = tmp; + } + // ...+1 for its tag. + return result+1; +} |