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 00:03:47 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-02-20 00:03:47 -0800
commit916ae8f5d64995f786ea5610affdd004258e8eb9 (patch)
treee2f5c8f13ead6933767915dcecbb3323dae83eda /cpp/017and-record
parentb291f85b8d0ece9312b066a84cbeca1b367fe85f (diff)
downloadmu-916ae8f5d64995f786ea5610affdd004258e8eb9.tar.gz
799 - 'get' on records
Diffstat (limited to 'cpp/017and-record')
-rw-r--r--cpp/017and-record56
1 files changed, 56 insertions, 0 deletions
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
+}