about summary refs log tree commit diff stats
path: root/042name.cc
Commit message (Expand)AuthorAgeFilesLines
* 3120Kartik K. Agaram2016-07-211-3/+3
* 3096Kartik K. Agaram2016-07-031-0/+1
* 3022Kartik K. Agaram2016-05-271-12/+3
* 3020 - names in chessboard testsKartik K. Agaram2016-05-261-0/+12
* 2990Kartik K. Agaram2016-05-201-6/+6
* 2955 - back to more refcount housekeepingKartik K. Agaram2016-05-121-1/+1
* 2898 - start filling in missing refcountsKartik K. Agaram2016-05-031-1/+1
* 2891 - precompute container sizes and offsetsKartik K. Agaram2016-05-021-1/+1
* 2864 - replace all address:shared with just addressKartik K. Agaram2016-04-241-2/+1
* 2861 - 'maybe-convert' no longer returns addressKartik K. Agaram2016-04-231-2/+4
* 2859 - rename 'get-address' to 'get-location'Kartik K. Agaram2016-04-231-2/+3
* 2831 - bugfix in static arraysKartik K. Agaram2016-04-131-0/+18
* 2830 - bring back deleted test from 2829Kartik K. Agaram2016-04-101-6/+7
* 2829 - issues while switching to 'put'Kartik K. Agaram2016-04-101-5/+5
* 2804 - support stashing arraysKartik K. Agaram2016-03-211-4/+7
* 2803Kartik K. Agaram2016-03-211-3/+3
* 2799 - new approach to undoing changes in testsKartik K. Agaram2016-03-201-4/+9
* 2773 - switch to 'int'Kartik K. Agaram2016-03-131-12/+12
* 2735 - define recipes using 'def'Kartik K. Agaram2016-03-081-11/+11
* 2718 - stop crashing on unknown spaceKartik K. Agaram2016-02-261-2/+16
* 2712Kartik K. Agaram2016-02-261-8/+8
* 2709Kartik K. Agaram2016-02-251-2/+0
* 2685Kartik K. Agaram2016-02-191-3/+3
* 2667 - redo container data structureKartik K. Agaram2016-02-171-3/+2
* 2633Kartik K. Agaram2016-02-061-4/+0
* 2614Kartik K. Agaram2016-01-291-5/+5
* 2576 - distinguish allocated addresses from othersKartik K. Agaram2016-01-191-1/+3
* 2562Kartik K. Agaram2016-01-171-3/+5
* 2555Kartik K. Agaram2016-01-111-2/+2
* 2546 - another phase-ordering constraintKartik K. Agaram2015-12-191-1/+1
* 2622Kartik K. Agaram2015-12-131-3/+3
* 2504 - support to-text in 'stash'Kartik K. Agaram2015-11-281-8/+9
* 2494Kartik K. Agaram2015-11-281-1/+1
* 2473 - bad idea to use /raw with multiple intentionsKartik K. Agaram2015-11-221-2/+2
* 2456Kartik K. Agaram2015-11-171-4/+8
* 2401Kartik K. Agaram2015-11-081-1/+1
* 2383 - new concern: idempotence of transformsKartik K. Agaram2015-11-061-1/+1
* 2382Kartik K. Agaram2015-11-061-0/+1
* 2379 - further improvements to map operationsKartik K. Agaram2015-11-061-1/+1
* 2377 - stop using operator[] in mapKartik K. Agaram2015-11-061-21/+21
* 2358 - starting to tackle the phase ordering problemKartik K. Agaram2015-11-041-2/+2
* 2321 - more preparations for static dispatchKartik K. Agaram2015-10-291-4/+3
* 2311Kartik K. Agaram2015-10-291-6/+7
* 2277 - reagents now have a tree of typesKartik K. Agaram2015-10-251-6/+7
* 2270Kartik K. Agaram2015-10-121-1/+1
* 2258 - separate warnings from errorsKartik K. Agaram2015-10-061-27/+28
* 2226 - standardize warning formatKartik K. Agaram2015-10-011-22/+22
* 2218 - check types in instructions much earlierKartik K. Agaram2015-09-301-10/+3
* 2199 - stop printing numbers in scientific notationKartik K. Agaram2015-09-141-2/+2
* 2095Kartik K. Agaram2015-08-281-2/+0
: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# A doubly linked list permits bidirectional traversal.

