about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-06-18 10:50:54 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-06-18 10:50:54 -0700
commit960e680db01537831d46c62448e8940e1abc4be8 (patch)
tree4823fa12a99799e7fbf7e6ca2503d0afaf245aef
parent00ee2decfe597212091e8d282232950055405635 (diff)
downloadmu-960e680db01537831d46c62448e8940e1abc4be8.tar.gz
1591
-rw-r--r--037call_reply.cc17
-rw-r--r--065duplex_list.mu46
2 files changed, 60 insertions, 3 deletions
diff --git a/037call_reply.cc b/037call_reply.cc
index 0d277658..fa224216 100644
--- a/037call_reply.cc
+++ b/037call_reply.cc
@@ -135,3 +135,20 @@ if (curr.name == "reply-if") {
   curr.operation = Recipe_number["reply"];
   curr.ingredients.swap(results);
 }
+// rewrite `reply-unless a, b, c, ...` to
+//   ```
+//   jump-if a, 1:offset
+//   reply b, c, ...
+//   ```
+if (curr.name == "reply-unless") {
+  assert(curr.products.empty());
+  curr.operation = Recipe_number["jump-if"];
+  vector<reagent> results;
+  copy(++curr.ingredients.begin(), curr.ingredients.end(), inserter(results, results.end()));
+  curr.ingredients.resize(1);
+  curr.ingredients.push_back(reagent("1:offset"));
+  result.steps.push_back(curr);
+  curr.clear();
+  curr.operation = Recipe_number["reply"];
+  curr.ingredients.swap(results);
+}
diff --git a/065duplex_list.mu b/065duplex_list.mu
index 8c5f6011..c33bce97 100644
--- a/065duplex_list.mu
+++ b/065duplex_list.mu
@@ -86,15 +86,17 @@ recipe insert-duplex [
   # in.next = new-node
   y:address:address:duplex-list <- get-address in:address:duplex-list/deref, next:offset
   y:address:address:duplex-list/deref <- copy new-node:address:duplex-list
+  # new-node.prev = in
+  y:address:address:duplex-list <- get-address new-node:address:duplex-list/deref, prev:offset
+  y:address:address:duplex-list/deref <- copy in:address:duplex-list
   # new-node.next = next-node
   y:address:address:duplex-list <- get-address new-node:address:duplex-list/deref, next:offset
   y:address:address:duplex-list/deref <- copy next-node:address:duplex-list
+  # if next-node is not null
+  reply-unless next-node:address:duplex-list, new-node:address:duplex-list
   # next-node.prev = new-node
   y:address:address:duplex-list <- get-address next-node:address:duplex-list/deref, prev:offset
   y:address:address:duplex-list/deref <- copy new-node:address:duplex-list
-  # new-node.prev = in
-  y:address:address:duplex-list <- get-address new-node:address:duplex-list/deref, prev:offset
-  y:address:address:duplex-list/deref <- copy in:address:duplex-list
   reply new-node:address:duplex-list  # just signalling something changed; don't rely on the result
 ]
 
@@ -134,3 +136,41 @@ scenario inserting-into-duplex-list [
     10 <- 1  # list back at start
   ]
 ]
+
+scenario inserting-at-end-of-duplex-list [
+  run [
+    1:address:duplex-list <- copy 0:literal  # 1 points to head of list
+    1:address:duplex-list <- push-duplex 3:literal, 1:address:duplex-list
+    1:address:duplex-list <- push-duplex 4:literal, 1:address:duplex-list
+    1:address:duplex-list <- push-duplex 5:literal, 1:address:duplex-list
+    2:address:duplex-list <- next-duplex 1:address:duplex-list  # 2 points inside list
+    2:address:duplex-list <- next-duplex 2:address:duplex-list  # now at end of list
+    2:address:duplex-list <- insert-duplex 6:literal, 2:address:duplex-list
+    # check structure like before
+    2:address:duplex-list <- copy 1:address:duplex-list
+    3:number <- first 2:address:duplex-list
+    2:address:duplex-list <- next-duplex 2:address:duplex-list
+    4:number <- first 2:address:duplex-list
+    2:address:duplex-list <- next-duplex 2:address:duplex-list
+    5:number <- first 2:address:duplex-list
+    2:address:duplex-list <- next-duplex 2:address:duplex-list
+    6:number <- first 2:address:duplex-list
+    2:address:duplex-list <- prev-duplex 2:address:duplex-list
+    7:number <- first 2:address:duplex-list
+    2:address:duplex-list <- prev-duplex 2:address:duplex-list
+    8:number <- first 2:address:duplex-list
+    2:address:duplex-list <- prev-duplex 2:address:duplex-list
+    9:number <- first 2:address:duplex-list
+    10:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
+  ]
+  memory-should-contain [
+    3 <- 5  # scanning next
+    4 <- 4
+    5 <- 3
+    6 <- 6  # inserted element
+    7 <- 3  # then prev
+    8 <- 4
+    9 <- 5
+    10 <- 1  # list back at start
+  ]
+]