OpenGL and Vulkan examples; ptr(int)/int(ptr); trailing commas in calls; drop .fn hint

- examples/opengl.fch: OpenGL 3.3 core, shaders, VAO/VBO, a spinning triangle (checked offscreen)
- examples/vulkan.fch: instance, GPUs with properties/memory/queues, logical device and queue
- ptr(n) and int(p) conversions for C APIs that pass offsets as pointers
- .ptr works on string literals; a comma after the last call argument is allowed
- a missing header included by an imported header is now named in the error
- the old .fn extension hint is gone (Finch is new, there are no old files)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-10-08 19:37:34 +02:00
commit 8497251dc5
22 changed files with 357 additions and 26 deletions

View file

@ -160,7 +160,7 @@ unary = ( "-" | "!" | "~" ) unary | postfix ;
postfix = primary { "." IDENT [ args ] | "[" expr "]" } ;
primary = INT | FLOAT | STRING | CHAR | "true" | "false" | "null"
| IDENT [ args ] | "(" expr ")" | "[" [ expr { "," expr } [","] ] "]" ;
args = "(" [ arg { "," arg } ] ")" ; arg = [ IDENT ":" ] expr ;
args = "(" [ arg { "," arg } [","] ] ")" ; arg = [ IDENT ":" ] expr ;
```
**Statement termination.** After a statement, `endOfStatement()` requires `}`, end of file, or a token
@ -343,6 +343,7 @@ int + float → float; f32 + f64 → f64. There is no C integer promotion: `u8 +
| `[]u8`/`[]char` → str | `finch_str_from_bytes` |
| ptr → str | `finch_str_from_c` (borrowed, `cap = -1`) |
| ptr ↔ ptr | nothing |
| int → ptr / ptr → int | `inttoptr` / `ptrtoint` |
### Comparisons

View file

@ -244,6 +244,8 @@ str(42) // "42" (also floats, bools, chars; str([]u8) turns bytes into
int("42") // 42 (stops the program if the text isn't a number)
float("2.5") // 2.5
ptr(p) // a typed pointer as an untyped one
ptr(16) // a number as a pointer (C APIs that pass offsets as pointers, like OpenGL)
int(p) // a pointer as a number (its address)
str(p) // a C char pointer as text
```
@ -345,6 +347,7 @@ fn no_result(str message) { // no "->": returns nothing
To change the caller's variable, pass a pointer: see [Pointers](#14-pointers).
- A function with `-> type` must return a value on every path. Finch checks this.
- A function cannot be defined inside another function.
- Long calls can be split over several lines inside the brackets; a comma after the last argument is fine.
---
@ -604,6 +607,30 @@ fn main() {
To give C a function to call back (like `qsort`'s comparator), use `addr(myFunction)`.
Its parameters must be C types (numbers, pointers, C structs).
### Graphics: OpenGL and Vulkan
Both work directly; `examples/opengl.fch` and `examples/vulkan.fch` are complete programs.
- **OpenGL 3+** on Linux: the modern functions (`glCreateShader`, `glGenVertexArrays`, …) are only
declared when `GL_GLEXT_PROTOTYPES` is defined. Finch can't define C macros, so put the includes in a
tiny header next to your program and import that:
```c
// opengl.h
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#include <GLFW/glfw3.h>
```
Then `import "opengl.h"`, `link "glfw"`, `link "GL"`. Out-parameters use `addr(...)`
(`glGenBuffers(1, addr(vbo))`), buffer data uses `.ptr` (`vertices.ptr`), and byte offsets use
`ptr(...)` (`glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, ptr(8))`).
- **Vulkan**: `import "vulkan/vulkan.h"` and `link "vulkan"`. Vulkan's create-info structs are built with
named fields (missing ones are zero), handles are `ptr`, and lists come from the usual "call twice"
pattern: once for the count, once with `list.ptr` after `list.resize(count)`. Function-like macros such as
`VK_MAKE_API_VERSION` aren't available; compute the number instead (`1 << 22` is Vulkan 1.0).
### What does not work yet
- C **unions** by value, and structs with **bit fields** by value (pointers to them work).
@ -680,7 +707,7 @@ Finch/
├── src/ the compiler (C++)
├── runtime/ finch_rt.c: the small runtime (text, arrays, input, files)
├── boot/ the Finch compiler written in Finch (see the engineers' guide)
├── examples/ hello, tour, types, structs, todo, guess, c_import, window, raylib, llvm
├── examples/ hello, tour, types, structs, todo, guess, c_import, window, opengl, vulkan, raylib, llvm
├── tests/
│ ├── run/ programs + the exact output they must print (.out), input (.in)
│ ├── fail/ programs that must fail, with the expected error in line 1