Add C and C++ library support
import "zlib.h" reads the header (cc -E + a small C declaration parser), links via pkg-config or link "name", and builds .c/.cpp files next to the program. C structs become Dino structs, pointers become pointer types (nil = NULL), T* parameters take variables or lists. Adds dino header, editor completion/hover for C names, docs chapter 13, a raylib example, and tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b42e2a3bd
commit
2f44f09b6a
26 files changed
+3418
-70
No files matched your search
@@ -167,7 +167,7 @@ Targets: game dev, CLI tools, desktop apps.
|
||||
from another file (fields are private too unless written `pub x: int`). Each file has its own imports and its
|
||||
own names, so two files may both define `f`. Import cycles and missing files are errors;
|
||||
errors point at the file they are in. Names of module types print with the module
|
||||
(`utils.Vec2{...}`). Importing a C library (`import c "x.h"`) is not supported yet.
|
||||
(`utils.Vec2{...}`). C headers: see **C libraries** below.
|
||||
- **Built-in modules**: `os` (`read_file write_file append_file exists exit env
|
||||
read_line`, and the value `args`), `math` (`sqrt pow floor ceil round trunc sin cos tan asin acos atan atan2 exp log log10 hypot min max abs PI E`),
|
||||
`time` (`now sleep`). Text has methods: `upper lower trim split lines fields replace
|
||||
@@ -184,7 +184,7 @@ Targets: game dev, CLI tools, desktop apps.
|
||||
`string(x)` (or `"{c}"`) makes text. `for c in s` / `for i, c in s` walk characters (`i` = byte position,
|
||||
invalid UTF-8 comes out as U+FFFD). `s[i]` is the character starting at byte `i` (panics out of range or
|
||||
in the middle of a character); strings can't be assigned to. Methods `is_digit is_letter is_alnum is_space
|
||||
is_upper is_lower upper lower` (ASCII plus Latin-1 letters). Not yet: `s.chars()`, `[]char`→string.
|
||||
is_upper is_lower upper lower` (ASCII plus Latin-1 letters). `s.chars()` gives a `[]char`. Not yet: `[]char`→string.
|
||||
- **Formatter** `dino fmt [--check] <file|folder>..` (`src/fmt.rs`): text based, so comments and blank lines survive.
|
||||
4-space indent, spaces around binary operators and after `,` `:`, tight `Point{...}` / map literals and `[]T`,
|
||||
at most one blank line, trailing `//` comments of neighbouring lines aligned. It never moves line breaks (a one-line
|
||||
@@ -194,7 +194,24 @@ Targets: game dev, CLI tools, desktop apps.
|
||||
dependencies): full-text sync, error diagnostics from the real checker (imports read from disk or from open
|
||||
buffers; errors in other files shown at the top of the importing file; UTF-16 columns), whole-file formatting.
|
||||
`editors/vscode` (extension + TextMate grammar), `editors/vim` (syntax), `editors/README.md` (Neovim, Helix, others).
|
||||
Not yet: hover, go to definition, completion, rename; the compiler reports only the first error.
|
||||
Also outline, go to definition, hover, completion and signature help from a token index (`src/index.rs`),
|
||||
including names from C headers. Not yet: references, rename; the compiler reports only the first error.
|
||||
- **C libraries** (`src/cheader.rs`): `import "zlib.h"` reads the header (no hand-written declarations): it runs
|
||||
`cc -E -dD` and parses the declarations, enum values and object-like `#define`s from the header and the headers it
|
||||
includes itself (in a shared folder like /usr/include: the header and what it includes, minus the C library).
|
||||
Names are used as `zlib.name` (prefix = file name without `.h`; `import z "zlib.h"` picks one);
|
||||
`import "zlib.h" { a, b }` makes only those names visible, without a prefix. Linking: `pkg-config` with names
|
||||
guessed from the header path, else `import "x.h" link "x"` (`-lx`). A header next to the program adds `-I` and
|
||||
builds the `.c`/`.cpp`/`.cc` file with the same name into the program (C++ with `c++`, linked with `-lstdc++`).
|
||||
Types: C ints -> sized numbers (`int` i32, `long` int, `size_t` u64), `double` float, `char` i8 (takes `'a'`),
|
||||
`char *` -> string (copied both ways, NULL -> ""), enums -> `i32` constants, plain structs -> Dino structs with the
|
||||
C field names (`raylib.Color`; converted field by field at calls), other pointers -> pointer types
|
||||
(`Ty::CPtr`, `stdio.FILE`, `nil` = NULL, `==` compares); `void *` (`pointer`) mixes with every pointer type.
|
||||
`T *` parameters take a variable (the address is passed; counts as a change for inferred mutation) or a list
|
||||
(its items); `const T *` also takes any value. Constants: numbers, strings, and struct literals like
|
||||
`CLITERAL(Color){...}` (emitted as the macro). Programs that import headers are built with `-std=gnu11`.
|
||||
Not supported (a clear error when called): variadic functions, callbacks, unions/arrays by value, lists of
|
||||
structs, `long double`, function-like macros. Dino never frees C memory. `dino header x.h [word]` lists what Dino sees.
|
||||
- **String slicing**: `s[a..b]`, `s[a..]`, `s[..b]` (byte positions, bounds checked; panics with "the length is N").
|
||||
- **Running**: `dino run file.dino a b c` passes `a b c` to the program (`os.args`).
|
||||
- **Example**: `examples/calc/` is a multi-file calculator (tokenizer, parser, evaluator, shared scope); `session.txt` / `session.out` are checked by a test.
|
||||
|
||||
Reference in new issue
Block a user