2015-05-19 17:36:46 +00:00
|
|
|
# A list links up multiple objects together to make them easier to manage.
|
|
|
|
#
|
2015-11-04 20:43:37 +00:00
|
|
|
# The objects must be of the same type. If you want to store multiple types in
|
|
|
|
# a single list, use an exclusive-container.
|
2015-05-19 17:36:46 +00:00
|
|
|
|
2015-11-04 20:43:37 +00:00
|
|
|
container list:_elem [
|
|
|
|
value:_elem
|
2016-04-24 18:54:30 +00:00
|
|
|
next:address:list:_elem
|
2015-05-16 06:00:16 +00:00
|
|
|
]
|
|
|
|
|
2016-05-20 18:59:02 +00:00
|
|
|
def push x:_elem, in:address:list:_elem -> result:address:list:_elem [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-04 20:43:37 +00:00
|
|
|
load-ingredients
|
2016-05-20 18:59:02 +00:00
|
|
|
result <- new {(list _elem): type}
|
2016-04-16 15:04:56 +00:00
|
|
|
*result <- merge x, in
|
2015-05-16 06:00:16 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def first in:address:list:_elem -> result:_elem [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-04 20:43:37 +00:00
|
|
|
load-ingredients
|
|
|
|
result <- get *in, value:offset
|
2015-05-16 06:00:16 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def rest in:address:list:_elem -> result:address:list:_elem/contained-in:in [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-04 20:43:37 +00:00
|
|
|
load-ingredients
|
|
|
|
result <- get *in, next:offset
|
|
|
|
]
|
|
|
|
|
2015-05-16 06:00:16 +00:00
|
|
|
scenario list-handling [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:list:number <- push 3, 0
|
|
|
|
1:address:list:number <- push 4, 1:address:list:number
|
|
|
|
1:address:list:number <- push 5, 1:address:list:number
|
|
|
|
2:number <- first 1:address:list:number
|
|
|
|
1:address:list:number <- rest 1:address:list:number
|
|
|
|
3:number <- first 1:address:list:number
|
|
|
|
1:address:list:number <- rest 1:address:list:number
|
|
|
|
4:number <- first 1:address:list:number
|
|
|
|
1:address:list:number <- rest 1:address:list:number
|
2015-05-16 06:00:16 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
1 <- 0 # empty to empty, dust to dust..
|
|
|
|
2 <- 5
|
|
|
|
3 <- 4
|
|
|
|
4 <- 3
|
|
|
|
]
|
|
|
|
]
|
2015-11-28 05:38:09 +00:00
|
|
|
|
2016-05-20 19:47:18 +00:00
|
|
|
def length l:address:list:_elem -> result:number [
|
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
return-unless l, 0
|
|
|
|
rest:address:list:_elem <- rest l
|
|
|
|
length-of-rest:number <- length rest
|
|
|
|
result <- add length-of-rest, 1
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text in:address:list:_elem -> result:address:array:character [
|
2015-11-28 05:38:09 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-04-24 18:54:30 +00:00
|
|
|
buf:address:buffer <- new-buffer 80
|
2015-11-28 05:38:09 +00:00
|
|
|
buf <- to-buffer in, buf
|
|
|
|
result <- buffer-to-array buf
|
|
|
|
]
|
|
|
|
|
2015-11-29 09:00:30 +00:00
|
|
|
# variant of 'to-text' which stops printing after a few elements (and so is robust to cycles)
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text-line in:address:list:_elem -> result:address:array:character [
|
2015-11-29 09:00:30 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-04-24 18:54:30 +00:00
|
|
|
buf:address:buffer <- new-buffer 80
|
2015-11-29 09:00:30 +00:00
|
|
|
buf <- to-buffer in, buf, 6 # max elements to display
|
|
|
|
result <- buffer-to-array buf
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-buffer in:address:list:_elem, buf:address:buffer -> buf:address:buffer [
|
2015-11-28 05:38:09 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
{
|
|
|
|
break-if in
|
|
|
|
buf <- append buf, 48/0
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-11-28 05:38:09 +00:00
|
|
|
}
|
|
|
|
# append in.value to buf
|
|
|
|
val:_elem <- get *in, value:offset
|
|
|
|
buf <- append buf, val
|
|
|
|
# now prepare next
|
2016-04-24 18:54:30 +00:00
|
|
|
next:address:list:_elem <- rest in
|
2015-11-28 05:38:09 +00:00
|
|
|
nextn:number <- copy next
|
2016-03-08 09:30:14 +00:00
|
|
|
return-unless next
|
2016-03-14 20:00:21 +00:00
|
|
|
buf <- append buf, [ -> ]
|
2015-11-28 05:38:09 +00:00
|
|
|
# and recurse
|
2015-11-29 09:00:30 +00:00
|
|
|
remaining:number, optional-ingredient-found?:boolean <- next-ingredient
|
|
|
|
{
|
|
|
|
break-if optional-ingredient-found?
|
|
|
|
# unlimited recursion
|
|
|
|
buf <- to-buffer next, buf
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-11-29 09:00:30 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
break-unless remaining
|
|
|
|
# limited recursion
|
|
|
|
remaining <- subtract remaining, 1
|
|
|
|
buf <- to-buffer next, buf, remaining
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-11-29 09:00:30 +00:00
|
|
|
}
|
|
|
|
# past recursion depth; insert ellipses and stop
|
2016-03-14 20:00:21 +00:00
|
|
|
append buf, [...]
|
2015-11-28 05:38:09 +00:00
|
|
|
]
|