mu/066stream.mu

81 lines
2.0 KiB
Plaintext
Raw Normal View History

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
]
2016-09-17 20:00:39 +00:00
def new-stream s:&:@:_elem -> result:&:stream:_elem [
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}
*result <- put *result, index:offset, 0
*result <- put *result, data:offset, s
]
2016-09-17 19:55:10 +00:00
def rewind in:&:stream:_elem -> in:&:stream:_elem [
local-scope
2017-12-04 07:25:40 +00:00
load-inputs
return-unless in
*in <- put *in, index:offset, 0
]
2016-09-17 19:55:10 +00:00
def read in:&:stream:_elem -> result:_elem, empty?:bool, in:&:stream:_elem [
local-scope
2017-12-04 07:25:40 +00:00
load-inputs
assert in, [cannot read; stream has no data]
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
return *empty-result, true
2016-07-28 05:33: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 [
local-scope
2017-12-04 07:25:40 +00:00
load-inputs
assert in, [cannot peek; stream has no data]
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
return *empty-result, true
2016-07-28 05:33:18 +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 [
local-scope
2017-12-04 07:25:40 +00:00
load-inputs
assert in, [cannot read-line; stream has no data]
2016-09-17 17:28:25 +00:00
idx:num <- get *in, index:offset
s:text <- get *in, data:offset
2016-09-17 17:28:25 +00:00
next-idx:num <- find-next s, 10/newline, idx
result <- copy-range s, idx, next-idx
idx <- add next-idx, 1 # skip newline
# write back
*in <- put *in, index:offset, idx
]
2016-09-17 19:55:10 +00:00
def end-of-stream? in:&:stream:_elem -> result:bool [
local-scope
2017-12-04 07:25:40 +00:00
load-inputs
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
]