container duplex-list:_elem [
  value:_elem
  next:address:shared:duplex-list:_elem
  prev:address:shared:duplex-list:_elem
]

# should I say in/contained-in:result, allow ingredients to refer to products?
recipe push x:_elem, in:address:shared:duplex-list:_elem -> in:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  result:address:shared:duplex-list:_elem <- new {(duplex-list _elem): type}
  val:address:_elem <- get-address *result, value:offset
  *val <- copy x
  next:address:address:shared:duplex-list:_elem <- get-address *result, next:offset
  *next <- copy in
  {
    break-unless in
    prev:address:address:shared:duplex-list:_elem <- get-address *in, prev:offset
    *prev <- copy result
  }
  reply result  # needed explicitly because we need to replace 'in' with 'result'
]

recipe first in:address:shared:duplex-list:_elem -> result:_elem [
  local-scope
  load-ingredients
  reply-unless in, 0
  result <- get *in, value:offset
]

recipe next in:address:shared:duplex-list:_elem -> result:address:shared:duplex-list:_elem/contained-in:in [
  local-scope
  load-ingredients
  reply-unless in, 0
  result <- get *in, next:offset
]

recipe prev in:address:shared:duplex-list:_elem -> result:address:shared:duplex-list:_elem/contained-in:in [
  local-scope
  load-ingredients
  reply-unless in, 0
  result <- get *in, prev:offset
  reply result
]

scenario duplex-list-handling [
  run [
    # reserve locations 0, 1 and 2 to check for missing null check
    1:number <- copy 34
    2:number <- copy 35
    3:address:shared:duplex-list:character <- push 3, 0
    3:address:shared:duplex-list:character <- push 4, 3:address:shared:duplex-list:character
    3:address:shared:duplex-list:character <- push 5, 3:address:shared:duplex-list:character
    4:address:shared:duplex-list:character <- copy 3:address:shared:duplex-list:character
    5:character <- first 4:address:shared:duplex-list:character
    4:address:shared:duplex-list:character <- next 4:address:shared:duplex-list:character
    6:character <- first 4:address:shared:duplex-list:character
    4:address:shared:duplex-list:character <- next 4:address:shared:duplex-list:character
    7:character <- first 4:address:shared:duplex-list:character
    8:address:shared:duplex-list:character <- next 4:address:shared:duplex-list:character
    9:character <- first 8:address:shared:duplex-list:character
    10:address:shared:duplex-list:character <- next 8:address:shared:duplex-list:character
    11:address:shared:duplex-list:character <- prev 8:address:shared:duplex-list:character
    4:address:shared:duplex-list:character <- prev 4:address:shared:duplex-list:character
    12:character <- first 4:address:shared:duplex-list:character
    4:address:shared:duplex-list:character <- prev 4:address:shared:duplex-list:character
    13:character <- first 4:address:shared:duplex-list:character
    14:boolean <- equal 3:address:shared:duplex-list:character, 4:address:shared:duplex-list:character
  ]
  memory-should-contain [
    0 <- 0  # no modifications to null pointers
    1 <- 34
    2 <- 35
    5 <- 5  # scanning next
    6 <- 4
    7 <- 3
    8 <- 0  # null
    9 <- 0  # first of null
    10 <- 0  # next of null
    11 <- 0  # prev of null
    12 <- 4  # then start scanning prev
    13 <- 5
    14 <- 1  # list back at start
  ]
]

