-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio.nwt
66 lines (58 loc) · 1.12 KB
/
io.nwt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import list
binary_read_mode := @stream_mode with { read = true, binary = true }
binary_write_mode := @stream_mode with { write = true, binary = true }
binary_overwrite_mode := @stream_mode with { write = true, binary = true }
stream {
source:string,
mode:stream_mode,
handle:int,
teardown := (this:stream?) -> error_list? {
match (this)
value {
return close(value.handle)
}
| nil {
return nil
}
}
}
stream_setup_result {
setup_value:stream
| setup_errors:error_list
}
open_stream := (path:string, mode:stream_mode) -> stream_setup_result {
open_result := open(path, mode)
match (open_result)
data {
return @stream with {
source = path,
mode = mode,
handle = data
}
}
| errors {
return errors
}
}
read := (s:stream) -> byte_iter? {
f := () -> byte_iter? {
result := get(s.handle)
match(result)
data {
return @byte_iter with {
data = data,
next = f
}
} | errors {
return @byte_iter with {
data = errors
}
} | eof {
return nil
}
}
return f()
}
write := (s:stream, value:byte) -> error_list? {
return put(s.handle, value)
}