about summary refs log tree commit diff stats
path: root/cpp/017and-record
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-02-20 20:25:45 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-02-20 20:25:45 -0800
commita4559f7287599a981a3b38a82b741f57d50e85c4 (patch)
tree30e03f26c4559eadb8a5a715ec76b8f60c27dd4b /cpp/017and-record
parenteaa75c87c5b9ffb34ebd8c2ea0f1682dfcad336a (diff)
downloadmu-a4559f7287599a981a3b38a82b741f57d50e85c4.tar.gz
801
Diffstat (limited to 'cpp/017and-record')
-rw-r--r--cpp/017and-record39
1 files changed, 39 insertions, 0 deletions
diff --git a/cpp/017and-record b/cpp/017and-record
index 94febae0..f38b031f 100644
--- a/cpp/017and-record
+++ b/cpp/017and-record
@@ -74,3 +74,42 @@ recipe main [
 +mem: location 13 is 0
 +run: product 0 is 0
 +mem: storing in location 15
+
+:(before "End Globals")
+// To write to fields of records, you need their address.
+const int GET_ADDRESS = 19;
+:(before "End Primitive Recipe Numbers")
+Recipe_number["get-address"] = GET_ADDRESS;
+Next_recipe_number++;
+:(before "End Primitive Recipe Implementations")
+case GET_ADDRESS: {
+  trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
+  int base_address = to_int(instructions[pc].ingredients[0].name);
+  trace("run") << "base address " << base_address;
+  int base_type = instructions[pc].ingredients[0].types[0];
+  trace("run") << "base type is " << base_type;
+  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
+  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;
+  trace("run") << "address to copy is " << src;
+  vector<int> result;
+  result.push_back(src);
+  trace("run") << "product 0 is " << result[0];
+  write_memory(instructions[pc].products[0], result);
+  break;
+}
+
+:(scenario "get_address")
+recipe main [
+  12:integer <- copy 34:literal
+  13:boolean <- copy 0:literal
+  15:integer <- get-address 12:integer-boolean, 1:offset
+]
++run: instruction 2
++run: ingredient 0 is 12
++run: base address 12
++run: ingredient 1 is 1
++run: address to copy is 13
++mem: storing in location 15