commit 770f648
shrub
·
2026-11-24 16:00:28 +0000 UTC
parent a2e1b80
use result for errors, add mkdir rmdir chdir exit
4 files changed,
+53,
-14
+8,
-8
1@@ -1,6 +1,6 @@
2 use <prelude.kav>
3 use <bytes.kav> as bytes
4-use <either.kav> as eth
5+use <result.kav> as res
6 use <str.kav> as str
7 use <mem.kav> as mem
8
9@@ -34,20 +34,20 @@ pub fn int[n: nat](m: bytes::Bytes[n], off: int, x: int) -> (bytes::Bytes[n], in
10 } else u64(m, off, x as u64)
11 }
12
13-fn scandigits(s: cstr, i: int, acc: int) -> eth::Either[int, int] {
14+fn scandigits(s: cstr, i: int, acc: int) -> res::Result[int, int] {
15 val c = mem::load8(mem::add(s as ptr, i)) as int
16- if c == 0 then eth::Right(acc)
17- else if c < '0' then eth::Left(0)
18- else if c > '9' then eth::Left(0)
19+ if c == 0 then res::Ok(acc)
20+ else if c < '0' then res::Err(0)
21+ else if c > '9' then res::Err(0)
22 else scandigits(s, i + 1, acc * 10 + (c - '0'))
23 }
24
25 /%
26-Get a decimal number from s, Left if s isn't one
27+Get a decimal number from s, Err if s isn't one
28
29 eg "67" (cstr) -> 67 (int)
30 %/
31-pub fn scan(s: cstr) -> eth::Either[int, int] {
32- if str::len(s) == 0 then eth::Left(0)
33+pub fn scan(s: cstr) -> res::Result[int, int] {
34+ if str::len(s) == 0 then res::Err(0)
35 else scandigits(s, 0, 0)
36 }
+16,
-6
1@@ -1,6 +1,6 @@
2 use <prelude.kav>
3 use <rt.kav> as rt
4-use <either.kav> as eth
5+use <result.kav> as res
6
7 pub abstract linear datatype File {
8 | File(int)
9@@ -11,20 +11,20 @@ pub fn fd(f: !File) -> int {
10 fd
11 }
12
13-fn open(w: !World, path: cstr, flags: int, mode: int) -> eth::Either[int, File] {
14+fn open(w: !World, path: cstr, flags: int, mode: int) -> res::Result[File, int] {
15 val fd = rt::open(path, flags, mode)
16- if fd < 0 then eth::Left(0 - fd) else eth::Right(File(fd))
17+ if fd < 0 then res::Err(0 - fd) else res::Ok(File(fd))
18 }
19
20-pub fn openread(w: !World, path: cstr) -> eth::Either[int, File] {
21+pub fn openread(w: !World, path: cstr) -> res::Result[File, int] {
22 open(w, path, 0, 0)
23 }
24
25-pub fn opentrunc(w: !World, path: cstr) -> eth::Either[int, File] {
26+pub fn opentrunc(w: !World, path: cstr) -> res::Result[File, int] {
27 open(w, path, 1 + 64 + 512, 420)
28 }
29
30-pub fn openappend(w: !World, path: cstr) -> eth::Either[int, File] {
31+pub fn openappend(w: !World, path: cstr) -> res::Result[File, int] {
32 open(w, path, 1 + 64 + 1024, 420)
33 }
34
35@@ -49,3 +49,13 @@ pub fn write(f: !File, p: ptr, n: int) -> int {
36 val File(fd) = f
37 rt::write(fd, p, n)
38 }
39+
40+pub fn mkdir(w: !World, path: cstr, mode: int) -> res::Result[one, int] {
41+ val r = rt::mkdir(path, mode)
42+ if r < 0 then res::Err(0 - r) else res::Ok(one)
43+}
44+
45+pub fn rmdir(w: !World, path: cstr) -> res::Result[one, int] {
46+ val r = rt::rmdir(path)
47+ if r < 0 then res::Err(0 - r) else res::Ok(one)
48+}
+13,
-0
1@@ -1,5 +1,7 @@
2 use <prelude.kav>
3 use <list.kav>
4+use <rt.kav> as rt
5+use <result.kav> as res
6
7 extern fn kavgetargc() -> int
8 extern fn kavgetarg(i: int) -> cstr
9@@ -25,3 +27,14 @@ fn build[k: nat](i: int, acc: List[cstr, k]) -> [m: nat] List[cstr, m] {
10 pub fn args(w: !World) -> [n: nat] List[cstr, n] {
11 build(argc(w) - 1, Nil)
12 }
13+
14+/% Change the current directory %/
15+pub fn chdir(w: !World, path: cstr) -> res::Result[one, int] {
16+ val r = rt::chdir(path)
17+ if r < 0 then res::Err(0 - r) else res::Ok(one)
18+}
19+
20+pub fn exit(w: World, code: int) -> World {
21+ val _ = rt::exit(code)
22+ w
23+}
+16,
-0
1@@ -15,3 +15,19 @@ pub fn open(path: cstr, flags: int, mode: int) -> int {
2 pub fn close(fd: int) -> int {
3 kavsyscall3(3, fd, 0, 0)
4 }
5+
6+pub fn mkdir(path: cstr, mode: int) -> int {
7+ kavsyscall3(83, path as int, mode, 0)
8+}
9+
10+pub fn rmdir(path: cstr) -> int {
11+ kavsyscall3(84, path as int, 0, 0)
12+}
13+
14+pub fn chdir(path: cstr) -> int {
15+ kavsyscall3(80, path as int, 0, 0)
16+}
17+
18+pub fn exit(code: int) -> int {
19+ kavsyscall3(231, code, 0, 0)
20+}