Phase 5-7: generic inference, extend Type<T>, String stdlib, Map<K,V>, self-hosting audit, docs update
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
// Generic impl blocks — extend Box<T> with methods
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
struct Box<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
extend Box<T> {
|
||||
func Get(self: *Box<T>) -> T {
|
||||
return self.value;
|
||||
}
|
||||
|
||||
func Set(self: *Box<T>, value: T) {
|
||||
self.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let b: Box<int> = Box<int> { value: 42 };
|
||||
PrintLine("Box value:");
|
||||
PrintInt(b.Get());
|
||||
PrintLine("");
|
||||
|
||||
b.Set(100);
|
||||
PrintLine("Box after Set(100):");
|
||||
PrintInt(b.Get());
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Generic function type inference — calls without explicit type args
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
func Max<T>(a: T, b: T) -> T {
|
||||
if a > b {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
// Inferred: T = int
|
||||
let m1: int = Max(10, 20);
|
||||
let m2: int = Max(5, 3);
|
||||
|
||||
PrintLine("Max(10, 20) = ");
|
||||
PrintInt(m1);
|
||||
PrintLine("");
|
||||
|
||||
PrintLine("Max(5, 3) = ");
|
||||
PrintInt(m2);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Generic function type inference — multiple type parameters
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
struct Pair<T, U> {
|
||||
first: T,
|
||||
second: U,
|
||||
}
|
||||
|
||||
func MakePair<T, U>(a: T, b: U) -> Pair<T, U> {
|
||||
return Pair<T, U> { first: a, second: b };
|
||||
}
|
||||
|
||||
func Pair_GetFirst<T, U>(self: *Pair<T, U>) -> T {
|
||||
return self.first;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
// Inferred: T = int, U = String
|
||||
let p: Pair<int, String> = MakePair(42, "hello");
|
||||
PrintInt(p.GetFirst());
|
||||
PrintLine("");
|
||||
return 0;
|
||||
}
|
||||
+19
-33
@@ -1,44 +1,30 @@
|
||||
// Map - Hash map with String keys and int values
|
||||
// Generic Map<K, V> — uses Std::Map (stdlib is merged into module)
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Map::{Map, Map_New, Map_Set, Map_Get, Map_Has, Map_Len};
|
||||
import Std::Map::{Map, Map_New, Map_Set, Map_Get, Map_Has, Map_Free};
|
||||
|
||||
func Main() -> int {
|
||||
let m: Map = Map_New(16);
|
||||
PrintLine("=== Map<int, String> ===");
|
||||
let m: Map<int, String> = Map_New<int, String>(16);
|
||||
|
||||
PrintLine("Map operations:");
|
||||
Map_Set<int, String>(&m, 1, "one");
|
||||
Map_Set<int, String>(&m, 2, "two");
|
||||
Map_Set<int, String>(&m, 3, "three");
|
||||
|
||||
Map_Set(&m, "one", 1);
|
||||
Map_Set(&m, "two", 2);
|
||||
Map_Set(&m, "three", 3);
|
||||
PrintLine("Get 1:");
|
||||
PrintLine(Map_Get<int, String>(&m, 1));
|
||||
|
||||
PrintLine("Length:");
|
||||
PrintInt(Map_Len(&m));
|
||||
PrintLine("");
|
||||
PrintLine("Get 2:");
|
||||
PrintLine(Map_Get<int, String>(&m, 2));
|
||||
|
||||
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<int, String>(&m, 1) {
|
||||
PrintLine("Has 1: yes");
|
||||
}
|
||||
|
||||
if !Map_Has(&m, "four") {
|
||||
PrintLine("Has 'four': no");
|
||||
}
|
||||
// Test update
|
||||
Map_Set<int, String>(&m, 2, "updated");
|
||||
PrintLine("Updated 2:");
|
||||
PrintLine(Map_Get<int, String>(&m, 2));
|
||||
|
||||
Map_Free<int, String>(&m);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// String improvements — slicing, trimming, contains, StringBuilder
|
||||
import Std::Io::{Print, PrintLine, PrintInt, PrintBool};
|
||||
import Std::String::{String_Len, String_Slice, String_Contains, String_EndsWith, String_StartsWith};
|
||||
import Std::String::{String_Trim, String_TrimLeft, String_TrimRight, String_FromInt, String_ToInt};
|
||||
import Std::String::{StringBuilder, StringBuilder_New, StringBuilder_Append, StringBuilder_AppendInt, StringBuilder_Build, StringBuilder_Free};
|
||||
|
||||
func Main() -> int {
|
||||
// --- Slicing ---
|
||||
let hello: String = "Hello, World!";
|
||||
Print("Slice(0,5): ");
|
||||
PrintLine(String_Slice(hello, 0, 5));
|
||||
|
||||
Print("Slice(7,5): ");
|
||||
PrintLine(String_Slice(hello, 7, 5));
|
||||
|
||||
// --- Contains ---
|
||||
Print("Contains 'World': ");
|
||||
PrintBool(String_Contains(hello, "World"));
|
||||
PrintLine("");
|
||||
|
||||
Print("Contains 'xyz': ");
|
||||
PrintBool(String_Contains(hello, "xyz"));
|
||||
PrintLine("");
|
||||
|
||||
// --- EndsWith ---
|
||||
Print("EndsWith '!': ");
|
||||
PrintBool(String_EndsWith(hello, "!"));
|
||||
PrintLine("");
|
||||
|
||||
// --- StartsWith ---
|
||||
Print("StartsWith 'Hello': ");
|
||||
PrintBool(String_StartsWith(hello, "Hello"));
|
||||
PrintLine("");
|
||||
|
||||
// --- Trim ---
|
||||
let spaced: String = " foo bar ";
|
||||
Print("Trim: '");
|
||||
Print(String_Trim(spaced));
|
||||
PrintLine("'");
|
||||
|
||||
Print("TrimLeft: '");
|
||||
Print(String_TrimLeft(spaced));
|
||||
PrintLine("'");
|
||||
|
||||
Print("TrimRight: '");
|
||||
Print(String_TrimRight(spaced));
|
||||
PrintLine("'");
|
||||
|
||||
// --- Int conversion ---
|
||||
Print("FromInt(42): ");
|
||||
PrintLine(String_FromInt(42));
|
||||
|
||||
Print("FromInt(-100): ");
|
||||
PrintLine(String_FromInt(-100));
|
||||
|
||||
let n: int64 = String_ToInt("123");
|
||||
Print("ToInt('123'): ");
|
||||
PrintInt(n as int);
|
||||
PrintLine("");
|
||||
|
||||
// --- StringBuilder ---
|
||||
let sb: StringBuilder = StringBuilder_New();
|
||||
StringBuilder_Append(&sb, "Hello");
|
||||
StringBuilder_Append(&sb, ", ");
|
||||
StringBuilder_Append(&sb, "World");
|
||||
StringBuilder_Append(&sb, "! #");
|
||||
StringBuilder_AppendInt(&sb, 42);
|
||||
|
||||
Print("StringBuilder: ");
|
||||
PrintLine(StringBuilder_Build(&sb));
|
||||
StringBuilder_Free(&sb);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user