mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 08:54:25 +00:00
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:
parent
f897fb3140
commit
8497251dc5
22 changed files with 357 additions and 26 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 ;
|
||||
```
|
||||
|
||||
**Koniec instrukcji.** Po instrukcji `endOfStatement()` wymaga `}`, końca pliku albo tokenu
|
||||
|
|
@ -347,6 +347,7 @@ Nie ma promocji całkowitych z C: `u8 + u8` zostaje `u8`.
|
|||
| `[]u8`/`[]char` → str | `finch_str_from_bytes` |
|
||||
| ptr → str | `finch_str_from_c` (pożyczony, `cap = -1`) |
|
||||
| ptr ↔ ptr | nic |
|
||||
| int → ptr / ptr → int | `inttoptr` / `ptrtoint` |
|
||||
|
||||
### Porównania
|
||||
|
||||
|
|
|
|||
|
|
@ -244,6 +244,8 @@ str(42) // "42" (też float, bool, char; str([]u8) zamienia bajty na t
|
|||
int("42") // 42 (zatrzymuje program, jeśli tekst nie jest liczbą)
|
||||
float("2.5") // 2.5
|
||||
ptr(p) // wskaźnik z typem jako wskaźnik bez typu
|
||||
ptr(16) // liczba jako wskaźnik (API C, które przekazują przesunięcia jako wskaźniki, np. OpenGL)
|
||||
int(p) // wskaźnik jako liczba (jego adres)
|
||||
str(p) // wskaźnik na znaki z C jako tekst
|
||||
```
|
||||
|
||||
|
|
@ -346,6 +348,7 @@ fn bez_wyniku(str wiadomosc) { // bez "->": nic nie zwraca
|
|||
Żeby zmienić zmienną wywołującego, przekaż wskaźnik, zobacz [Wskaźniki](#14-wskaźniki).
|
||||
- Funkcja z `-> typ` musi zwrócić wartość na każdej ścieżce. Finch to sprawdza.
|
||||
- Funkcji nie można definiować wewnątrz innej funkcji.
|
||||
- Długie wywołania możesz rozbić na kilka linijek wewnątrz nawiasów; przecinek po ostatnim argumencie jest dozwolony.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -605,6 +608,30 @@ fn main() {
|
|||
Żeby dać C funkcję do wywołania zwrotnego (np. porównywarkę dla `qsort`), użyj `addr(mojaFunkcja)`.
|
||||
Jej parametry muszą być typami C (liczby, wskaźniki, struktury C).
|
||||
|
||||
### Grafika: OpenGL i Vulkan
|
||||
|
||||
Oba działają bezpośrednio; `examples/opengl.fch` i `examples/vulkan.fch` to kompletne programy.
|
||||
|
||||
- **OpenGL 3+** na Linuksie: nowoczesne funkcje (`glCreateShader`, `glGenVertexArrays`, …) są deklarowane
|
||||
dopiero po zdefiniowaniu `GL_GLEXT_PROTOTYPES`. Finch nie definiuje makr C, więc dołączenia umieść w małym
|
||||
nagłówku obok programu i zaimportuj go:
|
||||
|
||||
```c
|
||||
// opengl.h
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
```
|
||||
|
||||
Potem `import "opengl.h"`, `link "glfw"`, `link "GL"`. Parametry wyjściowe przekazujesz przez `addr(...)`
|
||||
(`glGenBuffers(1, addr(vbo))`), dane bufora przez `.ptr` (`vertices.ptr`), a przesunięcia w bajtach przez
|
||||
`ptr(...)` (`glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, ptr(8))`).
|
||||
- **Vulkan**: `import "vulkan/vulkan.h"` i `link "vulkan"`. Struktury „create info” budujesz z nazwanymi polami
|
||||
(brakujące są zerami), uchwyty to `ptr`, a listy pobierasz typowym wzorcem „dwóch wywołań”: raz po liczbę,
|
||||
raz z `lista.ptr` po `lista.resize(liczba)`. Makra-funkcje, jak `VK_MAKE_API_VERSION`, nie są dostępne;
|
||||
policz liczbę sam (`1 << 22` to Vulkan 1.0).
|
||||
|
||||
### Co jeszcze nie działa
|
||||
|
||||
- **Unie** C przez wartość i struktury z **polami bitowymi** przez wartość (wskaźniki do nich działają).
|
||||
|
|
@ -682,7 +709,7 @@ Finch/
|
|||
├── src/ kompilator (C++)
|
||||
├── runtime/ finch_rt.c: mała biblioteka uruchomieniowa (teksty, tablice, wejście, pliki)
|
||||
├── boot/ kompilator Fincha napisany w Finchu (zobacz przewodnik dla inżynierów)
|
||||
├── 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/ programy + dokładny wynik (.out) i wejście (.in)
|
||||
│ ├── fail/ programy, które muszą się nie udać, z oczekiwanym błędem w 1. linijce
|
||||
|
|
|
|||
Loading…
Reference in a new issue