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 [
1:number <- copy 34
2:number <- copy 35
3:address:duplex-list:character <- push 3, 0
3:address:duplex-list:character <- push 4, 3:address:duplex-list:character
3:address:duplex-list:character <- push 5, 3:address:duplex-list:character
4:address:duplex-list:character <- copy 3:address:duplex-list:character
5:character <- first 4:address:duplex-list:character
4:address:duplex-list:character <- next 4:address:duplex-list:character
6:character <- first 4:address:duplex-list:character
4:address:duplex-list:character <- next 4:address:duplex-list:character
7:character <- first 4:address:duplex-list:character
8:address:duplex-list:character <- next 4:address:duplex-list:character
9:character <- first 8:address:duplex-list:character
10:address:duplex-list:character <- next 8:address:duplex-list:character
11:address:duplex-list:character <- prev 8:address:duplex-list:character
4:address:duplex-list:character <- prev 4:address:duplex-list:character
12:character <- first 4:address:duplex-list:character
4:address:duplex-list:character <- prev 4:address:duplex-list:character
13:character <- first 4:address:duplex-list:character
14:boolean <- equal 3:address:duplex-list:character, 4:address:duplex-list:character
]
memory-should-contain [
0 <- 0
1 <- 34
2 <- 35
5 <- 5
6 <- 4
7 <- 3
8 <- 0
9 <- 0
10 <- 0
11 <- 0
12 <- 4
13 <- 5
14 <- 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 [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
2:address:duplex-list:character <- next 1:address:duplex-list:character
2:address:duplex-list:character <- insert 6, 2:address:duplex-list:character
2:address:duplex-list:character <- copy 1:address:duplex-list:character
3:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
5:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
6:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
7:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
8:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
9:character <- first 2:address:duplex-list:character
10:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 5
4 <- 4
5 <- 6
6 <- 3
7 <- 6
8 <- 4
9 <- 5
10 <- 1
]
]
scenario inserting-at-end-of-duplex-list [
run [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
2:address:duplex-list:character <- next 1:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
2:address:duplex-list:character <- insert 6, 2:address:duplex-list:character
2:address:duplex-list:character <- copy 1:address:duplex-list:character
3:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
5:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
6:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
7:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
8:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
9:character <- first 2:address:duplex-list:character
10:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 5
4 <- 4
5 <- 3
6 <- 6
7 <- 3
8 <- 4
9 <- 5
10 <- 1
]
]
scenario inserting-after-start-of-duplex-list [
run [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
1:address:duplex-list:character <- insert 6, 1:address:duplex-list:character
2:address:duplex-list:character <- copy 1:address:duplex-list:character
3:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
5:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
6:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
7:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
8:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
9:character <- first 2:address:duplex-list:character
10:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 5
4 <- 6
5 <- 4
6 <- 3
7 <- 4
8 <- 6
9 <- 5
10 <- 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 [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
2:address:duplex-list:character <- next 1:address:duplex-list:character
1:address:duplex-list:character <- remove 2:address:duplex-list:character, 1:address:duplex-list:character
3:boolean <- equal 2:address:duplex-list:character, 0
2:address:duplex-list:character <- copy 1:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
5:character <- first 2:address:duplex-list:character
6:address:duplex-list:character <- next 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
7:character <- first 2:address:duplex-list:character
8:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 0
4 <- 5
5 <- 3
6 <- 0
7 <- 5
8 <- 1
]
]
scenario removing-from-start-of-duplex-list [
run [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
1:address:duplex-list:character <- remove 1:address:duplex-list:character, 1:address:duplex-list:character
2:address:duplex-list:character <- copy 1:address:duplex-list:character
3:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
5:address:duplex-list:character <- next 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
6:character <- first 2:address:duplex-list:character
7:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 4
4 <- 3
5 <- 0
6 <- 4
7 <- 1
]
]
scenario removing-from-end-of-duplex-list [
run [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- push 4, 1:address:duplex-list:character
1:address:duplex-list:character <- push 5, 1:address:duplex-list:character
2:address:duplex-list:character <- next 1:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
1:address:duplex-list:character <- remove 2:address:duplex-list:character, 1:address:duplex-list:character
3:boolean <- equal 2:address:duplex-list:character, 0
2:address:duplex-list:character <- copy 1:address:duplex-list:character
4:character <- first 2:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
5:character <- first 2:address:duplex-list:character
6:address:duplex-list:character <- next 2:address:duplex-list:character
2:address:duplex-list:character <- prev 2:address:duplex-list:character
7:character <- first 2:address:duplex-list:character
8:boolean <- equal 1:address:duplex-list:character, 2:address:duplex-list:character
]
memory-should-contain [
3 <- 0
4 <- 5
5 <- 4
6 <- 0
7 <- 5
8 <- 1
]
]
scenario removing-from-singleton-list [
run [
1:address:duplex-list:character <- push 3, 0
1:address:duplex-list:character <- remove 1:address:duplex-list:character, 1:address:duplex-list:character
]
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 [
1:address:duplex-list:character <- push 18, 0
1:address:duplex-list:character <- push 17, 1:address:duplex-list:character
1:address:duplex-list:character <- push 16, 1:address:duplex-list:character
1:address:duplex-list:character <- push 15, 1:address:duplex-list:character
1:address:duplex-list:character <- push 14, 1:address:duplex-list:character
1:address:duplex-list:character <- push 13, 1:address:duplex-list:character
run [
2:address:duplex-list:character <- next 1:address:duplex-list:character
2:address:duplex-list:character <- next 2:address:duplex-list:character
2:address:duplex-list:character <- remove-between 2:address:duplex-list:character, 0
4:character <- get *1:address:duplex-list:character, value:offset
5:address:duplex-list:character <- next 1:address:duplex-list:character
6:character <- get *5:address:duplex-list:character, value:offset
7:address:duplex-list:character <- next 5:address:duplex-list:character
8:character <- get *7:address:duplex-list:character, value:offset
9:address:duplex-list:character <- next 7:address:duplex-list:character
]
memory-should-contain [
4 <- 13
6 <- 14
8 <- 15
9 <- 0
]
]
scenario remove-range-to-final [
1:address:duplex-list:character <- push 18, 0
1:address:duplex-list:character <- push 17, 1:address:duplex-list:character
1:address:duplex-list:character <- push 16, 1:address:duplex-list:character
1:address:duplex-list:character <- push 15, 1:address:duplex-list:character
1:address:duplex-list:character <- push 14, 1:address:duplex-list:character
1:address:duplex-list:character <- push 13, 1:address:duplex-list:character
run [
2:address:duplex-list:character <- next 1:address:duplex-list:character
3:address:duplex-list:character <- next 2:address:duplex-list:character
3:address:duplex-list:character <- next 3:address:duplex-list:character
3:address:duplex-list:character <- next 3:address:duplex-list:character
3:address:duplex-list:character <- next 3:address:duplex-list:character
remove-between 2:address:duplex-list:character, 3:address:duplex-list:character
4:character <- get *1:address:duplex-list:character, value:offset
5:address:duplex-list:character <- next 1:address:duplex-list:character
6:character <- get *5:address:duplex-list:character, value:offset
7:address:duplex-list:character <- next 5:address:duplex-list:character
8:character <- get *7:address:duplex-list:character, value:offset
9:address:duplex-list:character <- next 7:address:duplex-list:character
]
memory-should-contain [
4 <- 13
6 <- 14
8 <- 18
9 <- 0
]
]
scenario remove-range-empty [
1:address:duplex-list:character <- push 15, 0
1:address:duplex-list:character <- push 14, 1:address:duplex-list:character
1:address:duplex-list:character <- push 13, 1:address:duplex-list:character
run [
2:address:duplex-list:character <- next 1:address:duplex-list:character
remove-between 1:address:duplex-list:character, 2:address:duplex-list:character
4:character <- get *1:address:duplex-list:character, value:offset
5:address:duplex-list:character <- next 1:address:duplex-list:character
6:character <- get *5:address:duplex-list:character, value:offset
7:address:duplex-list:character <- next 5:address:duplex-list:character
8:character <- get *7:address:duplex-list:character, value:offset
9:address:duplex-list:character <- next 7:address:duplex-list:character
]
memory-should-contain [
4 <- 13
6 <- 14
8 <- 15
9 <- 0
]
]
scenario remove-range-to-end [
1:address:duplex-list:character <- push 18, 0
1:address:duplex-list:character <- push 17, 1:address:duplex-list:character
1:address:duplex-list:character <- push 16, 1:address:duplex-list:character
1:address:duplex-list:character <- push 15, 1:address:duplex-list:character
1:address:duplex-list:character <- push 14, 1:address:duplex-list:character
1:address:duplex-list:character <- push 13, 1:address:duplex-list:character
run [
2:address:duplex-list:character <- next 1:address:duplex-list:character
remove-between 2:address:duplex-list:character, 0
4:character <- get *1:address:duplex-list:character, value:offset
5:address:duplex-list:character <- next 1:address:duplex-list:character
6:character <- get *5:address:duplex-list:character, value:offset
7:address:duplex-list:character <- next 5:address:duplex-list:character
]
memory-should-contain [
4 <- 13
6 <- 14
7 <- 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
]