about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/.traces/new_array28
-rw-r--r--cpp/026new18
2 files changed, 45 insertions, 1 deletions
diff --git a/cpp/.traces/new_array b/cpp/.traces/new_array
new file mode 100644
index 00000000..2fc43f51
--- /dev/null
+++ b/cpp/.traces/new_array
@@ -0,0 +1,28 @@
+parse/0: instruction: 24
+parse/0:   ingredient: {name: "integer", value: 0, type: 0, properties: [integer: type]}
+parse/0:   ingredient: {name: "5", value: 0, type: 0, properties: [5: literal]}
+parse/0:   product: {name: "1", value: 0, type: 2-4-1, properties: [1: address:array:integer, raw: ]}
+parse/0: instruction: 24
+parse/0:   ingredient: {name: "integer", value: 0, type: 0, properties: [integer: type]}
+parse/0:   product: {name: "2", value: 0, type: 2-1, properties: [2: address:integer, raw: ]}
+parse/0: instruction: 3
+parse/0:   ingredient: {name: "2", value: 0, type: 2-1, properties: [2: address:integer, raw: ]}
+parse/0:   ingredient: {name: "1", value: 0, type: 2-4-1, properties: [1: address:array:integer, raw: ]}
+parse/0:   product: {name: "3", value: 0, type: 1, properties: [3: integer, raw: ]}
+new/0: integer -> 1
+new/0: integer -> 1
+after-brace/0: recipe main
+after-brace/0: new ...
+after-brace/0: new ...
+after-brace/0: subtract ...
+run/0: instruction main/0
+mem/0: storing 1000 in location 1
+run/0: instruction main/1
+mem/0: storing 1005 in location 2
+run/0: instruction main/2
+run/0: ingredient 0 is 2
+mem/0: location 2 is 1005
+run/0: ingredient 1 is 1
+mem/0: location 1 is 1000
+run/0: product 0 is 5
+mem/0: storing 5 in location 3
diff --git a/cpp/026new b/cpp/026new
index edc17b17..ffb4429a 100644
--- a/cpp/026new
+++ b/cpp/026new
@@ -47,6 +47,22 @@ case NEW: {
   write_memory(instructions[pc].products[0], result);
   vector<int> types;
   types.push_back(instructions[pc].ingredients[0].value);
-  Current_routine.alloc += size_of(types);
+  if (instructions[pc].ingredients.size() > 1) {
+    // array
+    vector<int> capacity = read_memory(instructions[pc].ingredients[1]);
+    Current_routine.alloc += capacity[0]*size_of(types);
+  }
+  else {
+    // scalar
+    Current_routine.alloc += size_of(types);
+  }
   break;
 }
+
+:(scenario "new_array")
+recipe main [
+  1:address:array:integer/raw <- new integer:type, 5:literal
+  2:address:integer/raw <- new integer:type
+  3:integer/raw <- subtract 2:address:integer/raw, 1:address:array:integer/raw
+]
++mem: storing 5 in location 3