Core & std Modules
Core & std Modules
These are the foundational modules in std/ that provide low-level operations.
std/core.tg — Core Primitives
The core module wraps C standard library functions and Thagore runtime intrinsics.
Memory Functions
extern func malloc(size: i32) -> ptrextern func free(p: ptr) -> voidextern func memcpy(dst: ptr, src: ptr, size: i32) -> ptrOutput Functions
func print_i32(value: i32) -> i32 # Print an integer with newlinefunc print_str(text: String) -> i32 # Print a string with newlinePointer Functions
func ptr_set(base: ptr, index: i32, value: ptr) -> i32 # Set pointer at indexfunc ptr_get(base: ptr, index: i32) -> ptr # Get pointer at indexfunc ptr_null() -> ptr # Returns null pointerString Functions
func str_substr(s: String, start: i32, len: i32) -> String # Substring extractionfunc thg_str_add(a: String, b: String) -> String # String concatenationControl
func abort(code: i32) -> i32 # Exit process with error codefunc mem_free(p: ptr) -> i32 # Free memory (returns 1)std/string.tg — String Operations
Basic Operations
func substring(text: String, start: i32, count: i32) -> Stringfunc char_at(text: String, index: i32) -> Stringfunc equals(a: String, b: String) -> boolExamples:
import "std/string.tg" as sstr
let text = "Hello World"let sub = sstr.substring(text, 0, 5)print(sub) # Output: Hello
let ch = sstr.char_at(text, 4)print(ch) # Output: oInteger to String Conversion
func to_string_i32(value: i32) -> StringConverts any i32 to its string representation. Handles negative numbers and edge cases (including INT_MIN).
import "std/string.tg" as sstr
let s = sstr.to_string_i32(42)print(s) # Output: "42"
let neg = sstr.to_string_i32(-100)print(neg) # Output: "-100"StringBuilder
A builder pattern for efficient string construction:
struct StringBuilder: total_len: i32 cache: String
func builder_new() -> StringBuilderfunc append(self: StringBuilder, part: String) -> StringBuilderfunc len(self: StringBuilder) -> i32func to_string(self: StringBuilder) -> Stringfunc dispose(self: StringBuilder) -> i32Example:
import "std/string.tg" as sstr
func main() -> i32: let sb = sstr.builder_new() sb = sstr.append(sb, "Hello") sb = sstr.append(sb, " ") sb = sstr.append(sb, "World") print(sstr.to_string(sb)) # Output: Hello World sstr.dispose(sb) return 0std/list.tg — Dynamic List
A dynamically-resizing array of pointers (similar to ArrayList):
struct List: data: ptr size: i32 capacity: i32API
func new() -> List # Create empty list (capacity 4)func len(self: List) -> i32 # Current sizefunc push(self: List, item: ptr) -> List # Append itemfunc get(self: List, index: i32) -> ptr # Get item (returns null if out of bounds)func set(self: List, index: i32, val: ptr) -> List # Set item at indexfunc dispose(self: List) -> i32 # Free memoryfunc is_null_ptr(p: ptr) -> i32 # Check if pointer is nullExample
import list
func main() -> i32: let my_list = list.new()
# Push items my_list.push("Item 1") my_list.push("Item 2") my_list.push("Item 3")
# Read size print(my_list.count()) # Output: 3
# Access items let i = 0 while (i < my_list.count()): let s = my_list.get(i) print(s) i = i + 1
# Clean up my_list.dispose() return 0Internal Details
- Initial capacity: 4 elements
- Growth strategy: double on overflow (4 → 8 → 16 → …)
- Elements are stored as
ptr— any pointer type can be stored - Out-of-bounds access returns
null(checked viais_null_ptr)