container duplex-list [
value:location
next:address:duplex-list
prev:address:duplex-list
]
recipe push-duplex [
local-scope
x:location <- next-ingredient
in:address:duplex-list <- next-ingredient
result:address:duplex-list <- new duplex-list:type
val:address:location <- get-address *result, value:offset
*val <- copy x
next:address:address:duplex-list <- get-address *result, next:offset
*next <- copy in
reply-unless in, result
prev:address:address:duplex-list <- get-address *in, prev:offset
*prev <- copy result
reply result
]
recipe first-duplex [
local-scope
in:address:duplex-list <- next-ingredient
reply-unless in, 0
result:location <- get *in, value:offset
reply result
]
recipe next-duplex [
local-scope
in:address:duplex-list <- next-ingredient
reply-unless in, 0
result:address:duplex-list <- get *in, next:offset
reply result
]
recipe prev-duplex [
local-scope
in:address:duplex-list <- next-ingredient
reply-unless in, 0
result:address:duplex-list <- get *in, prev:offset
reply result
]
scenario duplex-list-handling [
run [
1:number <- copy 34
2:number <- copy 35
3:address:duplex-list <- copy 0
3:address:duplex-list <- push-duplex 3, 3:address:duplex-list
3:address:duplex-list <- push-duplex 4, 3:address:duplex-list
3:address:duplex-list <- push-duplex 5, 3:address:duplex-list
4:address:duplex-list <- copy 3:address:duplex-list
5:number <- first-duplex 4:address:duplex-list
4:address:duplex-list <- next-duplex 4:address:duplex-list
6:number <- first-duplex 4:address:duplex-list
4:address:duplex-list <- next-duplex 4:address:duplex-list
7:number <- first-duplex 4:address:duplex-list
8:address:duplex-list <- next-duplex 4:address:duplex-list
9:number <- first-duplex 8:address:duplex-list
10:address:duplex-list <- next-duplex 8:address:duplex-list
11:address:duplex-list <- prev-duplex 8:address:duplex-list
4:address:duplex-list <- prev-duplex 4:address:duplex-list
12:number <- first-duplex 4:address:duplex-list
4:address:duplex-list <- prev-duplex 4:address:duplex-list
13:number <- first-duplex 4:address:duplex-list
14:boolean <- equal 3:address:duplex-list, 4:address:duplex-list
]
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
]
]
recipe insert-duplex [
local-scope
x:location <- next-ingredient
in:address:duplex-list <- next-ingredient
new-node:address:duplex-list <- new duplex-list:type
val:address:location <- get-address *new-node, value:offset
*val <- copy x
next-node:address:duplex-list <- get *in, next:offset
y:address:address:duplex-list <- get-address *in, next:offset
*y <- copy new-node
y <- get-address *new-node, prev:offset
*y <- copy in
y <- get-address *new-node, next:offset
*y <- copy next-node
reply-unless next-node, new-node
y <- get-address *next-node, prev:offset
*y <- copy new-node
reply new-node
]
scenario inserting-into-duplex-list [
run [
1:address:duplex-list <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
2:address:duplex-list <- next-duplex 1:address:duplex-list
2:address:duplex-list <- insert-duplex 6, 2:address:duplex-list
2:address:duplex-list <- copy 1:address:duplex-list
3:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
5:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
6:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
7:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
8:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
9:number <- first-duplex 2:address:duplex-list
10:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
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 <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
2:address:duplex-list <- next-duplex 1:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
2:address:duplex-list <- insert-duplex 6, 2:address:duplex-list
2:address:duplex-list <- copy 1:address:duplex-list
3:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
5:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
6:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
7:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
8:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
9:number <- first-duplex 2:address:duplex-list
10:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
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 <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
2:address:duplex-list <- insert-duplex 6, 1:address:duplex-list
2:address:duplex-list <- copy 1:address:duplex-list
3:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
5:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
6:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
7:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
8:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
9:number <- first-duplex 2:address:duplex-list
10:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
memory-should-contain [
3 <- 5
4 <- 6
5 <- 4
6 <- 3
7 <- 4
8 <- 6
9 <- 5
10 <- 1
]
]
recipe remove-duplex [
local-scope
in:address:duplex-list <- next-ingredient
reply-unless in, in
next-node:address:duplex-list <- get *in, next:offset
prev-node:address:duplex-list <- get *in, prev:offset
x:address:address:duplex-list <- get-address *in, next:offset
*x <- copy 0
x <- get-address *in, prev:offset
*x <- copy 0
{
break-unless next-node
x <- get-address *next-node, prev:offset
*x <- copy prev-node
}
{
break-unless prev-node
x <- get-address *prev-node, next:offset
*x <- copy next-node
reply prev-node
}
reply next-node
]
scenario removing-from-duplex-list [
run [
1:address:duplex-list <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
2:address:duplex-list <- next-duplex 1:address:duplex-list
2:address:duplex-list <- remove-duplex 2:address:duplex-list
3:boolean <- equal 2:address:duplex-list, 0
2:address:duplex-list <- copy 1:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
5:number <- first-duplex 2:address:duplex-list
6:address:duplex-list <- next-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
7:number <- first-duplex 2:address:duplex-list
8:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
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 <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
1:address:duplex-list <- remove-duplex 1:address:duplex-list
2:address:duplex-list <- copy 1:address:duplex-list
3:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
5:address:duplex-list <- next-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
6:number <- first-duplex 2:address:duplex-list
7:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
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 <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
1:address:duplex-list <- push-duplex 4, 1:address:duplex-list
1:address:duplex-list <- push-duplex 5, 1:address:duplex-list
2:address:duplex-list <- next-duplex 1:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
2:address:duplex-list <- remove-duplex 2:address:duplex-list
3:boolean <- equal 2:address:duplex-list, 0
2:address:duplex-list <- copy 1:address:duplex-list
4:number <- first-duplex 2:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
5:number <- first-duplex 2:address:duplex-list
6:address:duplex-list <- next-duplex 2:address:duplex-list
2:address:duplex-list <- prev-duplex 2:address:duplex-list
7:number <- first-duplex 2:address:duplex-list
8:boolean <- equal 1:address:duplex-list, 2:address:duplex-list
]
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 <- copy 0
1:address:duplex-list <- push-duplex 3, 1:address:duplex-list
2:address:duplex-list <- remove-duplex 1:address:duplex-list
3:address:duplex-list <- get *1:address:duplex-list, next:offset
4:address:duplex-list <- get *1:address:duplex-list, prev:offset
]
memory-should-contain [
2 <- 0
3 <- 0
4 <- 0
]
]
recipe remove-duplex-between [
local-scope
start:address:duplex-list <- next-ingredient
end:address:duplex-list <- next-ingredient
reply-unless start, start
next:address:address:duplex-list <- get-address *start, next:offset
nothing-to-delete?:boolean <- equal *next, end
reply-if nothing-to-delete?, start
prev:address:address:duplex-list <- get-address **next, prev:offset
*prev <- copy 0
*next <- copy end
reply-unless end, start
prev <- get-address *end, prev:offset
next <- get-address **prev, next:offset
*next <- copy 0
*prev <- copy start
reply start
]
scenario remove-range [
1:address:duplex-list <- copy 0
1:address:duplex-list <- push-duplex 18, 1:address:duplex-list
1:address:duplex-list <- push-duplex 17, 1:address:duplex-list
1:address:duplex-list <- push-duplex 16, 1:address:duplex-list
1:address:duplex-list <- push-duplex 15, 1:address:duplex-list
1:address:duplex-list <- push-duplex 14, 1:address:duplex-list
1:address:duplex-list <- push-duplex 13, 1:address:duplex-list
run [
2:address:duplex-list <- next-duplex 1:address:duplex-list
2:address:duplex-list <- next-duplex 2:address:duplex-list
remove-duplex-between 2:address:duplex-list, 0
4:number <- get *1:address:duplex-list, value:offset
5:address:duplex-list <- next-duplex 1:address:duplex-list
6:number <- get *5:address:duplex-list, value:offset
7:address:duplex-list <- next-duplex 5:address:duplex-list
8:number <- get *7:address:duplex-list, value:offset
9:address:duplex-list <- next-duplex 7:address:duplex-list
]
memory-should-contain [
4 <- 13
6 <- 14
8 <- 15
9 <- 0
]
]
scenario remove-range-to-end [
1:address:duplex-list <- copy 0
1:address:duplex-list <- push-duplex 18, 1:address:duplex-list
1:address:duplex-list <- push-duplex 17, 1:address:duplex-list
1:address:duplex-list <- push-duplex 16, 1:address:duplex-list
1:address:duplex-list <- push-duplex 15, 1:address:duplex-list
1:address:duplex-list <- push-duplex 14, 1:address:duplex-list
1:address:duplex-list <- push-duplex 13, 1:address:duplex-list
run [
2:address:duplex-list <- next-duplex 1:address:duplex-list
3:address:duplex-list <- next-duplex 2:address:duplex-list
3:address:duplex-list <- next-duplex 3:address:duplex-list
3:address:duplex-list <- next-duplex 3:address:duplex-list
3:address:duplex-list <- next-duplex 3:address:duplex-list
remove-duplex-between 2:address:duplex-list, 3:address:duplex-list
4:number <- get *1:address:duplex-list, value:offset
5:address:duplex-list <- next-duplex 1:address:duplex-list
6:number <- get *5:address:duplex-list, value:offset
7:address:duplex-list <- next-duplex 5:address:duplex-list
8:number <- get *7:address:duplex-list, value:offset
9:address:duplex-list <- next-duplex 7:address:duplex-list
]
memory-should-contain [
4 <- 13
6 <- 14
8 <- 18
9 <- 0
]
]
scenario remove-range-empty [
1:address:duplex-list <- copy 0
1:address:duplex-list <- push-duplex 14, 1:address:duplex-list
1:address:duplex-list <- push-duplex 13, 1:address:duplex-list
run [
2:address:duplex-list <- next-duplex 1:address:duplex-list
remove-duplex-between 1:address:duplex-list, 2:address:duplex-list
4:number <- get *1:address:duplex-list, value:offset
5:address:duplex-list <- next-duplex 1:address:duplex-list
6:number <- get *5:address:duplex-list, value:offset
7:address:duplex-list <- next-duplex 5:address:duplex-list
]
memory-should-contain [
4 <- 13
6 <- 14
7 <- 0
]
]
recipe insert-duplex-range [
local-scope
in:address:duplex-list <- next-ingredient
start:address:duplex-list <- next-ingredient
reply-unless in, in
reply-unless start, in
end:address:duplex-list <- copy start
{
next:address:duplex-list <- next-duplex end
break-unless next
end <- copy next
loop
}
next:address:duplex-list <- next-duplex in
dest:address:address:duplex-list <- 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
reply in
]
recipe append-duplex [
local-scope
in:address:duplex-list <- next-ingredient
new:address:duplex-list <- next-ingredient
last:address:duplex-list <- last-duplex in
dest:address:address:duplex-list <- get-address *last, next:offset
*dest <- copy new
reply-unless new, in/same-as-ingredient:0
dest <- get-address *new, prev:offset
*dest <- copy last
reply in/same-as-ingredient:0
]
recipe last-duplex [
local-scope
in:address:duplex-list <- next-ingredient
result:address:duplex-list <- copy in
{
next:address:duplex-list <- next-duplex result
break-unless next
result <- copy next
loop
}
reply result
]
recipe dump-duplex-from [
local-scope
x:address:duplex-list <- next-ingredient
$print x, [: ]
{
break-unless x
c:character <- get *x, value:offset
$print c, [ ]
x <- next-duplex x
{
is-newline?:boolean <- equal c, 10/newline
break-unless is-newline?
$print 10/newline
$print x, [: ]
}
loop
}
$print 10/newline, [---], 10/newline
]