# insert 'x' after 'in'
recipe insert x:_elem, in:address:shared:duplex-list:_elem -> in:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  new-node:address:shared:duplex-list:_elem <- new {(duplex-list _elem): type}
  val:address:_elem <- get-address *new-node, value:offset
  *val <- copy x
  next-node:address:shared:duplex-list:_elem <- get *in, next:offset
  # in.next = new-node
  y:address:address:shared:duplex-list:_elem <- get-address *in, next:offset
  *y <- copy new-node
  # new-node.prev = in
  y <- get-address *new-node, prev:offset
  *y <- copy in
  # new-node.next = next-node
  y <- get-address *new-node, next:offset
  *y <- copy next-node
  # if next-node is not null
  reply-unless next-node
  # next-node.prev = new-node
  y <- get-address *next-node, prev:offset
  *y <- copy new-node
]

scenario inserting-into-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character  # 2 points inside list
    2:address:shared:duplex-list:character <- insert 6, 2:address:shared:duplex-list:character
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    3:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    5:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    6:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    7:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    8:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    9:character <- first 2:address:shared:duplex-list:character
    10:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  memory-should-contain [
    3 <- 5  # scanning next
    4 <- 4
    5 <- 6  # inserted element
    6 <- 3
    7 <- 6  # then prev
    8 <- 4
    9 <- 5
    10 <- 1  # list back at start
  ]
]

scenario inserting-at-end-of-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character  # 2 points inside list
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character  # now at end of list
    2:address:shared:duplex-list:character <- insert 6, 2:address:shared:duplex-list:character
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    3:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    5:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    6:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    7:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    8:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    9:character <- first 2:address:shared:duplex-list:character
    10:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  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
  ]
]

scenario inserting-after-start-of-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- insert 6, 1:address:shared:duplex-list:character
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    3:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    5:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    6:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    7:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    8:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    9:character <- first 2:address:shared:duplex-list:character
    10:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  memory-should-contain [
    3 <- 5  # scanning next
    4 <- 6  # inserted element
    5 <- 4
    6 <- 3
    7 <- 4  # then prev
    8 <- 6
    9 <- 5
    10 <- 1  # list back at start
  ]
]

# remove 'x' from its surrounding list 'in'
#
# Returns null if and only if list is empty. Beware: in that case any other
# pointers to the head are now invalid.
recipe remove x:address:shared:duplex-list:_elem/contained-in:in, in:address:shared:duplex-list:_elem -> in:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  # if 'x' is null, return
  reply-unless x
  next-node:address:shared:duplex-list:_elem <- get *x, next:offset
  prev-node:address:shared:duplex-list:_elem <- get *x, prev:offset
  # null x's pointers
  tmp:address:address:shared:duplex-list:_elem <- get-address *x, next:offset
  *tmp <- copy 0
  tmp <- get-address *x, prev:offset
  *tmp <- copy 0
  # if next-node is not null, set its prev pointer
  {
    break-unless next-node
    tmp <- get-address *next-node, prev:offset
    *tmp <- copy prev-node
  }
  # if prev-node is not null, set its next pointer and return
  {
    break-unless prev-node
    tmp <- get-address *prev-node, next:offset
    *tmp <- copy next-node
    reply
  }
  # if prev-node is null, then we removed the node at 'in'
  # return the new head rather than the old 'in'
  reply next-node
]

scenario removing-from-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character  # 2 points at second element
    1:address:shared:duplex-list:character <- remove 2:address:shared:duplex-list:character, 1:address:shared:duplex-list:character
    3:boolean <- equal 2:address:shared:duplex-list:character, 0
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    5:character <- first 2:address:shared:duplex-list:character
    6:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    7:character <- first 2:address:shared:duplex-list:character
    8:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  memory-should-contain [
    3 <- 0  # remove returned non-null
    4 <- 5  # scanning next, skipping deleted element
    5 <- 3
    6 <- 0  # no more elements
    7 <- 5  # prev of final element
    8 <- 1  # list back at start
  ]
]

