7ee6a73ea4
Add standard library modules: - Std::String: strlen, strcmp, concat, copy, starts_with wrappers - Std::Map: linear-probing hash map with String keys, int values Add error handling: - Result and Option algebraic enum examples - ? try operator for automatic error propagation in HIR lowering Compiler bugfixes: - Fix sizeof() for user-defined structs (new hSizeOf HIR node) - Fix HIR type lowering for char8, bool8, int8, uint8, etc. - Fix let statement lowering for pointer types (*char8 → int* bug) - Fix PrintInt ABI mismatch (int64_t → int in io.c) - Fix Makefile test bug (_test_tmp_pkg already exists) Documentation: - Rewrite README.md with features, examples, project structure - Add docs/LanguageRef.md — complete language reference - Add docs/Stdlib.md — standard library documentation - Add docs/BuildAndTest.md — build and test guide New examples: strings, map, result_option, try_operator (13 total)
117 lines
2.3 KiB
Plaintext
117 lines
2.3 KiB
Plaintext
// Result and Option - Error handling and optional values
|
|
import Std::Io::{PrintLine, PrintInt};
|
|
|
|
enum Result {
|
|
Ok(int),
|
|
Err(String)
|
|
}
|
|
|
|
func Result_NewOk(value: int) -> Result {
|
|
let r: Result = Result { tag: Result_Ok };
|
|
r.data.Ok_0 = value;
|
|
return r;
|
|
}
|
|
|
|
func Result_NewErr(msg: String) -> Result {
|
|
let r: Result = Result { tag: Result_Err };
|
|
r.data.Err_0 = msg;
|
|
return r;
|
|
}
|
|
|
|
func Result_IsOk(r: Result) -> bool {
|
|
return r.tag == Result_Ok;
|
|
}
|
|
|
|
func Result_IsErr(r: Result) -> bool {
|
|
return r.tag == Result_Err;
|
|
}
|
|
|
|
func Result_Unwrap(r: Result) -> int {
|
|
if r.tag == Result_Ok {
|
|
return r.data.Ok_0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
func Result_UnwrapOr(r: Result, fallback: int) -> int {
|
|
if r.tag == Result_Ok {
|
|
return r.data.Ok_0;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
enum Option {
|
|
Some(int),
|
|
None
|
|
}
|
|
|
|
func Option_NewSome(value: int) -> Option {
|
|
let o: Option = Option { tag: Option_Some };
|
|
o.data.Some_0 = value;
|
|
return o;
|
|
}
|
|
|
|
func Option_NewNone() -> Option {
|
|
return Option { tag: Option_None };
|
|
}
|
|
|
|
func Option_IsSome(o: Option) -> bool {
|
|
return o.tag == Option_Some;
|
|
}
|
|
|
|
func Option_UnwrapOr(o: Option, fallback: int) -> int {
|
|
if o.tag == Option_Some {
|
|
return o.data.Some_0;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
func Divide(a: int, b: int) -> Result {
|
|
if b == 0 {
|
|
return Result_NewErr("division by zero");
|
|
}
|
|
return Result_NewOk(a / b);
|
|
}
|
|
|
|
func SafeDivide(a: int, b: int) -> Option {
|
|
if b == 0 {
|
|
return Option_NewNone();
|
|
}
|
|
return Option_NewSome(a / b);
|
|
}
|
|
|
|
func Main() -> int {
|
|
PrintLine("Result demo:");
|
|
|
|
let r1: Result = Divide(10, 2);
|
|
if Result_IsOk(r1) {
|
|
PrintLine("10 / 2 = ");
|
|
PrintInt(Result_Unwrap(r1));
|
|
PrintLine("");
|
|
}
|
|
|
|
let r2: Result = Divide(10, 0);
|
|
if Result_IsErr(r2) {
|
|
PrintLine("10 / 0 failed");
|
|
}
|
|
PrintLine("unwrap_or = ");
|
|
PrintInt(Result_UnwrapOr(r2, -1));
|
|
PrintLine("");
|
|
|
|
PrintLine("Option demo:");
|
|
|
|
let o1: Option = SafeDivide(20, 4);
|
|
if Option_IsSome(o1) {
|
|
PrintLine("20 / 4 = ");
|
|
PrintInt(Option_UnwrapOr(o1, 0));
|
|
PrintLine("");
|
|
}
|
|
|
|
let o2: Option = SafeDivide(20, 0);
|
|
PrintLine("20 / 0 fallback = ");
|
|
PrintInt(Option_UnwrapOr(o2, -1));
|
|
PrintLine("");
|
|
|
|
return 0;
|
|
}
|