Skip to content

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_stdlib.tg
import fs
import env
import process

This imports modules from the lib/ directory. You access functions via the module name:

use_module.tg
import fs
func main() -> i32:
let content = fs.read_text("data.txt")
print(content)
return 0

2. Import a File by Path

import_path.tg
import "src/driver/cli/main.tg" as cli
import "lib/string.tg" as str
import "std/core.tg" as core

The as keyword creates a namespace alias:

namespaced.tg
import "lib/string.tg" as str
func main() -> i32:
let trimmed = str.trim(" hello ")
print(trimmed)
return 0

3. Import a File Without Alias

import_direct.tg
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:

ffi_basic.tg
extern func strlen(s: String) -> i32
extern func printf(fmt: String, value: i32) -> i32
extern func exit(code: i32) -> void

Calling C Math Functions

ffi_math.tg
extern func sqrtf(x: f32) -> f32
extern 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 0

Calling C Standard Library Functions

ffi_stdlib.tg
extern func malloc(size: i32) -> ptr
extern func free(p: ptr) -> void
extern func memcpy(dst: ptr, src: ptr, size: i32) -> ptr
extern func puts(text: String) -> i32

FFI Type Mapping

Thagore TypeC Equivalent
i32int / int32_t
f32float
f64double
Stringconst char*
ptrvoid*
voidvoid

Runtime Intrinsics

The Thagore runtime provides built-in intrinsic functions prefixed with __thg_:

intrinsics.tg
extern func __thg_str_substr(s: String, start: i32, len: i32) -> String
extern func __thg_ptr_null() -> ptr
extern func __thg_ptr_set(base: ptr, index: i32, value: ptr) -> void
extern func __thg_ptr_get(base: ptr, index: i32) -> ptr
extern func __string_from_codepoint(cp: i32) -> String
extern func __thg_str_contains(text: String, needle: String) -> i32
extern func __thg_str_starts_with(text: String, prefix: String) -> i32
extern func __thg_str_ends_with(text: String, suffix: String) -> i32
extern func __thg_str_trim(text: String) -> String
extern func __thg_str_replace(text: String, old: String, new: String) -> String
extern func __thg_str_lower(text: String) -> String
extern func __thg_str_upper(text: String) -> String

Python Bridge

Thagore provides FFI functions for Python interop:

python_bridge.tg
extern func __thg_py_initialize() -> void
extern func __thg_py_finalize() -> void
extern func __thg_py_import(name: String) -> ptr
extern func __thg_py_getattr(obj: ptr, name: String) -> ptr
extern func __thg_py_call0(callable: ptr) -> ptr
extern 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 0

See Integration: C FFI & Python for a complete guide.

Import Resolution Order

  1. Quoted path imports (import "path/to/file.tg") — resolved relative to the project root
  2. Bare module imports (import fs) — resolved from the lib/ directory
  3. Standard library (import "std/core.tg") — resolved from the std/ directory