diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-21 22:08:58 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-21 22:08:58 -0800 |
commit | d160a8e170ed5488b91c6a9d5e46546d8132233b (patch) | |
tree | 2e3b66cdcdea3732a1d3096e1e97924180cb27f0 /cpp/019array | |
parent | fe67d47aa4598497e295510cf27f702a3e118632 (diff) | |
download | mu-d160a8e170ed5488b91c6a9d5e46546d8132233b.tar.gz |
811 - first test for arrays
Diffstat (limited to 'cpp/019array')
-rw-r--r-- | cpp/019array | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cpp/019array b/cpp/019array new file mode 100644 index 00000000..51265826 --- /dev/null +++ b/cpp/019array @@ -0,0 +1,54 @@ +// Support for records. +:(before "End Mu Types") +// We'll use this array as a running example: +int integer_array = Type_number["integer-array"] = Next_type_number++; +Type[integer_array].is_array = true; +Type[integer_array].element.push_back(integer); + +:(scenario copy_array) +# Arrays can be copied around with a single instruction just like integers, +# no matter how large they are. +recipe main [ + 1:integer <- copy 3:literal + 2:integer <- copy 14:literal + 3:integer <- copy 15:literal + 4:integer <- copy 16:literal + 5:integer-array <- copy 1:integer-array +] ++run: instruction 4 ++run: ingredient 0 is 1 ++mem: location 1 is 3 ++mem: location 2 is 14 ++mem: location 3 is 15 ++mem: location 4 is 16 ++mem: storing in location 5 ++mem: storing in location 6 ++mem: storing in location 7 ++mem: storing in location 8 + +:(replace{} "size_t size_of(reagent r)") +size_t size_of(reagent r) { + type_info t = Type[r.types[0]]; + if (t.is_record) { + int result = 0; + for (size_t i = 0; i < t.size; ++i) { + ostringstream out; + out << result; + reagent x(out.str()); + copy(t.elements[i].begin(), t.elements[i].end(), inserter(x.types, x.types.begin())); + result += size_of(x); + } + return result; + } + if (t.is_array) { + int base = to_int(r.name); + if (Memory[base] == 0) return 0; + ostringstream out; + out << base+1; + reagent x(out.str()); + x.types.push_back(t.element[0]); + return 1 + Memory[base]*size_of(x); + } + // scalar + return t.size; +} |