commit 50b6ce5
shrub
·
2026-11-23 03:04:17 +0000 UTC
parent 0ae9a6e
readers
6 files changed,
+175,
-6
+17,
-0
1@@ -0,0 +1,17 @@
2+use <prelude.kav>
3+use <buffer.kav> as buf
4+use <fs.kav> as fs
5+use <either.kav> as eth
6+
7+pub fn main(w: World) -> World {
8+ val _ =
9+ case fs::openread(w, "examples/hello.kav") of {
10+ | ~eth::Right(f) => buf::stdout(w)
11+ |> buf::copy(buf::fromfile(f))
12+ |> buf::close()
13+ | ~eth::Left(e) => buf::stderr(w)
14+ |> buf::puts("unable to open file\n")
15+ |> buf::close()
16+ }
17+ w
18+}
+62,
-5
1@@ -2,6 +2,7 @@ use <prelude.kav>
2 use <bytes.kav> as bytes
3 use <fmt.kav> as fmt
4 use <rt.kav> as rt
5+use <fs.kav> as fs
6
7 extern fn kavload8(p: int) -> u8
8 extern fn kavstrlen(s: cstr) -> int
9@@ -10,6 +11,11 @@ pub linear datatype Buffer {
10 | Buffer(int, int, bytes::Bytes[4096])
11 }
12
13+pub linear datatype Reader {
14+ /* read, close, the fd, the next unread byte, where the data read ends, the storage */
15+ | Reader(fn(int, int, int) -> int, fn(int) -> int, int, int, int, bytes::Bytes[4096])
16+}
17+
18 fn cap() -> int {
19 4096
20 }
21@@ -19,6 +25,57 @@ pub fn new(w: !World, fd: int) -> Buffer {
22 Buffer(fd, 0, bytes::alloc(cap()))
23 }
24
25+fn keep(fd: int) -> int {
26+ 0
27+}
28+
29+/% read from fd, which the caller still owns %/
30+pub fn reader(w: !World, fd: int) -> Reader {
31+ Reader(rt::read, keep, fd, 0, 0, bytes::alloc(cap()))
32+}
33+
34+/% read from a File, which the Reader now owns and closes on release %/
35+pub fn fromfile(f: fs::File) -> Reader {
36+ val ~fs::File(fd) = f
37+ Reader(rt::read, rt::close, fd, 0, 0, bytes::alloc(cap()))
38+}
39+
40+pub fn stdin(w: !World) -> Reader {
41+ reader(w, 0)
42+}
43+
44+/% make sure the Reader has unread bytes, returning how many: 0 at end of file, negative on error %/
45+pub fn feed(r: Reader) -> (Reader, int) {
46+ val ~Reader(op, cl, fd, pos, len, m) = r
47+ if pos < len then (Reader(op, cl, fd, pos, len, m), len - pos)
48+ else {
49+ val n = op(fd, bytes::addr(m), cap())
50+ if n <= 0 then (Reader(op, cl, fd, 0, 0, m), n)
51+ else (Reader(op, cl, fd, 0, n, m), n)
52+ }
53+}
54+
55+/% move everything left in the Reader into the Buffer, then release the Reader %/
56+pub fn copy(out: Buffer, in: Reader) -> Buffer {
57+ val (in, n) = feed(in)
58+ if n <= 0 then {
59+ val _ = release(in)
60+ out
61+ } else {
62+ val ~Reader(op, cl, fd, pos, len, m) = in
63+ val ~Buffer(ofd, olen, om) = out
64+ val out = copyaddr(ofd, olen, om, bytes::addr(m) + pos, 0, len - pos)
65+ copy(out, Reader(op, cl, fd, len, len, m))
66+ }
67+}
68+
69+/% free the Reader, closing its fd if it owns it %/
70+pub fn release(r: Reader) -> one {
71+ val ~Reader(op, cl, fd, pos, len, m) = r
72+ val _ = cl(fd)
73+ bytes::free(m)
74+}
75+
76 /% create a new Buffer on stdout %/
77 pub fn stdout(w: !World) -> Buffer {
78 new(w, 1)
79@@ -43,18 +100,18 @@ fn drain(fd: int, m: bytes::Bytes[4096], len: int) -> bytes::Bytes[4096] {
80 }
81
82 /%
83-Move bytes from a cstr into a Buffer's storage one byte a time, flushing when it fills up.
84+Move n bytes from address p into a Buffer's storage one byte a time, flushing when it fills up.
85 %/
86-fn copy(fd: int, len: int, m: bytes::Bytes[4096], s: cstr, i: int, n: int) -> Buffer {
87+fn copyaddr(fd: int, len: int, m: bytes::Bytes[4096], p: int, i: int, n: int) -> Buffer {
88 if i == n then Buffer(fd, len, m)
89- else if len == cap() then copy(fd, 0, drain(fd, m, len), s, i, n)
90- else copy(fd, len + 1, bytes::set(m, len, kavload8(s as int + i)), s, i + 1, n)
91+ else if len == cap() then copyaddr(fd, 0, drain(fd, m, len), p, i, n)
92+ else copyaddr(fd, len + 1, bytes::set(m, len, kavload8(p + i)), p, i + 1, n)
93 }
94
95 /% add n bytes from s into a Buffer %/
96 pub fn put(b: Buffer, s: cstr, n: int) -> Buffer {
97 val ~Buffer(fd, len, m) = b
98- copy(fd, len, m, s, 0, n)
99+ copyaddr(fd, len, m, s as int, 0, n)
100 }
101
102 /% add a whole cstr into the Buffer %/
+4,
-0
1@@ -0,0 +1,4 @@
2+pub linear datatype Either[a: type, b: type] {
3+ | Left(a)
4+ | Right(b)
5+}
+45,
-0
1@@ -0,0 +1,45 @@
2+use <prelude.kav>
3+use <rt.kav> as rt
4+use <either.kav> as eth
5+
6+pub linear datatype File {
7+ | File(int)
8+}
9+
10+pub fn fd(f: !File) -> int {
11+ val File(fd) = f
12+ fd
13+}
14+
15+fn open(w: !World, path: cstr, flags: int, mode: int) -> eth::Either[int, File] {
16+ val fd = rt::open(path, flags, mode)
17+ if fd < 0 then eth::Left(0 - fd) else eth::Right(File(fd))
18+}
19+
20+pub fn openread(w: !World, path: cstr) -> eth::Either[int, File] {
21+ open(w, path, 0, 0)
22+}
23+
24+pub fn opentrunc(w: !World, path: cstr) -> eth::Either[int, File] {
25+ open(w, path, 1 + 64 + 512, 420)
26+}
27+
28+pub fn openappend(w: !World, path: cstr) -> eth::Either[int, File] {
29+ open(w, path, 1 + 64 + 1024, 420)
30+}
31+
32+pub fn close(f: File) -> int {
33+ val ~File(fd) = f
34+ rt::close(fd)
35+}
36+
37+pub fn read(f: !File, p: int, n: int) -> int {
38+ val File(fd) = f
39+ rt::read(fd, p, n)
40+}
41+
42+
43+pub fn write(f: !File, p: int, n: int) -> int {
44+ val File(fd) = f
45+ rt::write(fd, p, n)
46+}
+12,
-0
1@@ -3,3 +3,15 @@ extern fn kavsyscall3(n: int, a: int, b: int, c: int) -> int
2 pub fn write(fd: int, p: int, n: int) -> int {
3 kavsyscall3(1, fd, p, n)
4 }
5+
6+pub fn read(fd: int, p: int, n: int) -> int {
7+ kavsyscall3(0, fd, p, n)
8+}
9+
10+pub fn open(path: cstr, flags: int, mode: int) -> int {
11+ kavsyscall3(2, path as int, flags, mode)
12+}
13+
14+pub fn close(fd: int) -> int {
15+ kavsyscall3(3, fd, 0, 0)
16+}
M
linear.c
+35,
-1
1@@ -184,6 +184,18 @@ hastilde(struct pat *p) {
2 return 0;
3 }
4
5+static int
6+haslinear(struct pat *p) {
7+ struct pat *a;
8+
9+ if(p->kind == PNAME)
10+ return islinear(p->binding->type);
11+ for(a = p->args; a; a = a->next)
12+ if(haslinear(a))
13+ return 1;
14+ return 0;
15+}
16+
17 static struct lvar *
18 findvar(struct expr *e) {
19 struct lvar *v;
20@@ -232,6 +244,8 @@ use(struct expr *e) {
21 int tilde = 0;
22 int old;
23 struct type *ps;
24+ struct lvar **locked;
25+ usize nlocked;
26
27 switch(e->kind) {
28 case EIDENT:
29@@ -272,11 +286,31 @@ use(struct expr *e) {
30 break;
31 case EBLOCK:
32 mark = vars;
33+ nlocked = 0;
34+ for(l = e->vals; l; l = l->next)
35+ nlocked++;
36+ locked = nlocked ? alloc(nlocked * sizeof(*locked)) : nil;
37+ nlocked = 0;
38 for(l = e->vals; l; l = l->next) {
39- use(l->init);
40+ v = nil;
41+ if(l->pat->kind == PCON && !l->pat->tilde)
42+ v = findvar(l->init);
43+ if(!v) {
44+ use(l->init);
45+ trackpat(l->pat, 0);
46+ continue;
47+ }
48+ /* val C(x) = v without ~ borrows v like a case does */
49+ look(l->init);
50 trackpat(l->pat, 0);
51+ if(haslinear(l->pat) && !v->borrowed) {
52+ locked[nlocked++] = v;
53+ v->borrowed = 1;
54+ }
55 }
56 use(e->body);
57+ while(nlocked > 0)
58+ locked[--nlocked]->borrowed = 0;
59 endscope(mark);
60 break;
61 case ECASE: