container duplex-list:_elem [
value:_elem
next:address:duplex-list:_elem
prev:address:duplex-list:_elem
]
def push x:_elem, in:address:duplex-list:_elem -> in:address:duplex-list:_elem [
local-scope
load-ingredients
result:address:duplex-list:_elem <- new {(duplex-list _elem): type}
*result <- merge x, in, 0
{
break-unless in
*in <- put *in, prev:offset, result
}
return result
]
def first in:address:duplex-list:_elem -> result:_elem [
local-scope
load-ingredients
return-unless in, 0
result <- get *in, value:offset
]
def next in:address:duplex-list:_elem -> result:address:duplex-list:_elem/contained-in:in [
local-scope
load-ingredients
return-unless in, 0
result <- get *in, next:offset
]
def prev in:address:duplex-list:_elem -> result:address:duplex-list:_elem/contained-in:in [
local-scope
load-ingredients
return-unless in, 0
result <- get *in, prev:offset
return result
]
scenario duplex-list-handling [
run [
local-scope
10:number/raw <- copy 34
11:number/raw <- copy 35
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list2:address:duplex-list:character <- copy list
20:character/raw <- first list2
list2 <- next list2
21:character/raw <- first list2
list2 <- next list2
22:character/raw <- first list2
30:address:duplex-list:character/raw <- next list2
31:character/raw <- first 30:address:duplex-list:character/raw
32:address:duplex-list:character/raw <- next 30:address:duplex-list:character/raw
33:address:duplex-list:character/raw <- prev 30:address:duplex-list:character/raw
list2 <- prev list2
40:character/raw <- first list2
list2 <- prev list2
41:character/raw <- first list2
50:boolean/raw <- equal list, list2
]
memory-should-contain [
0 <- 0
10 <- 34
11 <- 35
20 <- 5
21 <- 4
22 <- 3
30 <- 0
31 <- 0
32 <- 0
33 <- 0
40 <- 4
41 <- 5
50 <- 1
]
]
def insert x:_elem, in:address:duplex-list:_elem -> in:address:duplex-list:_elem [
local-scope
load-ingredients
new-node:address:duplex-list:_elem <- new {(duplex-list _elem): type}
*new-node <- put *new-node, value:offset, x
next-node:address:duplex-list:_elem <- get *in, next:offset
*in <- put *in, next:offset, new-node
*new-node <- put *new-node, prev:offset, in
*new-node <- put *new-node, next:offset, next-node
return-unless next-node
*next-node <- put *next-node, prev:offset, new-node
]
scenario inserting-into-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list2:address:duplex-list:character <- next list
list2 <- insert 6, list2
list2 <- copy list
10:character/raw <- first list2
list2 <- next list2
11:character/raw <- first list2
list2 <- next list2
12:character/raw <- first list2
list2 <- next list2
13:character/raw <- first list2
list2 <- prev list2
20:character/raw <- first list2
list2 <- prev list2
21:character/raw <- first list2
list2 <- prev list2
22:character/raw <- first list2
30:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 5
11 <- 4
12 <- 6
13 <- 3
20 <- 6
21 <- 4
22 <- 5
30 <- 1
]
]
scenario inserting-at-end-of-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list2:address:duplex-list:character <- next list
list2 <- next list2
list2 <- insert 6, list2
list2 <- copy list
10:character/raw <- first list2
list2 <- next list2
11:character/raw <- first list2
list2 <- next list2
12:character/raw <- first list2
list2 <- next list2
13:character/raw <- first list2
list2 <- prev list2
20:character/raw <- first list2
list2 <- prev list2
21:character/raw <- first list2
list2 <- prev list2
22:character/raw <- first list2
30:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 5
11 <- 4
12 <- 3
13 <- 6
20 <- 3
21 <- 4
22 <- 5
30 <- 1
]
]
scenario inserting-after-start-of-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list <- insert 6, list
list2:address:duplex-list:character <- copy list
10:character/raw <- first list2
list2 <- next list2
11:character/raw <- first list2
list2 <- next list2
12:character/raw <- first list2
list2 <- next list2
13:character/raw <- first list2
list2 <- prev list2
20:character/raw <- first list2
list2 <- prev list2
21:character/raw <- first list2
list2 <- prev list2
22:character/raw <- first list2
30:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 5
11 <- 6
12 <- 4
13 <- 3
20 <- 4
21 <- 6
22 <- 5
30 <- 1
]
]
def remove x:address:duplex-list:_elem/contained-in:in, in:address:duplex-list:_elem -> in:address:duplex-list:_elem [
local-scope
load-ingredients
return-unless x
next-node:address:duplex-list:_elem <- get *x, next:offset
prev-node:address:duplex-list:_elem <- get *x, prev:offset
*x <- put *x, next:offset, 0
*x <- put *x, prev:offset, 0
{
break-unless next-node
*next-node <- put *next-node, prev:offset, prev-node
}
{
break-unless prev-node
*prev-node <- put *prev-node, next:offset, next-node
return
}
return next-node
]
scenario removing-from-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list2:address:duplex-list:character <- next list
list <- remove list2, list
10:boolean/raw <- equal list2, 0
list2 <- copy list
11:character/raw <- first list2
list2 <- next list2
12:character/raw <- first list2
20:address:duplex-list:character/raw <- next list2
list2 <- prev list2
30:character/raw <- first list2
40:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 0
11 <- 5
12 <- 3
20 <- 0
30 <- 5
40 <- 1
]
]
scenario removing-from-start-of-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list <- remove list, list
list2:address:duplex-list:character <- copy list
10:character/raw <- first list2
list2 <- next list2
11:character/raw <- first list2
20:address:duplex-list:character/raw <- next list2
list2 <- prev list2
30:character/raw <- first list2
40:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 4
11 <- 3
20 <- 0
30 <- 4
40 <- 1
]
]
scenario removing-from-end-of-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- push 4, list
list <- push 5, list
list2:address:duplex-list:character <- next list
list2 <- next list2
list <- remove list2, list
10:boolean/raw <- equal list2, 0
list2 <- copy list
11:character/raw <- first list2
list2 <- next list2
12:character/raw <- first list2
20:address:duplex-list:character/raw <- next list2
list2 <- prev list2
30:character/raw <- first list2
40:boolean/raw <- equal list, list2
]
memory-should-contain [
10 <- 0
11 <- 5
12 <- 4
20 <- 0
30 <- 5
40 <- 1
]
]
scenario removing-from-singleton-duplex-list [
run [
local-scope
list:address:duplex-list:character <- push 3, 0
list <- remove list, list
1:number/raw <- copy list
]
memory-should-contain [
1 <- 0
]
]
def remove-between start:address:duplex-list:_elem, end:address:duplex-list:_elem/contained-in:start -> start:address:duplex-list:_elem [
local-scope
load-ingredients
next:address:duplex-list:_elem <- get *start, next:offset
nothing-to-delete?:boolean <- equal next, end
return-if nothing-to-delete?
assert next, [malformed duplex list]
*next <- put *next, prev:offset, 0
*start <- put *start, next:offset, end
return-unless end
prev:address:duplex-list:_elem <- get *end, prev:offset
assert prev, [malformed duplex list - 2]
*prev <- put *prev, next:offset, 0
*end <- put *end, prev:offset, start
]
scenario remove-range [
local-scope
list:address:duplex-list:character <- push 18, 0
list <- push 17, list
list <- push 16, list
list <- push 15, list
list <- push 14, list
list <- push 13, list
1:address:duplex-list:character/raw <- copy list
run [
local-scope
list:address:duplex-list:character <- copy 1:address:duplex-list:character/raw
list2:address:duplex-list:character <- next list
list2 <- next list2
list2 <- remove-between list2, 0
10:character/raw <- get *list, value:offset
list <- next list
11:character/raw <- get *list, value:offset
list <- next list
12:character/raw <- get *list, value:offset
20:address:duplex-list:character/raw <- next list
]
memory-should-contain [
10 <- 13
11 <- 14
12 <- 15
20 <- 0
]
]
scenario remove-range-to-final [
local-scope
list:address:duplex-list:character <- push 18, 0
list <- push 17, list
list <- push 16, list
list <- push 15, list
list <- push 14, list
list <- push 13, list
1:address:duplex-list:character/raw <- copy list
run [
local-scope
list:address:duplex-list:character <- copy 1:address:duplex-list:character/raw
list2:address:duplex-list:character <- next list
end:address:duplex-list:character <- next list2
end <- next end
end <- next end
end <- next end
remove-between list2, end
10:character/raw <- get *list, value:offset
list <- next list
11:character/raw <- get *list, value:offset
list <- next list
12:character/raw <- get *list, value:offset
20:address:duplex-list:character/raw <- next list
]
memory-should-contain [
10 <- 13
11 <- 14
12 <- 18
20 <- 0
]
]
scenario remove-range-empty [
local-scope
list:address:duplex-list:character <- push 15, 0
list <- push 14, list
list <- push 13, list
1:address:duplex-list:character/raw <- copy list
run [
local-scope
list:address:duplex-list:character <- copy 1:address:duplex-list:character/raw
list2:address:duplex-list:character <- next list
remove-between list, list2
10:character/raw <- get *list, value:offset
list <- next list
11:character/raw <- get *list, value:offset
list <- next list
12:character/raw <- get *list, value:offset
20:address:duplex-list:character/raw <- next list
]
memory-should-contain [
10 <- 13
11 <- 14
12 <- 15
20 <- 0
]
]
scenario remove-range-to-end [
local-scope
list:address:duplex-list:character <- push 18, 0
list <- push 17, list
list <- push 16, list
list <- push 15, list
list <- push 14, list
list <- push 13, list
1:address:duplex-list:character/raw <- copy list
run [
local-scope
list:address:duplex-list:character <- copy 1:address:duplex-list:character/raw
list2:address:duplex-list:character <- next list
remove-between list2, 0
10:character/raw <- get *list, value:offset
list <- next list
11:character/raw <- get *list, value:offset
20:address:duplex-list:character/raw <- next list
]
memory-should-contain [
10 <- 13
11 <- 14
20 <- 0
]
]
def insert-range in:address:duplex-list:_elem, start:address:duplex-list:_elem/contained-in:in -> in:address:duplex-list:_elem [
local-scope
load-ingredients
return-unless in
return-unless start
end:address:duplex-list:_elem <- copy start
{
next:address:duplex-list:_elem <- next end/insert-range
break-unless next
end <- copy next
loop
}
next:address:duplex-list:_elem <- next in
*end <- put *end, next:offset, next
{
break-unless next
*next <- put *next, prev:offset, end
}
*in <- put *in, next:offset, start
*start <- put *start, prev:offset, in
]
def append in:address:duplex-list:_elem, new:address:duplex-list:_elem/contained-in:in -> in:address:duplex-list:_elem [
local-scope
load-ingredients
last:address:duplex-list:_elem <- last in
*last <- put *last, next:offset, new
return-unless new
*new <- put *new, prev:offset, last
]
def last in:address:duplex-list:_elem -> result:address:duplex-list:_elem [
local-scope
load-ingredients
result <- copy in
{
next:address:duplex-list:_elem <- next result
break-unless next
result <- copy next
loop
}
]
def dump-from x:address: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
]