C FFI & Python Bridge
C FFI & Python Bridge
C Foreign Function Interface
Thagore provides direct access to C functions through extern func declarations. Any function available in a C shared library or the system’s C standard library can be called.
Basic C FFI
# Declare C standard library functionsextern func strlen(s: String) -> i32extern func puts(text: String) -> i32extern func printf(fmt: String, value: i32) -> i32
func main() -> i32: let msg = "Hello from FFI!" puts(msg) printf("String length: %d\n", strlen(msg)) return 0Math Library FFI
Call C math functions directly:
# Float math functionsextern func sqrtf(x: f32) -> f32extern func powf(base: f32, exp: f32) -> f32extern func sinf(x: f32) -> f32extern func cosf(x: f32) -> f32extern func fabsf(x: f32) -> f32
# Double math functionsextern func sqrt(x: f64) -> f64extern func pow(base: f64, exp: f64) -> f64
func calc_hypotenuse(a: f32, b: f32) -> f32: return sqrtf(a * a + b * b)
func main() -> i32: let h = calc_hypotenuse(3.0, 4.0) print(h) # Output: 5.0 return 0Memory Management FFI
extern func malloc(size: i32) -> ptrextern func free(p: ptr) -> voidextern func memcpy(dst: ptr, src: ptr, size: i32) -> ptr
func allocate_buffer(size: i32) -> ptr: let buffer = malloc(size) return buffer
func main() -> i32: let buf = allocate_buffer(1024) # ... use buffer ... free(buf) return 0FFI Type Mapping Reference
| Thagore Type | C Type | Notes |
|---|---|---|
i32 | int / int32_t | 32-bit signed integer |
f32 | float | IEEE 754 single precision |
f64 | double | IEEE 754 double precision |
String | const char* | Null-terminated C string |
ptr | void* | Generic pointer |
void | void | No return value |
Python Bridge
Thagore can interface with Python through the runtime’s Python bridge. This enables calling Python libraries (including NumPy, PyTorch, etc.) from Thagore code.
Python Bridge Functions
| Function | Purpose |
|---|---|
__thg_py_initialize() | Initialize the Python interpreter |
__thg_py_finalize() | Shutdown the Python interpreter |
__thg_py_import(name) | Import a Python module |
__thg_py_getattr(obj, name) | Get an attribute from a Python object |
__thg_py_call0(callable) | Call a Python callable with zero arguments |
__thg_py_call_2(func, a1, a2) | Call with two arguments |
__thg_py_from_i32(val) | Convert Thagore i32 to Python int |
__thg_py_print_obj(obj) | Print a Python object to stdout |
Basic Python Usage
extern func __thg_py_initialize() -> voidextern func __thg_py_finalize() -> voidextern func __thg_py_import(name: String) -> ptrextern func __thg_py_print_obj(obj: ptr) -> void
func main() -> i32: print("Importing Python math module...") __thg_py_initialize()
let math_mod = __thg_py_import("math") __thg_py_print_obj(math_mod)
__thg_py_finalize() return 0PyTorch Integration
Call PyTorch from Thagore for tensor operations:
extern func __thg_py_initialize() -> voidextern func __thg_py_import(name: String) -> ptrextern func __thg_py_getattr(obj: ptr, name: String) -> ptrextern func __thg_py_print_obj(obj: ptr) -> voidextern func __thg_py_from_i32(val: i32) -> ptrextern func __thg_py_call_2(func_obj: ptr, a1: ptr, a2: ptr) -> ptr
func main() -> i32: __thg_py_initialize()
print("Loading PyTorch...") let torch = __thg_py_import("torch")
# Get torch.rand function let rand_func = __thg_py_getattr(torch, "rand")
# Create dimensions let dim1 = __thg_py_from_i32(3) let dim2 = __thg_py_from_i32(3)
# Generate a 3x3 random tensor print("Generating Tensor(3, 3)...") let tensor = __thg_py_call_2(rand_func, dim1, dim2)
__thg_py_print_obj(tensor) return 0Requirements
To use the Python bridge:
- Python 3.x must be installed and on PATH
- Link against the Python shared library
- Required Python packages must be installed (
pip install torchfor PyTorch)