Files
c3c/resources/examples/contextfree/boolerr.c3
Christoffer Lerno fc849c1440 0.6.0: init_new/init_temp removed. LinkedList API rewritten. List "pop" and "remove" function now return Optionals. RingBuffer API rewritten. Allocator interface changed. Deprecated Allocator, DString and mem functions removed. "identity" functions are now constants for Matrix and Complex numbers. @default implementations for interfaces removed. any* => any, same for interfaces. Emit local/private globals as "private" in LLVM, following C "static". Updated enum syntax. Add support [rgba] properties in vectors. Improved checks of aliased "void". Subarray -> slice. Fix of llvm codegen enum check. Improved alignment handling. Add --output-dir #1155. Removed List/Object append. GenericList renamed AnyList. Remove unused "unwrap". Fixes to cond. Optimize output in dead branches. Better checking of operator methods. Disallow any from implementing dynamic methods. Check for operator mismatch. Remove unnecessary bitfield. Remove numbering in --list* commands Old style enum declaration for params/type, but now the type is optional. Add note on #1086. Allow making distinct types out of "void", "typeid", "anyfault" and faults. Remove system linker build options. "Try" expressions must be simple expressions. Add optimized build to Mac tests. Register int. assert(false) only allowed in unused branches or in tests. Compile time failed asserts is a compile time error. Remove current_block_is_target. Bug when assigning an optional from an optional. Remove unused emit_zstring. Simplify phi code. Remove unnecessary unreachable blocks and remove unnecessary current_block NULL assignments. Proper handling of '.' and Win32 '//server' paths. Unify expression and macro blocks in the middle end. Add "no discard" to expression blocks with a return value. Detect "unsigned >= 0" as errors. Fix issue with distinct void as a member #1147. Improve callstack debug information #1184. Fix issue with absolute output-dir paths. Lambdas were not type checked thoroughly #1185. Fix compilation warning #1187. Request jump table using @jump for switches. Path normalization - fix possible null terminator out of bounds. Improved error messages on inlined macros.
2024-05-22 18:22:04 +02:00

99 lines
2.5 KiB
C

module test;
import libc;
import std::io;
import std::collections::maybe;
def MaybeString = Maybe(<String>);
def MaybeHead = Maybe(<Head>);
def new_head_val = maybe::value(<Head>);
def new_string_val = maybe::value(<String>);
fault TitleResult
{
TITLE_MISSING
}
fault ReadError
{
BAD_READ,
}
struct Doc { MaybeHead head; }
struct Head { MaybeString title; }
struct Summary
{
MaybeString 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 = new_head_val({}) };
if (url.contains("title-empty")) return { .head = new_head_val({ .title = new_string_val("")}) };
return { .head = new_head_val({ .title = new_string_val(string::new_format("Title of %s", url)) }) };
}
fn Summary build_summary(Doc doc)
{
return Summary {
.title = new_string_val(doc.head.get().title.get()) ?? MaybeString {},
.ok = true,
};
}
fn Summary read_and_build_summary(String url)
{
return build_summary(read_doc(url)) ?? Summary {};
}
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();
}