feat: Std::String, Std::Map, Result/Option, ? operator, sizeof fix, docs
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)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Map - Hash map with String keys and int values
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Map::{Map, Map_New, Map_Set, Map_Get, Map_Has, Map_Len};
|
||||
|
||||
func Main() -> int {
|
||||
let m: Map = Map_New(16);
|
||||
|
||||
PrintLine("Map operations:");
|
||||
|
||||
Map_Set(&m, "one", 1);
|
||||
Map_Set(&m, "two", 2);
|
||||
Map_Set(&m, "three", 3);
|
||||
|
||||
PrintLine("Length:");
|
||||
PrintInt(Map_Len(&m));
|
||||
PrintLine("");
|
||||
|
||||
PrintLine("Get 'one':");
|
||||
PrintInt(Map_Get(&m, "one"));
|
||||
PrintLine("");
|
||||
|
||||
PrintLine("Get 'two':");
|
||||
PrintInt(Map_Get(&m, "two"));
|
||||
PrintLine("");
|
||||
|
||||
PrintLine("Get 'three':");
|
||||
PrintInt(Map_Get(&m, "three"));
|
||||
PrintLine("");
|
||||
|
||||
Map_Set(&m, "two", 22);
|
||||
PrintLine("Updated 'two':");
|
||||
PrintInt(Map_Get(&m, "two"));
|
||||
PrintLine("");
|
||||
|
||||
if Map_Has(&m, "one") {
|
||||
PrintLine("Has 'one': yes");
|
||||
}
|
||||
|
||||
if !Map_Has(&m, "four") {
|
||||
PrintLine("Has 'four': no");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Strings - String manipulation with Std::String
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::String::{String_Len, String_Eq, String_Concat};
|
||||
|
||||
func Main() -> int {
|
||||
let hello: String = "Hello";
|
||||
let world: String = "World";
|
||||
|
||||
PrintLine("String operations:");
|
||||
|
||||
// Length
|
||||
PrintLine("Length of 'Hello':");
|
||||
PrintInt(String_Len(hello));
|
||||
PrintLine("");
|
||||
|
||||
// Concatenation
|
||||
let greeting: String = String_Concat(hello, ", ");
|
||||
let greeting2: String = String_Concat(greeting, world);
|
||||
let full: String = String_Concat(greeting2, "!");
|
||||
|
||||
PrintLine("Concatenated:");
|
||||
PrintLine(full);
|
||||
|
||||
// Equality
|
||||
if String_Eq(hello, "Hello") {
|
||||
PrintLine("'Hello' equals 'Hello'");
|
||||
}
|
||||
|
||||
if !String_Eq(hello, world) {
|
||||
PrintLine("'Hello' does not equal 'World'");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Try operator ? - Error propagation
|
||||
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 Divide(a: int, b: int) -> Result {
|
||||
if b == 0 {
|
||||
return Result_NewErr("division by zero");
|
||||
}
|
||||
return Result_NewOk(a / b);
|
||||
}
|
||||
|
||||
func Compute() -> Result {
|
||||
let x: int = Divide(10, 2)?;
|
||||
let y: int = Divide(x, 5)?;
|
||||
return Result_NewOk(y);
|
||||
}
|
||||
|
||||
func ComputeWithError() -> Result {
|
||||
let x: int = Divide(10, 0)?;
|
||||
let y: int = Divide(x, 5)?;
|
||||
return Result_NewOk(y);
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
PrintLine("Try operator demo:");
|
||||
|
||||
let r1: Result = Compute();
|
||||
if r1.tag == Result_Ok {
|
||||
PrintLine("Compute() = ");
|
||||
PrintInt(r1.data.Ok_0);
|
||||
PrintLine("");
|
||||
} else {
|
||||
PrintLine("Compute() failed");
|
||||
}
|
||||
|
||||
let r2: Result = ComputeWithError();
|
||||
if r2.tag == Result_Err {
|
||||
PrintLine("ComputeWithError() failed as expected");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user