scenario removing-from-start-of-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- remove 1:address:shared:duplex-list:character, 1:address:shared:duplex-list:character
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    3:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    5:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    6:character <- first 2:address:shared:duplex-list:character
    7:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  memory-should-contain [
    3 <- 4  # scanning next, skipping deleted element
    4 <- 3
    5 <- 0  # no more elements
    6 <- 4  # prev of final element
    7 <- 1  # list back at start
  ]
]

scenario removing-from-end-of-duplex-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- push 4, 1:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- push 5, 1:address:shared:duplex-list:character
    # delete last element
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    1:address:shared:duplex-list:character <- remove 2:address:shared:duplex-list:character, 1:address:shared:duplex-list:character
    3:boolean <- equal 2:address:shared:duplex-list:character, 0
    # check structure like before
    2:address:shared:duplex-list:character <- copy 1:address:shared:duplex-list:character
    4:character <- first 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    5:character <- first 2:address:shared:duplex-list:character
    6:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- prev 2:address:shared:duplex-list:character
    7:character <- first 2:address:shared:duplex-list:character
    8:boolean <- equal 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
  ]
  memory-should-contain [
    3 <- 0  # remove returned non-null
    4 <- 5  # scanning next, skipping deleted element
    5 <- 4
    6 <- 0  # no more elements
    7 <- 5  # prev of final element
    8 <- 1  # list back at start
  ]
]

scenario removing-from-singleton-list [
  run [
    1:address:shared:duplex-list:character <- push 3, 0
    1:address:shared:duplex-list:character <- remove 1:address:shared:duplex-list:character, 1:address:shared:duplex-list:character
  ]
  memory-should-contain [
    1 <- 0  # back to an empty list
  ]
]

# remove values between 'start' and 'end' (both exclusive)
# also clear pointers back out from start/end for hygiene
recipe remove-between start:address:shared:duplex-list:_elem, end:address:shared:duplex-list:_elem/contained-in:start -> start:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  reply-unless start
  # start->next->prev = 0
  # start->next = end
  next:address:address:shared:duplex-list:_elem <- get-address *start, next:offset
  nothing-to-delete?:boolean <- equal *next, end
  reply-if nothing-to-delete?
  prev:address:address:shared:duplex-list:_elem <- get-address **next, prev:offset
  *prev <- copy 0
  *next <- copy end
  reply-unless end
  # end->prev->next = 0
  # end->prev = start
  prev <- get-address *end, prev:offset
  next <- get-address **prev, next:offset
  *next <- copy 0
  *prev <- copy start
]

scenario remove-range [
  # construct a duplex list with six elements [13, 14, 15, 16, 17, 18]
  1:address:shared:duplex-list:character <- push 18, 0
  1:address:shared:duplex-list:character <- push 17, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 16, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 15, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 14, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 13, 1:address:shared:duplex-list:character
  run [
    # delete 16 onwards
    # first pointer: to the third element
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    2:address:shared:duplex-list:character <- remove-between 2:address:shared:duplex-list:character, 0
    # now check the list
    4:character <- get *1:address:shared:duplex-list:character, value:offset
    5:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    6:character <- get *5:address:shared:duplex-list:character, value:offset
    7:address:shared:duplex-list:character <- next 5:address:shared:duplex-list:character
    8:character <- get *7:address:shared:duplex-list:character, value:offset
    9:address:shared:duplex-list:character <- next 7:address:shared:duplex-list:character
  ]
  memory-should-contain [
    4 <- 13
    6 <- 14
    8 <- 15
    9 <- 0
  ]
]

