bitwise shrub/kav / examples / targets.kav
 1use <prelude.kav>
 2use <buffer.kav> as buf
 3use <stralloc.kav> as sa
 4
 5/* usage : ./targets < Makefile */
 6
 7fn target(out: buf::Buffer, line: !sa::Stralloc) -> buf::Buffer {
 8    if sa::at(line, 0) == '\t' then out
 9    else {
10        val colon = sa::find(line, ':', 0)
11        /* there's no colon */
12        if colon == sa::len(line) then out
13        else {
14            val target = sa::sub(line, 0, colon)
15            val out = buf::putsa(out, target) |> buf::putc('\n')
16            val _ = sa::free(target)
17            out
18         }
19    }
20}
21
22fn loop(out: buf::Buffer, r: buf::Reader, line: sa::Stralloc) -> buf::Buffer {
23    val (r, line, match) = buf::getln(r, line, '\n')
24    if match == 0 then {
25        val out = buf::putsa(out, line)
26        val _ = buf::release(r)
27        val _ = sa::free(line)
28        out
29    } else loop(target(out, line), r, line)
30}
31
32pub fn main(w: World) -> World {
33    val _ = loop(buf::stdout(w), buf::stdin(w), sa::new(0)) |> buf::close()
34    w
35}