about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/.traces/get26
-rw-r--r--cpp/012run1
-rw-r--r--cpp/017and-record56
3 files changed, 83 insertions, 0 deletions
diff --git a/cpp/.traces/get b/cpp/.traces/get
new file mode 100644
index 00000000..019070c2
--- /dev/null
+++ b/cpp/.traces/get
@@ -0,0 +1,26 @@
+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: 18
+parse/0:   ingredient: {name: "12", type: 3}
+parse/0:   ingredient: {name: "1", type: 0}
+parse/0:   product: {name: "15", type: 2}
+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: its type is 2
+mem/0: location 13 is 0
+run/0: product 0 is 0
+mem/0: storing in location 15
diff --git a/cpp/012run b/cpp/012run
index a1778705..5d0e28cf 100644
--- a/cpp/012run
+++ b/cpp/012run
@@ -42,6 +42,7 @@ void run(recipe_number r) {
 }
 
 vector<int> read_memory(reagent x) {
+//?   cout << "read_memory: " << x.to_string() << '\n'; //? 1
   vector<int> result;
   if (x.types[0] == 0) {  // literal
     result.push_back(to_int(x.name));
diff --git a/cpp/017and-record b/cpp/017and-record
index a9a3c0a0..b4026e10 100644
--- a/cpp/017and-record
+++ b/cpp/017and-record
@@ -25,3 +25,59 @@ recipe main [
 +mem: location 2 is 0
 +mem: storing in location 3
 +mem: storing in location 4
+
+:(before "End Globals")
+// Operator to look at fields of records.
+const int GET = 18;
+:(before "End Primitive Recipe Numbers")
+Recipe_number["get"] = GET;
+Next_recipe_number++;
+:(before "End Primitive Recipe Implementations")
+case GET: {
+  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;
+  assert(Type[base_type].is_record);
+  assert(Type[base_type].elements.size() > offset);
+  int src_type = Type[base_type].elements[offset][0];
+  trace("run") << "its type is " << src_type;
+  ostringstream s;
+  s << src;
+  reagent tmp(s.str());
+  tmp.types[0] = src_type;
+  vector<int> result(read_memory(tmp));
+  trace("run") << "product 0 is " << result[0];
+  write_memory(instructions[pc].products[0], result);
+  break;
+}
+
+:(scenario "get")
+recipe main [
+  12:integer <- copy 34:literal
+  13:boolean <- copy 0:literal
+  15:boolean <- get 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
++run: its type is 2
++mem: location 13 is 0
++run: product 0 is 0
++mem: storing in location 15
+
+:(code)
+int size_of(type_number x) {
+  type_info t = Type[x];
+  if (!t.is_record && !t.is_array) return t.size;
+  return t.size;  // TODO
+}