scenario remove-range-to-end [
  # construct a duplex list with six elements [13, 14, 15, 16, 17, 18]
  1:address:shared:duplex-list:character <- push 18, 0
  1:address:shared:duplex-list:character <- push 17, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 16, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 15, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 14, 1:address:shared:duplex-list:character
  1:address:shared:duplex-list:character <- push 13, 1:address:shared:duplex-list:character
  run [
    # delete 15, 16 and 17
    # first pointer: to the third element
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    # second pointer: to the fifth element
    3:address:shared:duplex-list:character <- next 2:address:shared:duplex-list:character
    3:address:shared:duplex-list:character <- next 3:address:shared:duplex-list:character
    3:address:shared:duplex-list:character <- next 3:address:shared:duplex-list:character
    3:address:shared:duplex-list:character <- next 3:address:shared:duplex-list:character
    remove-between 2:address:shared:duplex-list:character, 3:address:shared:duplex-list:character
    # now check the list
    4:character <- get *1:address:shared:duplex-list:character, value:offset
    5:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    6:character <- get *5:address:shared:duplex-list:character, value:offset
    7:address:shared:duplex-list:character <- next 5:address:shared:duplex-list:character
    8:character <- get *7:address:shared:duplex-list:character, value:offset
    9:address:shared:duplex-list:character <- next 7:address:shared:duplex-list:character
  ]
  memory-should-contain [
    4 <- 13
    6 <- 14
    8 <- 18
    9 <- 0
  ]
]

scenario remove-range-empty [
  # construct a duplex list with six elements [13, 14, 15, 16, 17, 18]
  1:address:shared:duplex-list:character <- push 14, 0
  1:address:shared:duplex-list:character <- push 13, 1:address:shared:duplex-list:character
  run [
    # delete 16 onwards
    # first pointer: to the third element
    2:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    remove-between 1:address:shared:duplex-list:character, 2:address:shared:duplex-list:character
    # now check the list
    4:character <- get *1:address:shared:duplex-list:character, value:offset
    5:address:shared:duplex-list:character <- next 1:address:shared:duplex-list:character
    6:character <- get *5:address:shared:duplex-list:character, value:offset
    7:address:shared:duplex-list:character <- next 5:address:shared:duplex-list:character
  ]
  memory-should-contain [
    4 <- 13
    6 <- 14
    7 <- 0
  ]
]

# insert list beginning at 'new' after 'in'
recipe insert-range in:address:shared:duplex-list:_elem, start:address:shared:duplex-list:_elem/contained-in:in -> in:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  reply-unless in
  reply-unless start
  end:address:shared:duplex-list:_elem <- copy start
  {
    next:address:shared:duplex-list:_elem <- next end/insert-range
    break-unless next
    end <- copy next
    loop
  }
  next:address:shared:duplex-list:_elem <- next in
  dest:address:address:shared:duplex-list:_elem <- get-address *end, next:offset
  *dest <- copy next
  {
    break-unless next
    dest <- get-address *next, prev:offset
    *dest <- copy end
  }
  dest <- get-address *in, next:offset
  *dest <- copy start
  dest <- get-address *start, prev:offset
  *dest <- copy in
]

recipe append in:address:shared:duplex-list:_elem, new:address:shared:duplex-list:_elem/contained-in:in -> in:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  last:address:shared:duplex-list:_elem <- last in
  dest:address:address:shared:duplex-list:_elem <- get-address *last, next:offset
  *dest <- copy new
  reply-unless new
  dest <- get-address *new, prev:offset
  *dest <- copy last
]

recipe last in:address:shared:duplex-list:_elem -> result:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  result <- copy in
  {
    next:address:shared:duplex-list:_elem <- next result
    break-unless next
    result <- copy next
    loop
  }
]

# helper for debugging
recipe dump-from x:address:shared:duplex-list:_elem [
  local-scope
  load-ingredients
  $print x, [: ]
  {
    break-unless x
    c:_elem <- get *x, value:offset
    $print c, [ ]
    x <- next x
    {
      is-newline?:boolean <- equal c, 10/newline
      break-unless is-newline?
      $print 10/newline
      $print x, [: ]
    }
    loop
  }
  $print 10/newline, [---], 10/newline
]