about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/.traces/get_address24
-rw-r--r--cpp/017and-record39
2 files changed, 63 insertions, 0 deletions
diff --git a/cpp/.traces/get_address b/cpp/.traces/get_address
new file mode 100644
index 00000000..5b8510e4
--- /dev/null
+++ b/cpp/.traces/get_address
@@ -0,0 +1,24 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "34", type: 0}
+parse/0:   product: {name: "12", type: 1}
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "0", type: 0}
+parse/0:   product: {name: "13", type: 2}
+parse/0: instruction: 19
+parse/0:   ingredient: {name: "12", type: 3}
+parse/0:   ingredient: {name: "1", type: 0}
+parse/0:   product: {name: "15", type: 1}
+run/0: instruction 0
+run/0: ingredient 0 is 34
+mem/0: storing in location 12
+run/0: instruction 1
+run/0: ingredient 0 is 0
+mem/0: storing in location 13
+run/0: instruction 2
+run/0: ingredient 0 is 12
+run/0: base address 12
+run/0: base type is 3
+run/0: ingredient 1 is 1
+run/0: address to copy is 13
+run/0: product 0 is 13
+mem/0: storing in location 15
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