2016-07-28 05:33:18 +00:00
|
|
|
# new type to help incrementally scan arrays
|
|
|
|
container stream:_elem [
|
2016-09-17 17:28:25 +00:00
|
|
|
index:num
|
2016-09-17 20:00:39 +00:00
|
|
|
data:&:@:_elem
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2016-09-17 20:00:39 +00:00
|
|
|
def new-stream s:&:@:_elem -> result:&:stream:_elem [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2018-06-17 18:20:53 +00:00
|
|
|
return-unless s, null
|
2016-07-28 05:33:18 +00:00
|
|
|
result <- new {(stream _elem): type}
|
2016-04-16 18:47:41 +00:00
|
|
|
*result <- put *result, index:offset, 0
|
|
|
|
*result <- put *result, data:offset, s
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2016-09-17 19:55:10 +00:00
|
|
|
def rewind in:&:stream:_elem -> in:&:stream:_elem [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-04-13 15:49:05 +00:00
|
|
|
return-unless in
|
2016-04-16 18:47:41 +00:00
|
|
|
*in <- put *in, index:offset, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2016-09-17 19:55:10 +00:00
|
|
|
def read in:&:stream:_elem -> result:_elem, empty?:bool, in:&:stream:_elem [
|
2016-07-23 02:56:18 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-04-13 15:49:05 +00:00
|
|
|
assert in, [cannot read; stream has no data]
|
2018-06-17 07:05:38 +00:00
|
|
|
empty? <- copy false
|
2016-09-17 17:28:25 +00:00
|
|
|
idx:num <- get *in, index:offset
|
2016-09-17 20:00:39 +00:00
|
|
|
s:&:@:_elem <- get *in, data:offset
|
2016-09-17 17:28:25 +00:00
|
|
|
len:num <- length *s
|
2016-09-17 17:30:24 +00:00
|
|
|
at-end?:bool <- greater-or-equal idx len
|
2016-07-28 05:33:18 +00:00
|
|
|
{
|
|
|
|
break-unless at-end?
|
2016-09-17 19:55:10 +00:00
|
|
|
empty-result:&:_elem <- new _elem:type
|
2018-06-17 07:05:38 +00:00
|
|
|
return *empty-result, true
|
2016-07-28 05:33:18 +00:00
|
|
|
}
|
2016-07-23 02:56:18 +00:00
|
|
|
result <- index *s, idx
|
|
|
|
idx <- add idx, 1
|
|
|
|
*in <- put *in, index:offset, idx
|
|
|
|
]
|
|
|
|
|
2016-09-17 19:55:10 +00:00
|
|
|
def peek in:&:stream:_elem -> result:_elem, empty?:bool [
|
2016-07-23 03:28:51 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-04-13 15:49:05 +00:00
|
|
|
assert in, [cannot peek; stream has no data]
|
2018-06-17 07:05:38 +00:00
|
|
|
empty?:bool <- copy false
|
2016-09-17 17:28:25 +00:00
|
|
|
idx:num <- get *in, index:offset
|
2016-09-17 20:00:39 +00:00
|
|
|
s:&:@:_elem <- get *in, data:offset
|
2016-09-17 17:28:25 +00:00
|
|
|
len:num <- length *s
|
2016-09-17 17:30:24 +00:00
|
|
|
at-end?:bool <- greater-or-equal idx len
|
2016-07-28 05:33:18 +00:00
|
|
|
{
|
|
|
|
break-unless at-end?
|
2016-09-17 19:55:10 +00:00
|
|
|
empty-result:&:_elem <- new _elem:type
|
2018-06-17 07:05:38 +00:00
|
|
|
return *empty-result, true
|
2016-07-28 05:33:18 +00:00
|
|
|
}
|
2016-07-23 03:28:51 +00:00
|
|
|
result <- index *s, idx
|
|
|
|
]
|
|
|
|
|
2016-09-17 19:55:10 +00:00
|
|
|
def read-line in:&:stream:char -> result:text, in:&:stream:char [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-04-13 15:49:05 +00:00
|
|
|
assert in, [cannot read-line; stream has no data]
|
2016-09-17 17:28:25 +00:00
|
|
|
idx:num <- get *in, index:offset
|
2016-09-12 07:06:40 +00:00
|
|
|
s:text <- get *in, data:offset
|
2016-09-17 17:28:25 +00:00
|
|
|
next-idx:num <- find-next s, 10/newline, idx
|
2016-04-16 18:47:41 +00:00
|
|
|
result <- copy-range s, idx, next-idx
|
|
|
|
idx <- add next-idx, 1 # skip newline
|
|
|
|
# write back
|
|
|
|
*in <- put *in, index:offset, idx
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2016-09-17 19:55:10 +00:00
|
|
|
def end-of-stream? in:&:stream:_elem -> result:bool [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-04-13 15:49:05 +00:00
|
|
|
assert in, [cannot check end-of-stream?; stream has no data]
|
2016-09-17 17:28:25 +00:00
|
|
|
idx:num <- get *in, index:offset
|
2016-09-17 20:00:39 +00:00
|
|
|
s:&:@:_elem <- get *in, data:offset
|
2016-09-17 17:28:25 +00:00
|
|
|
len:num <- length *s
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- greater-or-equal idx, len
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|