From 6cc4621602ac00effcfedeeb01593d5e619db0f9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 22 Feb 2015 00:15:14 -0800 Subject: 814 --- cpp/017record | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'cpp/017record') diff --git a/cpp/017record b/cpp/017record index 3248eb49..8dd02643 100644 --- a/cpp/017record +++ b/cpp/017record @@ -38,7 +38,10 @@ case GET: { assert(instructions[pc].ingredients[1].types.size() == 1); assert(instructions[pc].ingredients[1].types[0] == 0); // must be literal size_t offset = to_int(instructions[pc].ingredients[1].name); - int src = base_address+offset; + int src = base_address; + for (size_t i = 0; i < offset; ++i) { + src += size_of(reagent(Type[base_type].elements[i][0])); + } trace("run") << "address to copy is " << src; assert(Type[base_type].is_record); assert(Type[base_type].elements.size() > offset); @@ -69,6 +72,33 @@ recipe main [ +run: product 0 is 35 +mem: storing in location 15 +:(before "End Mu Types") +// A more complex record, containing another record. +int point_integer = Type_number["point-integer"] = Next_type_number++; +Type[point_integer].size = 2; +Type[point_integer].is_record = true; +vector p2; +p2.push_back(point); +Type[point_integer].elements.push_back(p2); +vector i2; +i2.push_back(integer); +Type[point_integer].elements.push_back(i2); +:(scenario "get_handles_record_fields") +recipe main [ + 12:integer <- copy 34:literal + 13:integer <- copy 35:literal + 14:integer <- copy 36:literal + 15:integer <- get 12:point-integer, 1:offset +] ++run: instruction 2 ++run: ingredient 0 is 12 ++run: ingredient 1 is 1 ++run: address to copy is 14 ++run: its type is 1 ++mem: location 14 is 36 ++run: product 0 is 36 ++mem: storing in location 15 + :(before "End Globals") // To write to fields of records, you need their address. const int GET_ADDRESS = 19; -- cgit 1.4.1-2-gfad0