Landin library reference discuss source

compiler/tests/fixtures/runtime/failure-words-report/main.ldn

1--  A program that reports its failures to a person, as `path: reason`
2--  lines on standard error, with the library's own words for each reason:
3--  a file that is not there, a count that is not a number and one too
4--  large for its type.  It ends with status 1, as a tool that failed does.
5import core/failure
6import core/io
7import core/text
8import hosted/system
9
10--  `subject: reason` and a line ending, on standard error.
11report: (host: system.host, subject: []u8, reason: failure.reason) -> none =
12    stream: io.file = io.err(host)
13    io.write(host, stream, subject) else none
14    io.write(host, stream, ": ") else none
15    io.write(host, stream, failure.words(reason)) else none
16    io.write(host, stream, "\n") else none
17end report
18
19--  Read the file at `path`, reporting why it cannot be opened.
20opened: (host: system.host, path: []u8) -> (yes: bool) =
21    yes = false
22    file := io.open_read(host, text.host_over(path)) else (problem)
23        report(host, path, problem)
24        return
25    end
26    io.close(host, file) else none
27    yes = true
28end opened
29
30--  Read `count` as a byte count, reporting why it is not one.
31counted: (host: system.host, count: []u8) -> (yes: bool) =
32    yes = false
33    _ = text.parse_u8(count) else (problem)
34        report(host, count, problem)
35        return
36    end
37    yes = true
38end counted
39
40public main: () -> (code: i32) =
41    host := system.new()
42    code = 0
43    missing: bool = not opened(host, "landin-absent-notes.txt")
44    not_number: bool = not counted(host, "twelve")
45    too_large: bool = not counted(host, "300")
46    if missing and not_number and too_large then
47        code = 1
48    end if
49end main