FFI & Import
FFI & Import
Module System
Thagore has a straightforward module system based on import statements. There are three import styles:
1. Import a Standard Library Module
import fsimport envimport processThis imports modules from the lib/ directory. You access functions via the module name:
import fs
func main() -> i32: let content = fs.read_text("data.txt") print(content) return 02. Import a File by Path
import "src/driver/cli/main.tg" as cliimport "lib/string.tg" as strimport "std/core.tg" as coreThe as keyword creates a namespace alias:
import "lib/string.tg" as str
func main() -> i32: let trimmed = str.trim(" hello ") print(trimmed) return 03. Import a File Without Alias
import "src/semantics/typedir/nodes.tg"This makes all exported symbols from the file available in the current scope.
extern func — C Foreign Function Interface
The extern func keyword declares an external C function that can be called from Thagore:
extern func strlen(s: String) -> i32extern func printf(fmt: String, value: i32) -> i32extern func exit(code: i32) -> voidCalling C Math Functions
extern func sqrtf(x: f32) -> f32extern func powf(base: f32, exp: f32) -> f32
func hypotenuse(a: f32, b: f32) -> f32: return sqrtf(powf(a, 2.0) + powf(b, 2.0))
func main() -> i32: let c = hypotenuse(3.0, 4.0) print(c) # Output: 5.0 return 0Calling C Standard Library Functions
extern func malloc(size: i32) -> ptrextern func free(p: ptr) -> voidextern func memcpy(dst: ptr, src: ptr, size: i32) -> ptrextern func puts(text: String) -> i32FFI Type Mapping
| Thagore Type | C Equivalent |
|---|---|
i32 | int / int32_t |
f32 | float |
f64 | double |
String | const char* |
ptr | void* |
void | void |
Runtime Intrinsics
The Thagore runtime provides built-in intrinsic functions prefixed with __thg_:
extern func __thg_str_substr(s: String, start: i32, len: i32) -> Stringextern func __thg_ptr_null() -> ptrextern func __thg_ptr_set(base: ptr, index: i32, value: ptr) -> voidextern func __thg_ptr_get(base: ptr, index: i32) -> ptrextern func __string_from_codepoint(cp: i32) -> Stringextern func __thg_str_contains(text: String, needle: String) -> i32extern func __thg_str_starts_with(text: String, prefix: String) -> i32extern func __thg_str_ends_with(text: String, suffix: String) -> i32extern func __thg_str_trim(text: String) -> Stringextern func __thg_str_replace(text: String, old: String, new: String) -> Stringextern func __thg_str_lower(text: String) -> Stringextern func __thg_str_upper(text: String) -> StringPython Bridge
Thagore provides FFI functions for Python interop:
extern func __thg_py_initialize() -> voidextern func __thg_py_finalize() -> voidextern func __thg_py_import(name: String) -> ptrextern func __thg_py_getattr(obj: ptr, name: String) -> ptrextern func __thg_py_call0(callable: ptr) -> ptrextern func __thg_py_print_obj(obj: ptr) -> void
func main() -> i32: __thg_py_initialize() let math_mod = __thg_py_import("math") __thg_py_print_obj(math_mod) __thg_py_finalize() return 0See Integration: C FFI & Python for a complete guide.
Import Resolution Order
- Quoted path imports (
import "path/to/file.tg") — resolved relative to the project root - Bare module imports (
import fs) — resolved from thelib/directory - Standard library (
import "std/core.tg") — resolved from thestd/directory