Finch/examples/c_import.fn
Nexoniarz 0591cd20f3 Finch 1.0.0
A small, fast programming language on LLVM with C-like syntax.

- Lexer, recursive-descent parser, single-pass type checker + LLVM IR codegen, O2 pipeline
- Types: int/float/bool/char/str, sized i8..u64/f32/f64, pointers ptr[T] with addr()/.value/null
- Functions (fn ... -> T), if/else, while, for..in, break/continue, Go-style operator precedence
- Implicit conversions only when lossless; literals adapt to their target type
- Runtime checks for division by zero, MIN/-1 and null .value
- C interop: import "header.h" via libclang, link "lib" with pkg-config, link-error advice
- Human-readable errors with source excerpts
- Test suite (34 tests), examples, docs for beginners/technicians/engineers in English and Polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 18:21:06 +02:00

23 lines
584 B
Text

// Use any C library: import its header and call it.
import "stdio.h"
import "math.h"
import "stdlib.h"
import "string.h"
fn main() {
printf("printf works: %d + %d = %d\n", 2, 3, 2 + 3)
puts("so does puts")
print("sqrt(2) =", sqrt(2))
print("pi from math.h:", M_PI)
print("strlen(\"finch\") =", strlen("finch"))
print("RAND_MAX =", RAND_MAX, "EOF =", EOF)
// memory from C: malloc gives a ptr, free gives it back
ptr[i32] n = malloc(4)
n.value = 7
print("from the heap:", n.value)
free(n)
fprintf(stderr, "this goes to stderr\n")
}