commit a2e1b80
shrub
·
2026-11-24 00:03:46 +0000 UTC
parent 15a9922
Stralloc
5 files changed,
+289,
-0
+35,
-0
1@@ -0,0 +1,35 @@
2+use <prelude.kav>
3+use <buffer.kav> as buf
4+use <stralloc.kav> as sa
5+
6+/* usage : ./targets < Makefile */
7+
8+fn target(out: buf::Buffer, line: !sa::Stralloc) -> buf::Buffer {
9+ if sa::at(line, 0) == '\t' then out
10+ else {
11+ val colon = sa::find(line, ':', 0)
12+ /* there's no colon */
13+ if colon == sa::len(line) then out
14+ else {
15+ val target = sa::sub(line, 0, colon)
16+ val out = buf::putsa(out, target) |> buf::putc('\n')
17+ val _ = sa::free(target)
18+ out
19+ }
20+ }
21+}
22+
23+fn loop(out: buf::Buffer, r: buf::Reader, line: sa::Stralloc) -> buf::Buffer {
24+ val (r, line, match) = buf::getln(r, line, '\n')
25+ if match == 0 then {
26+ val out = buf::putsa(out, line)
27+ val _ = buf::release(r)
28+ val _ = sa::free(line)
29+ out
30+ } else loop(target(out, line), r, line)
31+}
32+
33+pub fn main(w: World) -> World {
34+ val _ = loop(buf::stdout(w), buf::stdin(w), sa::new(0)) |> buf::close()
35+ w
36+}
+22,
-0
1@@ -3,6 +3,7 @@ use <bytes.kav> as bytes
2 use <fmt.kav> as fmt
3 use <rt.kav> as rt
4 use <fs.kav> as fs
5+use <stralloc.kav> as sa
6 use <mem.kav> as mem
7
8 extern fn kavstrlen(s: cstr) -> int
9@@ -128,6 +129,12 @@ pub fn puts(b: Buffer, s: cstr) -> Buffer {
10 put(b, s, kavstrlen(s))
11 }
12
13+/% add the contents of a Stralloc into the Buffer %/
14+pub fn putsa(b: Buffer, sa: !sa::Stralloc) -> Buffer {
15+ val ~Buffer(fd, len, m) = b
16+ copyaddr(fd, len, m, sa::addr(sa), 0, sa::len(sa))
17+}
18+
19 /% add one byte into the Buffer %/
20 pub fn putc(b: Buffer, c: int) -> Buffer {
21 val ~Buffer(fd, len, m) = b
22@@ -145,6 +152,21 @@ pub fn putnum(b: Buffer, x: int) -> Buffer {
23 }
24 }
25
26+fn getlnloop(r: Reader, sa: sa::Stralloc, sep: int) -> (Reader, sa::Stralloc, int) {
27+ val (r, c) = getc(r)
28+ if c < 0 then (r, sa, 0)
29+ else {
30+ val sa = sa::catc(sa, c)
31+ if c == sep then (r, sa, 1)
32+ else getlnloop(r, sa, sep)
33+ }
34+}
35+
36+/% read up to and including sep into sa. the int is 1 if sep was found, 0 at the end of input %/
37+pub fn getln(r: Reader, sa: sa::Stralloc, sep: int) -> (Reader, sa::Stralloc, int) {
38+ getlnloop(r, sa::clear(sa), sep)
39+}
40+
41 /% write out everything in the Buffer, but keep the Buffer %/
42 pub fn flush(b: Buffer) -> Buffer {
43 val ~Buffer(fd, len, m) = b
+8,
-0
1@@ -4,6 +4,7 @@ pub linear datatype Bytes[n: nat] {}
2
3 extern fn kavalloc[n: nat](len: int) -> Bytes[n]
4 extern fn kavfree[n: nat](m: Bytes[n]) -> one
5+extern fn kavrealloc[n: nat, m: nat](b: Bytes[n], len: int) -> Bytes[m]
6 extern fn kavaddr[n: nat](m: !Bytes[n]) -> ptr
7
8 /%
9@@ -13,6 +14,13 @@ pub fn alloc[n: nat](len: int) -> Bytes[n] {
10 kavalloc(len)
11 }
12
13+/%
14+resize a Bytes block to len bytes, keeping its contents.
15+%/
16+pub fn realloc[n: nat, m: nat](b: Bytes[n], len: int) -> Bytes[m] {
17+ kavrealloc(b, len)
18+}
19+
20 /%
21 free some Bytes.
22 %/
+213,
-0
1@@ -0,0 +1,213 @@
2+use <prelude.kav>
3+use <bytes.kav> as bytes
4+use <mem.kav> as mem
5+use <list.kav> as l
6+use <str.kav> as str
7+
8+pub abstract linear datatype Stralloc {
9+ | [n: nat] Stralloc(int, int, bytes::Bytes[n]) : Stralloc
10+}
11+
12+/% create a new Stralloc with room for cap bytes %/
13+pub fn new(cap: int) -> Stralloc {
14+ val m = bytes::alloc(cap)
15+ Stralloc(0, cap, m)
16+}
17+
18+/% free a Stralloc %/
19+pub fn free(sa: Stralloc) -> one {
20+ val ~Stralloc(len, cap, m) = sa
21+ bytes::free(m)
22+}
23+
24+/% empty a Stralloc, but keep the memory %/
25+pub fn clear(sa: Stralloc) -> Stralloc {
26+ val ~Stralloc(len, cap, m) = sa
27+ Stralloc(0, cap, m)
28+}
29+
30+/% append one byte to a Stralloc %/
31+pub fn catc(sa: Stralloc, c: int) -> Stralloc {
32+ val ~Stralloc(len, cap, m) = sa
33+ if len < cap then {
34+ val m = bytes::set(m, len, c as u8)
35+ Stralloc(len + 1, cap, m)
36+ }
37+ else {
38+ val newcap = cap * 2 + 16
39+ val m = bytes::realloc(m, newcap)
40+ catc(Stralloc(len, newcap, m), c)
41+ }
42+}
43+
44+/% return the length of a Stralloc %/
45+pub fn len(sa: !Stralloc) -> int {
46+ val Stralloc(len, cap, m) = sa
47+ len
48+}
49+
50+/% return the address where the Stralloc starts %/
51+pub fn addr(sa: !Stralloc) -> ptr {
52+ val Stralloc(len, cap, m) = sa
53+ bytes::addr(m)
54+}
55+
56+fn catsat(sa: Stralloc, s: cstr, i: int) -> Stralloc {
57+ val c = mem::load8(mem::add(s as ptr, i)) as int
58+ if c == 0 then sa else catsat(catc(sa, c), s, i + 1)
59+}
60+
61+/% append the cstr s to a Stralloc %/
62+pub fn cats(sa: Stralloc, s: cstr) -> Stralloc {
63+ catsat(sa, s, 0)
64+}
65+
66+/% return the byte at position i of a Stralloc %/
67+pub fn at(sa: !Stralloc, i: int) -> int {
68+ val Stralloc(len, cap, m) = sa
69+ if i < 0 then -1
70+ else if i >= len then -1
71+ else bytes::get(m, i) as int
72+}
73+
74+fn findat(sa: !Stralloc, c: int, i: int) -> int {
75+ if i >= len(sa) then len(sa)
76+ else if at(sa, i) == c then i
77+ else findat(sa, c, i + 1)
78+}
79+
80+/%
81+search a Stralloc for byte c starting at from and return where it appears.
82+if it isn't present, return the length of the Stralloc.
83+%/
84+pub fn find(sa: !Stralloc, c: int, from: int) -> int {
85+ findat(sa, c, from)
86+}
87+
88+fn subat(sa: !Stralloc, out: Stralloc, i: int, to: int) -> Stralloc {
89+ if i >= to then out
90+ else subat(sa, catc(out, at(sa, i)), i + 1, to)
91+}
92+
93+/%
94+Borrow a Stralloc and return a new Stralloc holding the bytes of sa from
95+from up to but not including to
96+%/
97+pub fn sub(sa: !Stralloc, from: int, to: int) -> Stralloc {
98+ val to = if to > len(sa) then len(sa) else to
99+ val from = if from < 0 then 0 else from
100+ if from >= to then new(0)
101+ else subat(sa, new(to - from), from, to)
102+}
103+
104+fn isspace(c: int) -> bool {
105+ if c == ' ' then true else c == '\t'
106+}
107+
108+fn skipspace(sa: !Stralloc, i: int) -> int {
109+ if i >= len(sa) then len(sa)
110+ else if isspace(at(sa, i)) then skipspace(sa, i + 1)
111+ else i
112+}
113+
114+fn wordend(sa: !Stralloc, i: int) -> int {
115+ if i >= len(sa) then len(sa)
116+ else if isspace(at(sa, i)) then i
117+ else wordend(sa, i + 1)
118+}
119+
120+fn splitat(sa: !Stralloc, i: int) -> [n: nat] l::List[Stralloc, n] {
121+ val start = skipspace(sa, i)
122+ if start >= len(sa) then l::Nil
123+ else {
124+ val end = wordend(sa, start)
125+ l::Cons(sub(sa, start, end), splitat(sa, end))
126+ }
127+}
128+
129+/% split a Stralloc into words at spaces and tabs and return a List of Strallocs %/
130+pub fn words(sa: !Stralloc) -> [n: nat] l::List[Stralloc, n] {
131+ splitat(sa, 0)
132+}
133+
134+/% free a list of Strallocs and everything in it %/
135+pub fn freeall[n: nat](xs: l::List[Stralloc, n]) -> one {
136+ case xs of {
137+ | ~l::Nil => one
138+ | ~l::Cons(x, rest) => {
139+ val _ = free(x)
140+ freeall(rest)
141+ }
142+ }
143+}
144+
145+fn cbyte(c: cstr, i: int) -> int {
146+ mem::load8(mem::add(c as ptr, i)) as int
147+}
148+
149+fn eqat(a: !Stralloc, b: !Stralloc, i: int) -> bool {
150+ if i >= len(a) then true
151+ else if at(a, i) != at(b, i) then false
152+ else eqat(a, b, i + 1)
153+}
154+
155+/% compare the bytes of two Strallocs %/
156+pub fn eq(a: !Stralloc, b: !Stralloc) -> bool {
157+ if len(a) != len(b) then false else eqat(a, b, 0)
158+}
159+
160+fn eqsat(sa: !Stralloc, c: cstr, i: int) -> bool {
161+ val cb = cbyte(c, i)
162+ if i >= len(sa) then cb == 0
163+ else if cb == 0 then false
164+ else if at(sa, i) != cb then false
165+ else eqsat(sa, c, i + 1)
166+}
167+
168+/% compare the bytes of a Stralloc and a cstr %/
169+pub fn eqs(sa: !Stralloc, c: cstr) -> bool {
170+ eqsat(sa, c, 0)
171+}
172+
173+fn startsat(sa: !Stralloc, c: cstr, i: int) -> bool {
174+ val cb = cbyte(c, i)
175+ if cb == 0 then true
176+ else if i >= len(sa) then false
177+ else if at(sa, i) != cb then false
178+ else startsat(sa, c, i + 1)
179+}
180+
181+/% check if a Stralloc begins with a cstr %/
182+pub fn starts(sa: !Stralloc, c: cstr) -> bool {
183+ startsat(sa, c, 0)
184+}
185+
186+fn endsat(sa: !Stralloc, c: cstr, off: int, i: int) -> bool {
187+ val cb = cbyte(c, i)
188+ if cb == 0 then true
189+ else if at(sa, off + i) != cb then false
190+ else endsat(sa, c, off, i + 1)
191+}
192+
193+/% check if a Stralloc ends with a cstr %/
194+pub fn ends(sa: !Stralloc, c: cstr) -> bool {
195+ val n = str::len(c)
196+ if n > len(sa) then false else endsat(sa, c, len(sa) - n, 0)
197+}
198+
199+fn catsaat(a: Stralloc, b: !Stralloc, i: int) -> Stralloc {
200+ if i >= len(b) then a else catsaat(catc(a, at(b, i)), b, i + 1)
201+}
202+
203+/% append the bytes of b to a %/
204+pub fn catsa(a: Stralloc, b: !Stralloc) -> Stralloc {
205+ catsaat(a, b, 0)
206+}
207+
208+/% keep only the first n bytes of a Stralloc %/
209+pub fn truncate(sa: Stralloc, n: int) -> Stralloc {
210+ val ~Stralloc(len, cap, m) = sa
211+ if n < 0 then Stralloc(0, cap, m)
212+ else if n < len then Stralloc(n, cap, m)
213+ else Stralloc(len, cap, m)
214+}
M
qbe.c
+11,
-0
1@@ -941,6 +941,17 @@ static const char runtime[] =
2 "\tret %p\n"
3 "}\n"
4 "\n"
5+ "section \".text.kavrealloc\" \"ax\"\n"
6+ "function l $kavrealloc(l %p, l %n) {\n"
7+ "@start\n"
8+ "\t%q =l call $realloc(l %p, l %n)\n"
9+ "\tjnz %q, @ok, @fail\n"
10+ "@fail\n"
11+ "\thlt\n"
12+ "@ok\n"
13+ "\tret %q\n"
14+ "}\n"
15+ "\n"
16 "section \".text.kavptradd\" \"ax\"\n"
17 "function l $kavptradd(l %p, l %n) {\n"
18 "@start\n"