From 9f5c5a9acf98b220c73c1c85d57532b5582b22f4 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 24 Feb 2025 02:20:02 +0100 Subject: [PATCH] Update some examples. --- resources/examples/binarydigits.c3 | 1 + resources/examples/binarydigits7.c3 | 24 +++++ resources/examples/contextfree/boolerr7.c3 | 101 +++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 resources/examples/binarydigits7.c3 create mode 100644 resources/examples/contextfree/boolerr7.c3 diff --git a/resources/examples/binarydigits.c3 b/resources/examples/binarydigits.c3 index f2588549f..914f4df80 100644 --- a/resources/examples/binarydigits.c3 +++ b/resources/examples/binarydigits.c3 @@ -15,6 +15,7 @@ fn DString bin(int x) { int bits = x == 0 ? 1 : 1 + (int)math::log2(x); DString str; + str.new_init(); str.append_repeat('0', bits); for (int i = 0; i < bits; i++) { diff --git a/resources/examples/binarydigits7.c3 b/resources/examples/binarydigits7.c3 new file mode 100644 index 000000000..ab4463890 --- /dev/null +++ b/resources/examples/binarydigits7.c3 @@ -0,0 +1,24 @@ +module binarydigits; +import std::math; +import std::io; +fn void main() +{ + for (int i = 0; i < 20; i++) + { + DString s = bin(i); + io::printf("%s\n", s); + } +} + +fn DString bin(int x) +{ + int bits = x == 0 ? 1 : 1 + (int)math::log2(x); + DString str; + str.append_repeat('0', bits); + for (int i = 0; i < bits; i++) + { + str.set((usz)(bits - i - 1), x & 1 ? '1' : '0'); + x >>= 1; + } + return str; +} \ No newline at end of file diff --git a/resources/examples/contextfree/boolerr7.c3 b/resources/examples/contextfree/boolerr7.c3 new file mode 100644 index 000000000..eefc8223b --- /dev/null +++ b/resources/examples/contextfree/boolerr7.c3 @@ -0,0 +1,101 @@ + +module test; +import libc; +import std::io; +import std::collections::maybe; + +fault TitleResult +{ + TITLE_MISSING +} + +fault ReadError +{ + BAD_READ, +} + +struct Doc +{ + Maybe { Head } head; +} + +struct Head +{ + Maybe { String } title; +} + +struct Summary +{ + Maybe { String } title; + bool ok; +} + +fn void! Summary.print(Summary* s, OutStream out) +{ + io::fprintf(out, "Summary({ .title = %s, .ok = %s})", s.title.get() ?? "missing", s.ok)!; +} + +fn Doc! read_doc(String url) +{ + if (url.contains("fail")) return ReadError.BAD_READ?; + if (url.contains("head-missing")) return { }; + if (url.contains("title-missing")) return { .head = maybe::value{Head}({}) }; + if (url.contains("title-empty")) return { .head = maybe::value{Head}({ .title = maybe::value{String}("")}) }; + return { .head = maybe::value{Head}({ .title = maybe::value{String}(string::format(mem, "Title of %s", url)) }) }; +} + +fn Summary build_summary(Doc doc) +{ + return { + .title = maybe::value{String}(doc.head.get().title.get()) ?? {}, + .ok = true, + }; +} + +fn Summary read_and_build_summary(String url) +{ + return build_summary(read_doc(url)) ?? {}; +} + +fn bool! is_title_non_empty(Doc doc) +{ + String! title = doc.head.get().title.get(); + if (catch title) return TitleResult.TITLE_MISSING?; + return title.len > 0; +} + +fn bool! read_whether_title_non_empty(String url) +{ + return is_title_non_empty(read_doc(url)); +} + +fn String bool_to_string(bool b) +{ + return b ? "true" : "false"; +} + +fn void main() +{ + const String[] URLS = { "good", "title-empty", "title-missing", "head-missing", "fail" }; + DynamicArenaAllocator dynamic_arena; + dynamic_arena.init(1024, allocator::heap()); + OutStream out = io::stdout(); + foreach (String url : URLS) + { + mem::@scoped(&dynamic_arena) + { + io::printf(`Checking "https://%s/":` "\n", url); + Summary summary = read_and_build_summary(url); + io::fprintf(out, " Summary: ")!!; + summary.print(out)!!; + io::fprintn(out, "")!!; + io::fprintf(out, " Title: %s\n", summary.title.get() ?? "")!!; + bool! has_title = read_whether_title_non_empty(url); + // This looks a bit less than elegant, but as you see it's mostly due to having to + // use printf here. + io::fprintf(out, " Has title: %s vs %s\n", bool_to_string(has_title) ?? (@catch(has_title)).nameof, has_title ?? false)!!; + }; + dynamic_arena.reset(); + } + dynamic_arena.free(); +}