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
+3410
-62
No files matched your search
@@ -63,6 +63,8 @@ error: age cannot be negative
|
|||||||
missing is `T?`. Both must be handled.
|
missing is `T?`. Both must be handled.
|
||||||
- **Modern basics.** Enums that carry data, exhaustive `match`, generics, interfaces,
|
- **Modern basics.** Enums that carry data, exhaustive `match`, generics, interfaces,
|
||||||
closures, maps, characters, modules.
|
closures, maps, characters, modules.
|
||||||
|
- **C and C++ libraries.** `import "raylib.h"` and call it: Dino reads the header itself,
|
||||||
|
links the library, and builds your own `.c`/`.cpp` files next to the program.
|
||||||
- **Tooling included.** A formatter, a language server, and editor files, all in the one `dino`
|
- **Tooling included.** A formatter, a language server, and editor files, all in the one `dino`
|
||||||
program.
|
program.
|
||||||
|
|
||||||
@@ -96,17 +98,18 @@ dino run hello.dino
|
|||||||
| `dino emit-c file.dino` | Print the generated C. |
|
| `dino emit-c file.dino` | Print the generated C. |
|
||||||
| `dino fmt [--check] path...` | Format code (`--check` only reports). |
|
| `dino fmt [--check] path...` | Format code (`--check` only reports). |
|
||||||
| `dino lsp` | Start the language server (editors run this for you). |
|
| `dino lsp` | Start the language server (editors run this for you). |
|
||||||
|
| `dino header file.h [word]` | Show what Dino can use from a C header. |
|
||||||
|
|
||||||
## Learn the language
|
## Learn the language
|
||||||
|
|
||||||
The [`docs/`](docs/README.md) folder is a short course, written to be read in order:
|
The [`docs/`](docs/README.md) folder is a short course, written to be read in order:
|
||||||
first steps, values, control flow, functions, lists and maps, structs and enums, errors,
|
first steps, values, control flow, functions, lists and maps, structs and enums, errors,
|
||||||
memory, generics and interfaces, modules, and a full cheatsheet. Every example in it is run by
|
memory, generics and interfaces, modules, C and C++ libraries, and a full cheatsheet. Every example in it is run by
|
||||||
the test suite, so what you read is what happens.
|
the test suite, so what you read is what happens.
|
||||||
|
|
||||||
The last chapter builds a calculator over five files. It lives in
|
Chapter 12 builds a calculator over five files. It lives in
|
||||||
[`examples/calc`](examples/calc), next to many smaller programs in
|
[`examples/calc`](examples/calc), next to many smaller programs in
|
||||||
[`examples/basics`](examples/basics).
|
[`examples/basics`](examples/basics). [`examples/c`](examples/c) has a small raylib game.
|
||||||
|
|
||||||
For the exact rules of the language, see [`SPEC.md`](SPEC.md).
|
For the exact rules of the language, see [`SPEC.md`](SPEC.md).
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
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;
|
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
|
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
|
- **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`),
|
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
|
`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,
|
`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
|
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
|
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.
|
- **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`,
|
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
|
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
|
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.
|
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).
|
`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").
|
- **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`).
|
- **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.
|
- **Example**: `examples/calc/` is a multi-file calculator (tokenizer, parser, evaluator, shared scope); `session.txt` / `session.out` are checked by a test.
|
||||||
|
|||||||
+10
-1
@@ -119,8 +119,17 @@ import os // built-in: os math time
|
|||||||
pub func f() {} // visible to other files
|
pub func f() {} // visible to other files
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## C libraries
|
||||||
|
```dino
|
||||||
|
import "zlib.h" // zlib.crc32(...), zlib.Z_OK, types like zlib.gzFile
|
||||||
|
import "raylib.h" { InitWindow } // only these names, used bare
|
||||||
|
import "foo.h" link "foo" // when pkg-config doesn't know it (libfoo.so)
|
||||||
|
var f = stdio.fopen("x.txt", "r") // C pointers: compare with nil, close them yourself
|
||||||
|
lib.fill(buf, n) // T* parameters take a variable or a list
|
||||||
|
```
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server)
|
`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server) · `dino header x.h [word]` (what a C header offers)
|
||||||
|
|
||||||
## Things that stop the program (exit code 2)
|
## Things that stop the program (exit code 2)
|
||||||
Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing
|
Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing
|
||||||
|
|||||||
@@ -97,3 +97,5 @@ run_line(scope, line)
|
|||||||
- Add a function `sin(x)` next to `sqrt`.
|
- Add a function `sin(x)` next to `sqrt`.
|
||||||
- Add `%` as an operator in the tokenizer, parser and `apply`.
|
- Add `%` as an operator in the tokenizer, parser and `apply`.
|
||||||
- Make `sum` accept any number of arguments.
|
- Make `sum` accept any number of arguments.
|
||||||
|
|
||||||
|
Next: [C and C++ libraries](13-c-libraries.md)
|
||||||
@@ -0,0 +1,304 @@
|
|||||||
|
# 13. C and C++ libraries
|
||||||
|
|
||||||
|
A huge amount of useful code is written in C: graphics (raylib, SDL), compression (zlib),
|
||||||
|
databases (SQLite), and the C library that comes with every system. Dino can use it
|
||||||
|
directly. You don't write any declarations: Dino reads the library's **header file** (the
|
||||||
|
`.h` file that lists what the library has) and works out the rest.
|
||||||
|
|
||||||
|
## Importing a header
|
||||||
|
|
||||||
|
Put the header's name in quotes. Its functions, types and constants are then used with the
|
||||||
|
header's name in front, the same way as your own files (`import utils` gives `utils.name`).
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "string.h"
|
||||||
|
import "stdlib.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print(string.strlen("hello"))
|
||||||
|
print(stdlib.abs(-7), stdlib.atoi("42") + 1)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
5
|
||||||
|
7 43
|
||||||
|
```
|
||||||
|
|
||||||
|
The name in front is the file name without `.h`: `"zlib.h"` gives `zlib`, `"SDL2/SDL.h"`
|
||||||
|
gives `SDL`, `"my-lib.h"` gives `my_lib`. To choose another one, write it before the quotes:
|
||||||
|
`import z "zlib.h"` gives `z.crc32(...)`.
|
||||||
|
|
||||||
|
## Picking a few names
|
||||||
|
|
||||||
|
When you only want some things from a header, list them in braces. Those names are then
|
||||||
|
used **without** a prefix, and nothing else from the header is visible:
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "math.h" { cbrt, hypot }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print(cbrt(27.0), hypot(3.0, 4.0))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
3.0 5.0
|
||||||
|
```
|
||||||
|
|
||||||
|
(`math.h` is the C math library. Dino's own `import math` is usually nicer; this is just a
|
||||||
|
small example.)
|
||||||
|
|
||||||
|
## How C types look in Dino
|
||||||
|
|
||||||
|
| In C | In Dino |
|
||||||
|
|------|---------|
|
||||||
|
| `int`, `unsigned int`, `short`, ... | `i32`, `u32`, `i16`, ... |
|
||||||
|
| `long`, `long long`, `int64_t` | `int` |
|
||||||
|
| `size_t`, `unsigned long`, `uint64_t` | `u64` |
|
||||||
|
| `double` / `float` | `float` / `f32` |
|
||||||
|
| `char` | `i8` (you can pass a character like `'a'`) |
|
||||||
|
| `bool` | `bool` |
|
||||||
|
| `const char *` and `char *` | `string` |
|
||||||
|
| an `enum` | its values are constants (`i32`) |
|
||||||
|
| a `struct` with plain fields | a Dino struct: `raylib.Color{255, 0, 0, 255}` |
|
||||||
|
| any other pointer (`FILE *`, `SDL_Window *`) | a pointer type: `stdio.FILE`, `SDL.SDL_Window` |
|
||||||
|
| `NULL` | `nil` |
|
||||||
|
|
||||||
|
Numbers follow the usual Dino rule: a number written in the code fits any number type, but
|
||||||
|
a variable has to be converted, for example `i32(n)` when the C function takes an `int`.
|
||||||
|
|
||||||
|
Text works both ways: a Dino string is handed to C as a C string, and a C string that comes
|
||||||
|
back becomes a Dino string (a copy, so it stays valid).
|
||||||
|
|
||||||
|
## Pointers: things you only pass around
|
||||||
|
|
||||||
|
Many C libraries give you a pointer to something they manage, like an open file or a
|
||||||
|
window. In Dino you keep it in a variable, hand it back to the library, and compare it with
|
||||||
|
`nil` to see if it is missing:
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "stdio.h"
|
||||||
|
import os
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var f = stdio.fopen("/tmp/dino-hello.txt", "w")
|
||||||
|
if f == nil {
|
||||||
|
print("can't open the file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stdio.fputs("hello from C\n", f)
|
||||||
|
stdio.fclose(f)
|
||||||
|
|
||||||
|
var text, err = os.read_file("/tmp/dino-hello.txt")
|
||||||
|
print(text.trim(), err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
hello from C nil
|
||||||
|
```
|
||||||
|
|
||||||
|
The type can be written like any other: `var f: stdio.FILE` starts out as `nil`.
|
||||||
|
|
||||||
|
**Dino does not free C things for you.** Whatever the library opens or creates, close or
|
||||||
|
free it with the library's own function (`fclose`, `sqlite3_close`, `UnloadTexture`, ...).
|
||||||
|
|
||||||
|
## Functions that write into your variables
|
||||||
|
|
||||||
|
Some C functions give back extra results by writing into a variable you pass them (in C:
|
||||||
|
a pointer parameter like `double *out`). Pass a Dino **variable**, and the function changes
|
||||||
|
it. Pass a **list** where the C function wants room for several values (a buffer):
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "math.h" { modf }
|
||||||
|
import "string.h" { memset }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var whole = 0.0
|
||||||
|
var frac = modf(3.75, whole) // modf writes the whole part into `whole`
|
||||||
|
print(frac, whole)
|
||||||
|
|
||||||
|
var buf: []u8 = [0, 0, 0]
|
||||||
|
memset(buf, 65, 3) // fills the list's items
|
||||||
|
print(buf)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
0.75 3.0
|
||||||
|
[65, 65, 65]
|
||||||
|
```
|
||||||
|
|
||||||
|
A list given to C must already have as many items as the function will write; the C
|
||||||
|
function can't make it longer.
|
||||||
|
|
||||||
|
The same goes for libraries that hand you a new pointer through a parameter (in C:
|
||||||
|
`sqlite3 **db`). Make a variable of that pointer type and pass it; afterwards it is set:
|
||||||
|
|
||||||
|
```text
|
||||||
|
var db: sqlite3.sqlite3
|
||||||
|
sqlite3.sqlite3_open("notes.db", db) // db is now open (or still nil if it failed)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Constants
|
||||||
|
|
||||||
|
Values from `#define` and `enum` work like Dino constants: `stdio.EOF`, `zlib.Z_OK`,
|
||||||
|
`raylib.KEY_SPACE`. raylib's colours (`raylib.RED`, `raylib.RAYWHITE`) are struct constants
|
||||||
|
of type `raylib.Color`.
|
||||||
|
|
||||||
|
## Linking: where the library's code is
|
||||||
|
|
||||||
|
The header only describes a library; its code is in a library file that has to be linked
|
||||||
|
in. Dino asks the system (`pkg-config`) for that, so for most installed libraries
|
||||||
|
`import "raylib.h"` or `import "zlib.h"` is all you need. If a library isn't known to
|
||||||
|
`pkg-config`, name it yourself: for the file `libfoo.so`, write
|
||||||
|
|
||||||
|
```text
|
||||||
|
import "foo.h" link "foo"
|
||||||
|
```
|
||||||
|
|
||||||
|
If it is missing, building stops with a message that says so.
|
||||||
|
|
||||||
|
## A window with raylib
|
||||||
|
|
||||||
|
[raylib](https://www.raylib.com) is a simple library for games and graphics. With it
|
||||||
|
installed, this opens a window and draws a circle:
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "raylib.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
raylib.InitWindow(800, 450, "Hello from Dino")
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
var x: f32 = 400.0
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
if raylib.IsKeyDown(raylib.KEY_RIGHT) { x += 4.0 }
|
||||||
|
if raylib.IsKeyDown(raylib.KEY_LEFT) { x -= 4.0 }
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
raylib.ClearBackground(raylib.RAYWHITE)
|
||||||
|
raylib.DrawCircleV(raylib.Vector2{x, 225.0}, 40.0, raylib.MAROON)
|
||||||
|
raylib.DrawText("arrow keys move the ball", 10, 10, 20, raylib.DARKGRAY)
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
raylib.CloseWindow()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
A bigger example with bouncing balls is in
|
||||||
|
[`examples/c/bounce.dino`](../examples/c/bounce.dino).
|
||||||
|
|
||||||
|
## Your own C code
|
||||||
|
|
||||||
|
Put a header and its `.c` file next to your program. `import "shapes.h"` reads the header,
|
||||||
|
and Dino compiles `shapes.c` and builds it into your program.
|
||||||
|
|
||||||
|
```c
|
||||||
|
// shapes.h
|
||||||
|
typedef struct { double w, h; } Size;
|
||||||
|
double area(Size s);
|
||||||
|
void grow(Size *s, double by);
|
||||||
|
```
|
||||||
|
|
||||||
|
```c
|
||||||
|
// shapes.c
|
||||||
|
#include "shapes.h"
|
||||||
|
double area(Size s) { return s.w * s.h; }
|
||||||
|
void grow(Size *s, double by) { s->w *= by; s->h *= by; }
|
||||||
|
```
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "shapes.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s = shapes.Size{2.0, 3.0}
|
||||||
|
shapes.grow(s, 10.0) // grow changes `s`
|
||||||
|
print(shapes.area(s), s.w)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
600.0 20.0
|
||||||
|
```
|
||||||
|
|
||||||
|
## C++
|
||||||
|
|
||||||
|
C++ code can't be called from C or Dino directly: C++ functions get scrambled ("mangled")
|
||||||
|
names when compiled, and classes and templates don't exist in C. The usual fix is a small
|
||||||
|
set of plain functions marked `extern "C"`. Write them in a `.cpp` file next to the header,
|
||||||
|
and Dino compiles it with your C++ compiler (`c++`) and links in the C++ library:
|
||||||
|
|
||||||
|
```c
|
||||||
|
// counter.h: plain C, also readable by C++
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
typedef struct Counter Counter;
|
||||||
|
Counter *counter_new(void);
|
||||||
|
void counter_add(Counter *c, int n);
|
||||||
|
int counter_total(const Counter *c);
|
||||||
|
void counter_free(Counter *c);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// counter.cpp: free to use any C++ inside
|
||||||
|
#include "counter.h"
|
||||||
|
#include <numeric>
|
||||||
|
#include <vector>
|
||||||
|
struct Counter { std::vector<int> items; };
|
||||||
|
extern "C" {
|
||||||
|
Counter *counter_new(void) { return new Counter{}; }
|
||||||
|
void counter_add(Counter *c, int n) { c->items.push_back(n); }
|
||||||
|
int counter_total(const Counter *c) { return std::accumulate(c->items.begin(), c->items.end(), 0); }
|
||||||
|
void counter_free(Counter *c) { delete c; }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```dino
|
||||||
|
import "counter.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var c = counter.counter_new()
|
||||||
|
for i in 1..5 { counter.counter_add(c, i32(i)) }
|
||||||
|
print(counter.counter_total(c))
|
||||||
|
counter.counter_free(c)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
10
|
||||||
|
```
|
||||||
|
|
||||||
|
The same trick works for a big C++ library: wrap the few things you need in `extern "C"`
|
||||||
|
functions.
|
||||||
|
|
||||||
|
## Seeing what a header has
|
||||||
|
|
||||||
|
`dino header` lists everything Dino can use from a header, written as Dino. Add a word to
|
||||||
|
only see names that contain it:
|
||||||
|
|
||||||
|
```text
|
||||||
|
dino header zlib.h compress
|
||||||
|
```
|
||||||
|
```text
|
||||||
|
import "zlib.h" // use the names below as zlib.name
|
||||||
|
// builds with: -DWITH_GZFILEOP -lz
|
||||||
|
|
||||||
|
// functions
|
||||||
|
func compress(dest: []u8 or u8 variable, destLen: []u64 or u64 variable, source: []u8 or u8, sourceLen: u64): i32
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
In an editor with Dino support, completion after `zlib.` and hovering a name show the same.
|
||||||
|
|
||||||
|
## What doesn't work yet
|
||||||
|
|
||||||
|
Dino tells you when you call one of these, and why:
|
||||||
|
|
||||||
|
- functions with a variable number of arguments, like `printf`
|
||||||
|
- functions that take a callback (a function pointer)
|
||||||
|
- `union` types, and structs with arrays inside (you can still pass pointers to them)
|
||||||
|
- a list of structs where C wants several structs in a row
|
||||||
|
- C macros that take arguments (`#define MAX(a, b) ...`)
|
||||||
|
|
||||||
|
When you need one of these, write a small function in your own `.c` file that does the C
|
||||||
|
part, and call that from Dino.
|
||||||
|
|
||||||
|
Next: back to the [overview](README.md)
|
||||||
@@ -21,6 +21,7 @@ These notes teach it from zero. Read them in order, or jump to the
|
|||||||
| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time` |
|
| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time` |
|
||||||
| 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page |
|
| 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page |
|
||||||
| 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files |
|
| 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files |
|
||||||
|
| 13 | [C and C++ libraries](13-c-libraries.md) | `import "raylib.h"`, your own C code |
|
||||||
|
|
||||||
## How to read the examples
|
## How to read the examples
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// A ball bouncing around a window, drawn with raylib (https://www.raylib.com).
|
||||||
|
// Needs raylib installed: `dino run bounce.dino`. Space adds a ball, Escape quits.
|
||||||
|
|
||||||
|
import "raylib.h"
|
||||||
|
|
||||||
|
const WIDTH = 800
|
||||||
|
const HEIGHT = 450
|
||||||
|
|
||||||
|
struct Ball {
|
||||||
|
pos: raylib.Vector2
|
||||||
|
speed: raylib.Vector2
|
||||||
|
radius: f32
|
||||||
|
color: raylib.Color
|
||||||
|
|
||||||
|
func step(dt: f32) {
|
||||||
|
pos.x += speed.x * dt
|
||||||
|
pos.y += speed.y * dt
|
||||||
|
if pos.x < radius || pos.x > f32(WIDTH) - radius { speed.x = -speed.x }
|
||||||
|
if pos.y < radius || pos.y > f32(HEIGHT) - radius { speed.y = -speed.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func new_ball(n: int): Ball {
|
||||||
|
var colors = [raylib.RED, raylib.GOLD, raylib.SKYBLUE, raylib.LIME, raylib.VIOLET]
|
||||||
|
var x = f32(100 + n * 37 % 600)
|
||||||
|
var y = f32(80 + n * 53 % 290)
|
||||||
|
return Ball{raylib.Vector2{x, y}, raylib.Vector2{f32(180 + n * 20), 140.0}, 20.0, colors[n % colors.len()]}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
raylib.InitWindow(WIDTH, HEIGHT, "Dino + raylib")
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
var balls = [new_ball(0)]
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
if raylib.IsKeyPressed(raylib.KEY_SPACE) {
|
||||||
|
balls.push(new_ball(balls.len()))
|
||||||
|
}
|
||||||
|
var dt = raylib.GetFrameTime()
|
||||||
|
for i in 0..balls.len() {
|
||||||
|
balls[i].step(dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
raylib.ClearBackground(raylib.RAYWHITE)
|
||||||
|
for b in balls {
|
||||||
|
raylib.DrawCircleV(b.pos, b.radius, b.color)
|
||||||
|
}
|
||||||
|
raylib.DrawText("balls: {balls.len()} (space adds one)", 10, 10, 20, raylib.DARKGRAY)
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
raylib.CloseWindow()
|
||||||
|
}
|
||||||
+8
-1
@@ -27,12 +27,19 @@ pub enum Item {
|
|||||||
Const(VarDecl),
|
Const(VarDecl),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `import os`, `import a.b`, or `import c "SDL2/SDL.h"`.
|
/// `import os`, `import a.b`, or a C header: `import "zlib.h"`, `import "zlib.h" { compress }`,
|
||||||
|
/// `import "mylib.h" link "mylib"` (and `import z "zlib.h"` to choose the name).
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Import {
|
pub struct Import {
|
||||||
pub alias: Option<String>,
|
pub alias: Option<String>,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
|
/// A C header (the path was written in quotes).
|
||||||
|
pub c: bool,
|
||||||
|
/// `{ a, b }`: only these names, used without a prefix.
|
||||||
|
pub names: Option<Vec<(String, Span)>>,
|
||||||
|
/// `link "z"`: the library to link with.
|
||||||
|
pub link: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
|||||||
+1702
File diff suppressed because it is too large.
Load diff
+386
-12
@@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
|
|||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::error::{Diag, PResult};
|
use crate::error::{Diag, PResult};
|
||||||
use crate::hir::*;
|
use crate::hir::*;
|
||||||
|
use crate::cheader::{self, CTy};
|
||||||
use crate::stdlib::{self, ModuleValue};
|
use crate::stdlib::{self, ModuleValue};
|
||||||
use crate::types::{Num, Ty};
|
use crate::types::{Num, Ty};
|
||||||
|
|
||||||
@@ -17,12 +18,23 @@ pub fn check_project(main: &Program, modules: &[(String, Program)]) -> PResult<H
|
|||||||
Checker::new().program(main, modules)
|
Checker::new().program(main, modules)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Like `check_project`, with the C headers the program imports already read.
|
||||||
|
pub fn check_project_c(main: &Program, modules: &[(String, Program)], clibs: Vec<cheader::Lib>) -> PResult<HProgram> {
|
||||||
|
let mut c = Checker::new();
|
||||||
|
c.clibs = clibs;
|
||||||
|
c.program(main, modules)
|
||||||
|
}
|
||||||
|
|
||||||
/// What an `import` name stands for.
|
/// What an `import` name stands for.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
enum Target {
|
enum Target {
|
||||||
Builtin(&'static str),
|
Builtin(&'static str),
|
||||||
/// A file of the user's; the full import path (`utils`, `a.b`).
|
/// A file of the user's; the full import path (`utils`, `a.b`).
|
||||||
User(String),
|
User(String),
|
||||||
|
/// A C header (index into `clibs`): its names are used as `zlib.name`.
|
||||||
|
C(usize),
|
||||||
|
/// One name picked from a C header with `import "zlib.h" { name }`.
|
||||||
|
CItem(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
const BUILTINS: &[&str] = &[
|
const BUILTINS: &[&str] = &[
|
||||||
@@ -146,6 +158,10 @@ struct Checker {
|
|||||||
/// While checking a generic function: what its type names (`T`) stand for.
|
/// While checking a generic function: what its type names (`T`) stand for.
|
||||||
tsubst: HashMap<String, Ty>,
|
tsubst: HashMap<String, Ty>,
|
||||||
inst_queue: Vec<Instance>,
|
inst_queue: Vec<Instance>,
|
||||||
|
/// The C headers the program imports.
|
||||||
|
clibs: Vec<cheader::Lib>,
|
||||||
|
/// Structs that come from C headers: table name -> how C spells the type.
|
||||||
|
c_structs: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- names used inside a function body (to find what a closure captures) ----------
|
// ---------- names used inside a function body (to find what a closure captures) ----------
|
||||||
@@ -410,6 +426,7 @@ fn has_call(h: &HExpr) -> bool {
|
|||||||
| HExprKind::ListPop(_)
|
| HExprKind::ListPop(_)
|
||||||
| HExprKind::MapCall { .. }
|
| HExprKind::MapCall { .. }
|
||||||
| HExprKind::Builtin { .. }
|
| HExprKind::Builtin { .. }
|
||||||
|
| HExprKind::CCall { .. }
|
||||||
| HExprKind::CallValue { .. }
|
| HExprKind::CallValue { .. }
|
||||||
| HExprKind::IfaceCall { .. }
|
| HExprKind::IfaceCall { .. }
|
||||||
| HExprKind::Closure { .. }
|
| HExprKind::Closure { .. }
|
||||||
@@ -514,6 +531,8 @@ impl Checker {
|
|||||||
bounds_of: HashMap::new(),
|
bounds_of: HashMap::new(),
|
||||||
tsubst: HashMap::new(),
|
tsubst: HashMap::new(),
|
||||||
inst_queue: Vec::new(),
|
inst_queue: Vec::new(),
|
||||||
|
clibs: Vec::new(),
|
||||||
|
c_structs: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -523,6 +542,7 @@ impl Checker {
|
|||||||
let mut mods: Vec<(&str, &Program)> = modules.iter().map(|(k, p)| (k.as_str(), p)).collect();
|
let mut mods: Vec<(&str, &Program)> = modules.iter().map(|(k, p)| (k.as_str(), p)).collect();
|
||||||
mods.push(("", main));
|
mods.push(("", main));
|
||||||
self.known_modules = modules.iter().map(|(k, _)| k.clone()).collect();
|
self.known_modules = modules.iter().map(|(k, _)| k.clone()).collect();
|
||||||
|
self.register_c_structs();
|
||||||
|
|
||||||
for (key, p) in &mods {
|
for (key, p) in &mods {
|
||||||
self.enter(key);
|
self.enter(key);
|
||||||
@@ -577,7 +597,11 @@ impl Checker {
|
|||||||
let types = type_order
|
let types = type_order
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|name| match self.structs.get(name) {
|
.map(|name| match self.structs.get(name) {
|
||||||
Some(info) => HTypeDef::Struct(HStruct { name: name.to_string(), fields: info.fields.clone() }),
|
Some(info) => HTypeDef::Struct(HStruct {
|
||||||
|
name: name.to_string(),
|
||||||
|
fields: info.fields.clone(),
|
||||||
|
c: self.c_structs.get(name).cloned(),
|
||||||
|
}),
|
||||||
None => HTypeDef::Enum(HEnum {
|
None => HTypeDef::Enum(HEnum {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
variants: self.enums[name]
|
variants: self.enums[name]
|
||||||
@@ -591,7 +615,21 @@ impl Checker {
|
|||||||
let mut ifaces: Vec<(String, Vec<(String, Vec<Ty>, Ty)>)> =
|
let mut ifaces: Vec<(String, Vec<(String, Vec<Ty>, Ty)>)> =
|
||||||
self.ifaces.iter().map(|(k, i)| (k.clone(), i.methods.clone())).collect();
|
self.ifaces.iter().map(|(k, i)| (k.clone(), i.methods.clone())).collect();
|
||||||
ifaces.sort_by(|a, b| a.0.cmp(&b.0));
|
ifaces.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
Ok(HProgram { types, funcs, ifaces })
|
let mut c = CInfo::default();
|
||||||
|
for lib in &self.clibs {
|
||||||
|
let add = |list: &mut Vec<String>, items: Vec<String>| {
|
||||||
|
for i in items {
|
||||||
|
if !list.contains(&i) {
|
||||||
|
list.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
add(&mut c.headers, vec![lib.header.clone()]);
|
||||||
|
add(&mut c.cflags, lib.cflags.clone());
|
||||||
|
add(&mut c.libs, lib.libs.clone());
|
||||||
|
add(&mut c.sources, lib.sources.iter().map(|p| p.display().to_string()).collect());
|
||||||
|
}
|
||||||
|
Ok(HProgram { types, funcs, ifaces, c })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start checking the module `key`.
|
/// Start checking the module `key`.
|
||||||
@@ -1012,8 +1050,8 @@ impl Checker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn import(&mut self, i: &Import) -> PResult<()> {
|
fn import(&mut self, i: &Import) -> PResult<()> {
|
||||||
if i.alias.is_some() {
|
if i.c {
|
||||||
return unsupported(i.span, "importing a C library");
|
return self.import_c(i);
|
||||||
}
|
}
|
||||||
let alias = i.path.rsplit('.').next().unwrap().to_string();
|
let alias = i.path.rsplit('.').next().unwrap().to_string();
|
||||||
if self.imports.contains_key(&alias) {
|
if self.imports.contains_key(&alias) {
|
||||||
@@ -1046,15 +1084,41 @@ impl Checker {
|
|||||||
fn user_alias(&self, e: &Expr) -> Option<String> {
|
fn user_alias(&self, e: &Expr) -> Option<String> {
|
||||||
match self.alias_target(e)? {
|
match self.alias_target(e)? {
|
||||||
Target::User(p) => Some(p.clone()),
|
Target::User(p) => Some(p.clone()),
|
||||||
Target::Builtin(_) => None,
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `e` is the name of an imported C header, which one.
|
||||||
|
fn c_alias(&self, e: &Expr) -> Option<usize> {
|
||||||
|
match self.alias_target(e)? {
|
||||||
|
Target::C(i) => Some(*i),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A name picked from a C header (`import "zlib.h" { name }`), not hidden by a variable.
|
||||||
|
fn c_item(&self, name: &str) -> Option<usize> {
|
||||||
|
if self.lookup(name).is_some() || self.implicit_field(name).is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match self.imports.get(name)? {
|
||||||
|
Target::CItem(i) => Some(*i),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The table name for a type or function written as `name` or `module.name`.
|
/// The table name for a type or function written as `name` or `module.name`.
|
||||||
fn global_key(&self, e: &Expr) -> Option<String> {
|
fn global_key(&self, e: &Expr) -> Option<String> {
|
||||||
match &e.kind {
|
match &e.kind {
|
||||||
ExprKind::Ident(n) => Some(self.q(n)),
|
ExprKind::Ident(n) => match self.c_item(n) {
|
||||||
ExprKind::Field { expr, name } => Some(format!("{}.{}", self.user_alias(expr)?, name)),
|
Some(i) => Some(format!("{}.{n}", self.clibs[i].prefix)),
|
||||||
|
None => Some(self.q(n)),
|
||||||
|
},
|
||||||
|
ExprKind::Field { expr, name } => match self.alias_target(expr)? {
|
||||||
|
Target::User(p) => Some(format!("{p}.{name}")),
|
||||||
|
Target::C(i) => Some(format!("{}.{name}", self.clibs[*i].prefix)),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1063,7 +1127,266 @@ impl Checker {
|
|||||||
fn module_alias(&self, e: &Expr) -> Option<&'static str> {
|
fn module_alias(&self, e: &Expr) -> Option<&'static str> {
|
||||||
match self.alias_target(e)? {
|
match self.alias_target(e)? {
|
||||||
Target::Builtin(m) => Some(m),
|
Target::Builtin(m) => Some(m),
|
||||||
Target::User(_) => None,
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- C headers ----------
|
||||||
|
|
||||||
|
fn import_c(&mut self, i: &Import) -> PResult<()> {
|
||||||
|
let Some(idx) = self.clibs.iter().position(|l| l.header == i.path) else {
|
||||||
|
return Err(Diag::new(format!("the C header `{}` could not be read", i.path), i.span));
|
||||||
|
};
|
||||||
|
let lib = &self.clibs[idx];
|
||||||
|
match &i.names {
|
||||||
|
None => {
|
||||||
|
let alias = i.alias.clone().unwrap_or_else(|| lib.prefix.clone());
|
||||||
|
if self.imports.contains_key(&alias) {
|
||||||
|
return Err(Diag::new(
|
||||||
|
format!("`{alias}` is imported twice; give the header another name, like `import c{alias} \"{}\"`", i.path),
|
||||||
|
i.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
self.imports.insert(alias, Target::C(idx));
|
||||||
|
}
|
||||||
|
Some(names) => {
|
||||||
|
for (n, span) in names {
|
||||||
|
if !lib.names().contains(&n.as_str()) && lib.func(n).is_none() {
|
||||||
|
let hint = similar(n, &lib.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default();
|
||||||
|
return Err(Diag::new(format!("`{}` has no `{n}`{hint}", lib.header), *span));
|
||||||
|
}
|
||||||
|
if self.imports.contains_key(n) {
|
||||||
|
return Err(Diag::new(format!("`{n}` is imported twice"), *span));
|
||||||
|
}
|
||||||
|
self.imports.insert(n.clone(), Target::CItem(idx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Dino type for a C type, if it has one.
|
||||||
|
fn c_type(&self, lib: usize, t: &CTy) -> Option<Ty> {
|
||||||
|
Some(match t {
|
||||||
|
CTy::Void => Ty::Unit,
|
||||||
|
CTy::Num(t) => *t,
|
||||||
|
CTy::Str => Ty::Str,
|
||||||
|
CTy::Handle(n) if n == "pointer" => Ty::CPtr("pointer"),
|
||||||
|
CTy::Handle(n) => Ty::CPtr(Box::leak(format!("{}.{n}", self.clibs[lib].prefix).into_boxed_str())),
|
||||||
|
CTy::Struct(n) => Ty::Struct(self.structs.get(&format!("{}.{n}", self.clibs[lib].prefix))?.name),
|
||||||
|
CTy::Ptr { .. } | CTy::Unsupported(_) => return None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Structs from C headers become Dino structs (`raylib.Color`), converted at calls.
|
||||||
|
fn register_c_structs(&mut self) {
|
||||||
|
for li in 0..self.clibs.len() {
|
||||||
|
let prefix = self.clibs[li].prefix.clone();
|
||||||
|
for s in self.clibs[li].structs.clone() {
|
||||||
|
let key = format!("{prefix}.{}", s.name);
|
||||||
|
let name: &'static str = Box::leak(key.clone().into_boxed_str());
|
||||||
|
self.structs.insert(key.clone(), StructInfo { name, fields: Vec::new(), methods: HashMap::new(), span: Span::default() });
|
||||||
|
self.c_structs.insert(key, s.c.clone());
|
||||||
|
}
|
||||||
|
for s in self.clibs[li].structs.clone() {
|
||||||
|
let key = format!("{prefix}.{}", s.name);
|
||||||
|
let fields: Vec<(String, Ty)> =
|
||||||
|
s.fields.iter().map(|(n, t)| (n.clone(), self.c_type(li, t).unwrap_or(Ty::CPtr("pointer")))).collect();
|
||||||
|
self.structs.get_mut(&key).unwrap().fields = fields;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The type `name` from C header `lib` (`zlib.gzFile`, `raylib.Color`).
|
||||||
|
fn c_named_type(&self, lib: usize, name: &str, span: Span) -> PResult<Ty> {
|
||||||
|
let l = &self.clibs[lib];
|
||||||
|
if l.strukt(name).is_some() {
|
||||||
|
return self.c_type(lib, &CTy::Struct(name.to_string())).ok_or_else(|| Diag::new("unknown C struct", span));
|
||||||
|
}
|
||||||
|
if l.has_handle(name) {
|
||||||
|
return Ok(self.c_type(lib, &CTy::Handle(name.to_string())).unwrap());
|
||||||
|
}
|
||||||
|
let types: Vec<&str> = l.structs.iter().map(|s| s.name.as_str()).chain(l.handles.iter().map(|h| h.as_str())).collect();
|
||||||
|
let hint = similar(name, &types).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default();
|
||||||
|
Err(Diag::new(format!("`{}` has no type `{name}`{hint}", l.header), span))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A constant from a C header: `zlib.Z_OK`, `raylib.RED`.
|
||||||
|
fn c_value(&self, lib: usize, name: &str, span: Span) -> PResult<HExpr> {
|
||||||
|
let l = &self.clibs[lib];
|
||||||
|
if let Some(c) = l.konst(name) {
|
||||||
|
return Ok(match &c.value {
|
||||||
|
cheader::Value::Int(i) => mk(HExprKind::Int(*i), Ty::Int),
|
||||||
|
cheader::Value::Float(f) => mk(HExprKind::Float(*f), Ty::Float),
|
||||||
|
cheader::Value::Str(s) => mk(HExprKind::Str(s.clone()), Ty::Str),
|
||||||
|
cheader::Value::Struct(sn) => {
|
||||||
|
let ty = self.c_named_type(lib, sn, span)?;
|
||||||
|
mk(HExprKind::CRaw(name.to_string()), ty)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if l.func(name).is_some() {
|
||||||
|
return Err(Diag::new(format!("`{name}` is a C function; call it like `{name}(...)`"), span));
|
||||||
|
}
|
||||||
|
if l.strukt(name).is_some() || l.has_handle(name) {
|
||||||
|
return Err(Diag::new(format!("`{name}` is a C type, not a value"), span));
|
||||||
|
}
|
||||||
|
let hint = similar(name, &l.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default();
|
||||||
|
Err(Diag::new(format!("`{}` has no `{name}`{hint}", l.header), span))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn c_call(&mut self, lib: usize, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult<HExpr> {
|
||||||
|
let l = &self.clibs[lib];
|
||||||
|
let Some(f) = l.func(name).cloned() else {
|
||||||
|
if l.konst(name).is_some() {
|
||||||
|
return Err(Diag::new(format!("`{name}` is a constant, not a function"), name_span));
|
||||||
|
}
|
||||||
|
if l.strukt(name).is_some() {
|
||||||
|
return Err(Diag::new(format!("`{name}` is a C struct; build one with `{name}{{...}}`"), name_span));
|
||||||
|
}
|
||||||
|
let hint = similar(name, &l.names()).map(|s| format!(" (did you mean `{s}`?)")).unwrap_or_default();
|
||||||
|
return Err(Diag::new(format!("`{}` has no function `{name}`{hint}", l.header), name_span));
|
||||||
|
};
|
||||||
|
if let Some(why) = &f.problem {
|
||||||
|
return Err(Diag::new(format!("`{name}` can't be called from Dino: it {why}"), name_span));
|
||||||
|
}
|
||||||
|
if args.len() != f.params.len() {
|
||||||
|
let names: Vec<String> = f
|
||||||
|
.params
|
||||||
|
.iter()
|
||||||
|
.map(|(n, t)| if n.is_empty() { self.c_param_text(lib, t) } else { format!("{n}: {}", self.c_param_text(lib, t)) })
|
||||||
|
.collect();
|
||||||
|
return Err(Diag::new(
|
||||||
|
format!(
|
||||||
|
"`{name}` takes {} argument{} ({}), but {} {} given",
|
||||||
|
f.params.len(),
|
||||||
|
if f.params.len() == 1 { "" } else { "s" },
|
||||||
|
names.join(", "),
|
||||||
|
args.len(),
|
||||||
|
if args.len() == 1 { "was" } else { "were" },
|
||||||
|
),
|
||||||
|
span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let mut hs = Vec::new();
|
||||||
|
let mut modes = Vec::new();
|
||||||
|
for ((pname, pt), a) in f.params.iter().zip(args) {
|
||||||
|
let (h, m) = self.c_arg(lib, name, pname, pt, a)?;
|
||||||
|
hs.push(h);
|
||||||
|
modes.push(m);
|
||||||
|
}
|
||||||
|
let ret = self.c_type(lib, &f.ret).unwrap_or(Ty::CPtr("pointer"));
|
||||||
|
Ok(mk(HExprKind::CCall { name: name.to_string(), args: hs, modes }, ret))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How a C parameter type reads in a message.
|
||||||
|
fn c_param_text(&self, lib: usize, t: &CTy) -> String {
|
||||||
|
match t {
|
||||||
|
CTy::Ptr { to, .. } if matches!(**to, CTy::Struct(_) | CTy::Handle(_)) => {
|
||||||
|
self.c_type(lib, to).map_or("pointer".into(), |t| format!("{t} variable"))
|
||||||
|
}
|
||||||
|
CTy::Ptr { to, konst } => match self.c_type(lib, to) {
|
||||||
|
Some(inner) if *konst => format!("{inner} or []{inner}"),
|
||||||
|
Some(inner) => format!("{inner} variable or []{inner}"),
|
||||||
|
None => "pointer".into(),
|
||||||
|
},
|
||||||
|
t => self.c_type(lib, t).map_or("?".into(), |t| t.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn c_arg(&mut self, lib: usize, fname: &str, pname: &str, pt: &CTy, a: &Expr) -> PResult<(HExpr, CArg)> {
|
||||||
|
let null = || Ok((mk(HExprKind::Bool(false), Ty::Bool), CArg::Null));
|
||||||
|
let is_nil = matches!(a.kind, ExprKind::Nil);
|
||||||
|
let what = if pname.is_empty() { format!("this argument of `{fname}`") } else { format!("`{pname}` of `{fname}`") };
|
||||||
|
match pt {
|
||||||
|
CTy::Str => {
|
||||||
|
if is_nil {
|
||||||
|
return null();
|
||||||
|
}
|
||||||
|
let h = self.expr(a, Some(Ty::Str))?;
|
||||||
|
match h.ty {
|
||||||
|
Ty::Str => Ok((h, CArg::Str)),
|
||||||
|
Ty::List(Ty::Num(Num::U8 | Num::I8)) => Ok((h, CArg::Buffer)),
|
||||||
|
t => Err(Diag::new(format!("{what} is a `string` (or a `[]u8` to fill in), found `{t}`"), a.span)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CTy::Handle(_) => {
|
||||||
|
let want = self.c_type(lib, pt).unwrap();
|
||||||
|
if is_nil {
|
||||||
|
return Ok((mk(HExprKind::Nil, want), CArg::Value));
|
||||||
|
}
|
||||||
|
let h = self.expr(a, Some(want))?;
|
||||||
|
match h.ty {
|
||||||
|
Ty::CPtr(x) if Ty::CPtr(x) == want || x == "pointer" || want == Ty::CPtr("pointer") => Ok((h, CArg::Value)),
|
||||||
|
Ty::List(e) if want == Ty::CPtr("pointer") && (e.is_number() || *e == Ty::Bool) => Ok((h, CArg::Buffer)),
|
||||||
|
Ty::Str if want == Ty::CPtr("pointer") => Ok((h, CArg::Str)),
|
||||||
|
t => Err(Diag::new(format!("{what} is a `{want}`, found `{t}`"), a.span)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CTy::Ptr { to, konst } => {
|
||||||
|
let Some(inner) = self.c_type(lib, to) else {
|
||||||
|
return Err(Diag::new(format!("{what} has a type Dino can't pass"), a.span));
|
||||||
|
};
|
||||||
|
if is_nil {
|
||||||
|
return null();
|
||||||
|
}
|
||||||
|
// `sqlite3 **db`: a pointer variable the function fills in
|
||||||
|
if let Ty::CPtr(want) = inner {
|
||||||
|
let h = self.expr(a, Some(inner))?;
|
||||||
|
let fits = |t: Ty| matches!(t, Ty::CPtr(x) if x == want || x == "pointer" || want == "pointer");
|
||||||
|
if !fits(h.ty) {
|
||||||
|
return Err(Diag::new(format!("{what} is a `{inner}` variable that the function fills in, found `{}`", h.ty), a.span));
|
||||||
|
}
|
||||||
|
if *konst {
|
||||||
|
return Ok((h, CArg::AddrIn));
|
||||||
|
}
|
||||||
|
let thing = if pname.is_empty() { "this argument".to_string() } else { format!("`{pname}`") };
|
||||||
|
let p = self.place(a).map_err(|_| {
|
||||||
|
Diag::new(format!("`{fname}` writes into {thing}, so it must be a variable (or a field of one)"), a.span)
|
||||||
|
})?;
|
||||||
|
return Ok((p, CArg::Addr));
|
||||||
|
}
|
||||||
|
let h = self.expr(a, Some(inner))?;
|
||||||
|
if h.ty == Ty::list(inner) {
|
||||||
|
if matches!(inner, Ty::Struct(_)) {
|
||||||
|
return Err(Diag::new(format!("{what} wants C structs in a row; a Dino list of structs can't be passed"), a.span));
|
||||||
|
}
|
||||||
|
return Ok((h, CArg::Buffer));
|
||||||
|
}
|
||||||
|
if let Ty::CPtr(_) = h.ty {
|
||||||
|
return Ok((h, CArg::Value));
|
||||||
|
}
|
||||||
|
let h = self.coerce(h, inner, a.span).map_err(|_| {
|
||||||
|
Diag::new(
|
||||||
|
format!(
|
||||||
|
"{what} is {} `{inner}` (or a list `[]{inner}`), found something else",
|
||||||
|
if *konst { "a" } else { "a variable of type" }
|
||||||
|
),
|
||||||
|
a.span,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
if *konst {
|
||||||
|
return Ok((h, CArg::AddrIn));
|
||||||
|
}
|
||||||
|
let p = self.place(a).map_err(|_| {
|
||||||
|
let thing = if pname.is_empty() { "this argument".to_string() } else { format!("`{pname}`") };
|
||||||
|
Diag::new(format!("`{fname}` writes into {thing}, so it must be a variable (or a field of one)"), a.span)
|
||||||
|
})?;
|
||||||
|
Ok((p, CArg::Addr))
|
||||||
|
}
|
||||||
|
CTy::Num(_) | CTy::Struct(_) => {
|
||||||
|
let want = self.c_type(lib, pt).unwrap();
|
||||||
|
// a C `char` takes a Dino character: `'a'`
|
||||||
|
if matches!(want, Ty::Num(Num::I8 | Num::U8)) {
|
||||||
|
let h = self.expr(a, Some(want))?;
|
||||||
|
if h.ty == Ty::Char {
|
||||||
|
return Ok((mk(HExprKind::Convert(Box::new(h)), want), CArg::Value));
|
||||||
|
}
|
||||||
|
return Ok((self.coerce(h, want, a.span)?, CArg::Value));
|
||||||
|
}
|
||||||
|
Ok((self.expr_as(a, want)?, CArg::Value))
|
||||||
|
}
|
||||||
|
CTy::Void | CTy::Unsupported(_) => Err(Diag::new(format!("{what} has a type Dino can't pass"), a.span)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1281,6 +1604,10 @@ impl Checker {
|
|||||||
"f64" => Ok(Ty::Float),
|
"f64" => Ok(Ty::Float),
|
||||||
n if Num::ALL.iter().any(|k| k.name() == n) => Ok(conversion_target(n).unwrap()),
|
n if Num::ALL.iter().any(|k| k.name() == n) => Ok(conversion_target(n).unwrap()),
|
||||||
n if self.tsubst.contains_key(n) => Ok(self.tsubst[n]),
|
n if self.tsubst.contains_key(n) => Ok(self.tsubst[n]),
|
||||||
|
n if matches!(self.imports.get(n), Some(Target::CItem(..))) => {
|
||||||
|
let Some(Target::CItem(lib)) = self.imports.get(n) else { unreachable!() };
|
||||||
|
self.c_named_type(*lib, n, span)
|
||||||
|
}
|
||||||
n => {
|
n => {
|
||||||
let key = self.q(n);
|
let key = self.q(n);
|
||||||
if self.ifaces.contains_key(&key) {
|
if self.ifaces.contains_key(&key) {
|
||||||
@@ -1333,6 +1660,10 @@ impl Checker {
|
|||||||
}
|
}
|
||||||
self.instantiate_type(&key, &shown, tys, span)
|
self.instantiate_type(&key, &shown, tys, span)
|
||||||
}
|
}
|
||||||
|
Type::Named { path, args } if path.len() == 2 && args.is_empty() && matches!(self.imports.get(&path[0]), Some(Target::C(_))) => {
|
||||||
|
let Some(Target::C(lib)) = self.imports.get(&path[0]) else { unreachable!() };
|
||||||
|
self.c_named_type(*lib, &path[1], span)
|
||||||
|
}
|
||||||
Type::Named { path, args } if path.len() == 2 && args.is_empty() => {
|
Type::Named { path, args } if path.len() == 2 && args.is_empty() => {
|
||||||
let Some(Target::User(module)) = self.imports.get(&path[0]) else {
|
let Some(Target::User(module)) = self.imports.get(&path[0]) else {
|
||||||
return Err(Diag::new(format!("unknown type `{}.{}` (is `{}` imported?)", path[0], path[1], path[0]), span));
|
return Err(Diag::new(format!("unknown type `{}.{}` (is `{}` imported?)", path[0], path[1], path[0]), span));
|
||||||
@@ -1496,7 +1827,7 @@ impl Checker {
|
|||||||
},
|
},
|
||||||
Ty::List(_) => HExprKind::ListLit { elems: Vec::new() },
|
Ty::List(_) => HExprKind::ListLit { elems: Vec::new() },
|
||||||
Ty::Map(..) => HExprKind::MapLit { entries: Vec::new() },
|
Ty::Map(..) => HExprKind::MapLit { entries: Vec::new() },
|
||||||
Ty::Error | Ty::Opt(_) | Ty::Weak(_) | Ty::Shared(_) | Ty::Func(_) | Ty::Iface(_) => HExprKind::Nil,
|
Ty::Error | Ty::Opt(_) | Ty::Weak(_) | Ty::Shared(_) | Ty::Func(_) | Ty::Iface(_) | Ty::CPtr(_) => HExprKind::Nil,
|
||||||
Ty::Tuple(items) => HExprKind::TupleLit(items.iter().map(|t| self.zero_expr(*t)).collect()),
|
Ty::Tuple(items) => HExprKind::TupleLit(items.iter().map(|t| self.zero_expr(*t)).collect()),
|
||||||
Ty::Unit => unreachable!("nothing has no zero value"),
|
Ty::Unit => unreachable!("nothing has no zero value"),
|
||||||
};
|
};
|
||||||
@@ -2218,7 +2549,7 @@ impl Checker {
|
|||||||
Some(h) => h,
|
Some(h) => h,
|
||||||
None => self.expr(other, None)?,
|
None => self.expr(other, None)?,
|
||||||
};
|
};
|
||||||
if !matches!(h.ty, Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_)) {
|
if !matches!(h.ty, Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_) | Ty::CPtr(_)) {
|
||||||
return Err(Diag::new(
|
return Err(Diag::new(
|
||||||
format!("a `{}` can never be nil, so comparing it with `nil` is pointless", h.ty),
|
format!("a `{}` can never be nil, so comparing it with `nil` is pointless", h.ty),
|
||||||
e.span,
|
e.span,
|
||||||
@@ -2253,6 +2584,10 @@ impl Checker {
|
|||||||
let m = self.module_alias(expr).unwrap();
|
let m = self.module_alias(expr).unwrap();
|
||||||
self.module_value(m, name, e.span)
|
self.module_value(m, name, e.span)
|
||||||
}
|
}
|
||||||
|
ExprKind::Field { expr, name } if self.c_alias(expr).is_some() => {
|
||||||
|
let lib = self.c_alias(expr).unwrap();
|
||||||
|
self.c_value(lib, name, e.span)
|
||||||
|
}
|
||||||
ExprKind::Generic { .. } => Err(Diag::new(
|
ExprKind::Generic { .. } => Err(Diag::new(
|
||||||
"a generic type is not a value; build one with `Name[types]{...}` or pick an enum case with `Name[types].Case`",
|
"a generic type is not a value; build one with `Name[types]{...}` or pick an enum case with `Name[types].Case`",
|
||||||
e.span,
|
e.span,
|
||||||
@@ -2315,7 +2650,7 @@ impl Checker {
|
|||||||
e.span,
|
e.span,
|
||||||
)),
|
)),
|
||||||
ExprKind::Nil => match expect {
|
ExprKind::Nil => match expect {
|
||||||
Some(t @ (Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_))) => Ok(mk(HExprKind::Nil, t)),
|
Some(t @ (Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_) | Ty::CPtr(_))) => Ok(mk(HExprKind::Nil, t)),
|
||||||
_ => Err(Diag::new(
|
_ => Err(Diag::new(
|
||||||
"can't tell what `nil` stands for here; it needs a type like `User?` or `error` (for example `return value, nil`)",
|
"can't tell what `nil` stands for here; it needs a type like `User?` or `error` (for example `return value, nil`)",
|
||||||
e.span,
|
e.span,
|
||||||
@@ -2469,6 +2804,9 @@ impl Checker {
|
|||||||
if let Some(c) = self.consts.get(&key) {
|
if let Some(c) = self.consts.get(&key) {
|
||||||
return Ok(c.clone());
|
return Ok(c.clone());
|
||||||
}
|
}
|
||||||
|
if let Some(lib) = self.c_item(name) {
|
||||||
|
return self.c_value(lib, name, span);
|
||||||
|
}
|
||||||
if self.funcs.contains_key(&key) {
|
if self.funcs.contains_key(&key) {
|
||||||
return self.func_value(&key, name, span);
|
return self.func_value(&key, name, span);
|
||||||
}
|
}
|
||||||
@@ -2860,7 +3198,7 @@ impl Checker {
|
|||||||
BinOp::Sub | BinOp::Mul | BinOp::Div => t.is_number(),
|
BinOp::Sub | BinOp::Mul | BinOp::Div => t.is_number(),
|
||||||
BinOp::Rem => t.is_int(),
|
BinOp::Rem => t.is_int(),
|
||||||
BinOp::Eq | BinOp::Ne => {
|
BinOp::Eq | BinOp::Ne => {
|
||||||
t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char)
|
t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char | Ty::CPtr(_))
|
||||||
|| matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty()))
|
|| matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty()))
|
||||||
}
|
}
|
||||||
BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => t.is_number() || t == Ty::Str || t == Ty::Char,
|
BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => t.is_number() || t == Ty::Str || t == Ty::Char,
|
||||||
@@ -3132,6 +3470,14 @@ impl Checker {
|
|||||||
|
|
||||||
fn call(&mut self, callee: &Expr, args: &[Expr], span: Span) -> PResult<HExpr> {
|
fn call(&mut self, callee: &Expr, args: &[Expr], span: Span) -> PResult<HExpr> {
|
||||||
match &callee.kind {
|
match &callee.kind {
|
||||||
|
ExprKind::Field { expr, name } if self.c_alias(expr).is_some() => {
|
||||||
|
let lib = self.c_alias(expr).unwrap();
|
||||||
|
self.c_call(lib, name, args, span, callee.span)
|
||||||
|
}
|
||||||
|
ExprKind::Ident(name) if self.c_item(name).is_some() => {
|
||||||
|
let lib = self.c_item(name).unwrap();
|
||||||
|
self.c_call(lib, name, args, span, callee.span)
|
||||||
|
}
|
||||||
ExprKind::Field { expr, name } if self.module_alias(expr).is_some() => {
|
ExprKind::Field { expr, name } if self.module_alias(expr).is_some() => {
|
||||||
let m = self.module_alias(expr).unwrap();
|
let m = self.module_alias(expr).unwrap();
|
||||||
self.module_call(m, name, args, span, callee.span)
|
self.module_call(m, name, args, span, callee.span)
|
||||||
@@ -3821,3 +4167,31 @@ fn check_mutating_calls(funcs: &[HFunc]) -> PResult<()> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The name in `names` closest to `name`, if one is close enough to be a likely typo.
|
||||||
|
fn similar<'a>(name: &str, names: &[&'a str]) -> Option<&'a str> {
|
||||||
|
fn dist(a: &str, b: &str) -> usize {
|
||||||
|
let (a, b): (Vec<char>, Vec<char>) = (a.chars().collect(), b.chars().collect());
|
||||||
|
let mut prev: Vec<usize> = (0..=b.len()).collect();
|
||||||
|
let mut prev2: Vec<usize> = prev.clone();
|
||||||
|
for i in 1..=a.len() {
|
||||||
|
let mut cur = vec![i; b.len() + 1];
|
||||||
|
for j in 1..=b.len() {
|
||||||
|
let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
|
||||||
|
cur[j] = (prev[j] + 1).min(cur[j - 1] + 1).min(prev[j - 1] + cost);
|
||||||
|
// two letters swapped count as one mistake
|
||||||
|
if i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1] {
|
||||||
|
cur[j] = cur[j].min(prev2[j - 2] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev2 = std::mem::replace(&mut prev, cur);
|
||||||
|
}
|
||||||
|
prev[b.len()]
|
||||||
|
}
|
||||||
|
let lower = name.to_lowercase();
|
||||||
|
if let Some(n) = names.iter().find(|n| n.to_lowercase() == lower) {
|
||||||
|
return Some(n);
|
||||||
|
}
|
||||||
|
let limit = (name.chars().count() / 3).max(1);
|
||||||
|
names.iter().map(|n| (dist(&lower, &n.to_lowercase()), *n)).filter(|(d, _)| *d <= limit).min_by_key(|(d, _)| *d).map(|(_, n)| n)
|
||||||
|
}
|
||||||
+153
-3
@@ -52,6 +52,7 @@ impl Info {
|
|||||||
let mut found = false;
|
let mut found = false;
|
||||||
walk_expr(e, &mut |_| {}, &mut |x| match &x.kind {
|
walk_expr(e, &mut |_| {}, &mut |x| match &x.kind {
|
||||||
HExprKind::ListPush { .. } | HExprKind::ListPop(_) | HExprKind::MapCall { op: MapOp::Remove, .. } => found = true,
|
HExprKind::ListPush { .. } | HExprKind::ListPop(_) | HExprKind::MapCall { op: MapOp::Remove, .. } => found = true,
|
||||||
|
HExprKind::CCall { modes, .. } if modes.iter().any(|m| matches!(m, CArg::Addr | CArg::Buffer)) => found = true,
|
||||||
HExprKind::MethodCall { owner, method, .. } if self.is_mut_method(owner, method) => found = true,
|
HExprKind::MethodCall { owner, method, .. } if self.is_mut_method(owner, method) => found = true,
|
||||||
_ => {}
|
_ => {}
|
||||||
});
|
});
|
||||||
@@ -118,9 +119,32 @@ pub fn generate(p: &HProgram) -> String {
|
|||||||
}
|
}
|
||||||
let wrappers = collect_wrappers(p);
|
let wrappers = collect_wrappers(p);
|
||||||
let main_fails = p.funcs.iter().any(|f| f.owner.is_none() && f.name == "main" && f.ret == Ty::Error);
|
let main_fails = p.funcs.iter().any(|f| f.owner.is_none() && f.name == "main" && f.ret == Ty::Error);
|
||||||
let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new() };
|
let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new(), c_structs: HashMap::new() };
|
||||||
|
for t in &p.types {
|
||||||
|
if let HTypeDef::Struct(HStruct { name, c: Some(c), .. }) = t {
|
||||||
|
g.c_structs.insert(name.clone(), c.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// what building needs, for the driver (one item per line, so paths may contain spaces)
|
||||||
|
for f in &p.c.cflags {
|
||||||
|
g.out.push_str(&format!("// dino-cflag: {f}\n"));
|
||||||
|
}
|
||||||
|
for f in &p.c.libs {
|
||||||
|
g.out.push_str(&format!("// dino-lib: {f}\n"));
|
||||||
|
}
|
||||||
|
for f in &p.c.sources {
|
||||||
|
g.out.push_str(&format!("// dino-source: {f}\n"));
|
||||||
|
}
|
||||||
|
if p.c.headers.is_empty() {
|
||||||
g.out.push_str(RUNTIME);
|
g.out.push_str(RUNTIME);
|
||||||
|
} else {
|
||||||
|
// C headers are read (and built) in GNU C mode, with the whole C library visible
|
||||||
|
g.out.push_str(&RUNTIME.replacen("#define _POSIX_C_SOURCE 200809L\n", "", 1));
|
||||||
|
}
|
||||||
g.out.push('\n');
|
g.out.push('\n');
|
||||||
|
for h in &p.c.headers {
|
||||||
|
g.out.push_str(&format!("#include \"{h}\"\n"));
|
||||||
|
}
|
||||||
|
|
||||||
// forward declarations, then bodies: lists first (they only hold pointers), then structs and enums
|
// forward declarations, then bodies: lists first (they only hold pointers), then structs and enums
|
||||||
for t in &p.types {
|
for t in &p.types {
|
||||||
@@ -264,7 +288,10 @@ pub fn generate(p: &HProgram) -> String {
|
|||||||
}
|
}
|
||||||
for t in &p.types {
|
for t in &p.types {
|
||||||
match t {
|
match t {
|
||||||
HTypeDef::Struct(s) => g.struct_helpers(s),
|
HTypeDef::Struct(s) => {
|
||||||
|
g.struct_helpers(s);
|
||||||
|
g.c_struct_conversions(s);
|
||||||
|
}
|
||||||
HTypeDef::Enum(e) => g.enum_helpers(e),
|
HTypeDef::Enum(e) => g.enum_helpers(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -481,6 +508,7 @@ fn tag(t: Ty) -> String {
|
|||||||
tag(ft.ret)
|
tag(ft.ret)
|
||||||
),
|
),
|
||||||
Ty::Tuple(items) => format!("T_{}", items.iter().map(|t| tag(*t)).collect::<Vec<_>>().join("_")),
|
Ty::Tuple(items) => format!("T_{}", items.iter().map(|t| tag(*t)).collect::<Vec<_>>().join("_")),
|
||||||
|
Ty::CPtr(_) => "ptr".into(),
|
||||||
Ty::Unit => "void".into(),
|
Ty::Unit => "void".into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -496,6 +524,7 @@ fn c_ty(t: Ty) -> String {
|
|||||||
Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Tuple(_) | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => tag(t),
|
Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Tuple(_) | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => tag(t),
|
||||||
Ty::Num(n) => num_c(n).into(),
|
Ty::Num(n) => num_c(n).into(),
|
||||||
Ty::Error => "dino_err".into(),
|
Ty::Error => "dino_err".into(),
|
||||||
|
Ty::CPtr(_) => "void *".into(),
|
||||||
Ty::Unit => "void".into(),
|
Ty::Unit => "void".into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -599,6 +628,7 @@ fn to_str(ty: Ty, c: &str) -> String {
|
|||||||
Ty::Num(Num::F32) => format!("dino_f32_str({c})"),
|
Ty::Num(Num::F32) => format!("dino_f32_str({c})"),
|
||||||
Ty::Num(_) => format!("dino_i64_str((int64_t)({c}))"),
|
Ty::Num(_) => format!("dino_i64_str((int64_t)({c}))"),
|
||||||
Ty::Error => format!("dino_err_str({c})"),
|
Ty::Error => format!("dino_err_str({c})"),
|
||||||
|
Ty::CPtr(_) => format!("dino_ptr_str({c})"),
|
||||||
Ty::Tuple(_) => unreachable!("several values can't be turned into text"),
|
Ty::Tuple(_) => unreachable!("several values can't be turned into text"),
|
||||||
Ty::Unit => unreachable!("nothing can't be turned into text"),
|
Ty::Unit => unreachable!("nothing can't be turned into text"),
|
||||||
}
|
}
|
||||||
@@ -626,6 +656,8 @@ struct Gen {
|
|||||||
closures: u32,
|
closures: u32,
|
||||||
closure_protos: String,
|
closure_protos: String,
|
||||||
closure_defs: String,
|
closure_defs: String,
|
||||||
|
/// Structs from C headers: table name -> how C spells the type.
|
||||||
|
c_structs: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -769,6 +801,116 @@ impl Gen {
|
|||||||
self.out.push_str("};\n\n");
|
self.out.push_str("};\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For a struct from a C header: `S_raylib_Color_to_c` and `S_raylib_Color_from_c`, which copy
|
||||||
|
/// the fields between Dino's struct and the C one.
|
||||||
|
fn c_struct_conversions(&mut self, s: &HStruct) {
|
||||||
|
let Some(cname) = &s.c else { return };
|
||||||
|
let c = c_struct(&s.name);
|
||||||
|
let mut to = Vec::new();
|
||||||
|
let mut from = String::new();
|
||||||
|
for (name, ty) in &s.fields {
|
||||||
|
let f = field_name(name);
|
||||||
|
let (into_c, out_of_c) = match ty {
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(*n) => {
|
||||||
|
(format!("{}_to_c(v.{f})", c_struct(n)), format!("{}_from_c(v.{name})", c_struct(n)))
|
||||||
|
}
|
||||||
|
Ty::CPtr(_) => (format!("(void *)v.{f}"), format!("(void *)v.{name}")),
|
||||||
|
t => (format!("({})v.{f}", c_ty(*t)), format!("({})v.{name}", c_ty(*t))),
|
||||||
|
};
|
||||||
|
to.push(format!(".{name} = {into_c}"));
|
||||||
|
from.push_str(&format!(" r.{f} = {out_of_c};\n"));
|
||||||
|
}
|
||||||
|
self.out.push_str(&format!(
|
||||||
|
"static {cname} {c}_to_c({c} v) {{\n {cname} r = {{ {} }};\n return r;\n}}\n\n\
|
||||||
|
static {c} {c}_from_c({cname} v) {{\n {c} r;\n{from} return r;\n}}\n\n",
|
||||||
|
to.join(", ")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A Dino value as the C function wants it.
|
||||||
|
fn to_c_value(&self, ty: Ty, v: &str) -> String {
|
||||||
|
match ty {
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_to_c({v})", c_struct(n)),
|
||||||
|
_ => v.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The C type a value is converted to for a C function.
|
||||||
|
fn c_side_ty(&self, ty: Ty) -> String {
|
||||||
|
match ty {
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(n) => self.c_structs[n].clone(),
|
||||||
|
t => c_ty(t),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn c_call(&mut self, name: &str, args: &[HExpr], modes: &[CArg], ty: Ty) -> String {
|
||||||
|
let vals: Vec<&HExpr> =
|
||||||
|
args.iter().zip(modes).filter(|(_, m)| !matches!(m, CArg::Addr | CArg::Null)).map(|(a, _)| a).collect();
|
||||||
|
let mut vs = self.seq(&vals).into_iter();
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
let mut after = Vec::new();
|
||||||
|
for (a, m) in args.iter().zip(modes) {
|
||||||
|
match m {
|
||||||
|
CArg::Null => parts.push("NULL".to_string()),
|
||||||
|
CArg::Value => {
|
||||||
|
let v = vs.next().unwrap();
|
||||||
|
parts.push(self.to_c_value(a.ty, &v));
|
||||||
|
}
|
||||||
|
CArg::Str => {
|
||||||
|
let v = vs.next().unwrap();
|
||||||
|
let t = self.fresh();
|
||||||
|
self.line(&format!("char *{t} = dino_cstr({v});"));
|
||||||
|
after.push(format!("free({t});"));
|
||||||
|
parts.push(t);
|
||||||
|
}
|
||||||
|
CArg::Buffer => {
|
||||||
|
let v = vs.next().unwrap();
|
||||||
|
parts.push(format!("(void *)({v}).p"));
|
||||||
|
}
|
||||||
|
CArg::AddrIn => {
|
||||||
|
let v = vs.next().unwrap();
|
||||||
|
let t = self.fresh();
|
||||||
|
let init = self.to_c_value(a.ty, &v);
|
||||||
|
self.line(&format!("{} {t} = {init};", self.c_side_ty(a.ty)));
|
||||||
|
parts.push(format!("(void *)&{t}"));
|
||||||
|
}
|
||||||
|
CArg::Addr => {
|
||||||
|
let p = self.place_str(a);
|
||||||
|
match a.ty {
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(n) => {
|
||||||
|
let t = self.fresh();
|
||||||
|
self.line(&format!("{} {t} = {}_to_c({p});", self.c_structs[n], c_struct(n)));
|
||||||
|
after.push(format!("{p} = {}_from_c({t});", c_struct(n)));
|
||||||
|
parts.push(format!("(void *)&{t}"));
|
||||||
|
}
|
||||||
|
// `void *` fits every pointer parameter (`long *` and `int64_t *` may differ in C)
|
||||||
|
_ => parts.push(format!("(void *)&{p}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let call = format!("{name}({})", parts.join(", "));
|
||||||
|
let result = if ty == Ty::Unit {
|
||||||
|
self.line(&format!("{call};"));
|
||||||
|
String::new()
|
||||||
|
} else {
|
||||||
|
let conv = match ty {
|
||||||
|
Ty::Str => format!("dino_from_cstr({call})"),
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_from_c({call})", c_struct(n)),
|
||||||
|
Ty::CPtr(_) => format!("(void *)({call})"),
|
||||||
|
t => format!("({})({call})", c_ty(t)),
|
||||||
|
};
|
||||||
|
let t = self.fresh();
|
||||||
|
self.line(&format!("{} {t} = {conv};", c_ty(ty)));
|
||||||
|
self.register_temp(&t, ty);
|
||||||
|
t
|
||||||
|
};
|
||||||
|
for l in after {
|
||||||
|
self.line(&l);
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
/// `S_Vec2_str` (how a struct looks when printed) and `S_Vec2_copy` (if it holds lists).
|
/// `S_Vec2_str` (how a struct looks when printed) and `S_Vec2_copy` (if it holds lists).
|
||||||
fn struct_helpers(&mut self, s: &HStruct) {
|
fn struct_helpers(&mut self, s: &HStruct) {
|
||||||
let c = c_struct(&s.name);
|
let c = c_struct(&s.name);
|
||||||
@@ -1976,7 +2118,13 @@ static dstr {c}_str({c} l) {{
|
|||||||
| HExprKind::Field { .. }
|
| HExprKind::Field { .. }
|
||||||
| HExprKind::Index { .. }
|
| HExprKind::Index { .. }
|
||||||
| HExprKind::Unwrap(_) => self.place_str(e),
|
| HExprKind::Unwrap(_) => self.place_str(e),
|
||||||
|
HExprKind::Nil if matches!(e.ty, Ty::CPtr(_)) => "((void *)0)".to_string(),
|
||||||
HExprKind::Nil => format!("(({}){{0}})", c_ty(e.ty)),
|
HExprKind::Nil => format!("(({}){{0}})", c_ty(e.ty)),
|
||||||
|
HExprKind::CCall { name, args, modes } => self.c_call(name, args, modes, e.ty),
|
||||||
|
HExprKind::CRaw(text) => match e.ty {
|
||||||
|
Ty::Struct(n) if self.c_structs.contains_key(n) => format!("{}_from_c({text})", c_struct(n)),
|
||||||
|
_ => format!("({text})"),
|
||||||
|
},
|
||||||
HExprKind::Wrap(inner) => {
|
HExprKind::Wrap(inner) => {
|
||||||
let v = self.expr(inner);
|
let v = self.expr(inner);
|
||||||
if self.heap(inner.ty) {
|
if self.heap(inner.ty) {
|
||||||
@@ -2064,6 +2212,8 @@ static dstr {c}_str({c} l) {{
|
|||||||
format!("{}_alive({v})", c_ty(inner.ty))
|
format!("{}_alive({v})", c_ty(inner.ty))
|
||||||
} else if inner.ty == Ty::Error {
|
} else if inner.ty == Ty::Error {
|
||||||
format!("(({v}).set)")
|
format!("(({v}).set)")
|
||||||
|
} else if matches!(inner.ty, Ty::CPtr(_)) {
|
||||||
|
format!("(({v}) != NULL)")
|
||||||
} else {
|
} else {
|
||||||
format!("(({v}).has)")
|
format!("(({v}).has)")
|
||||||
}
|
}
|
||||||
@@ -2191,7 +2341,7 @@ static dstr {c}_str({c} l) {{
|
|||||||
Ty::Num(Num::U64) => format!("dino_print_u64({v});"),
|
Ty::Num(Num::U64) => format!("dino_print_u64({v});"),
|
||||||
Ty::Num(Num::F32) => format!("dino_print_f32({v});"),
|
Ty::Num(Num::F32) => format!("dino_print_f32({v});"),
|
||||||
Ty::Num(_) => format!("dino_print_i64((int64_t)({v}));"),
|
Ty::Num(_) => format!("dino_print_i64((int64_t)({v}));"),
|
||||||
Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => {
|
Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) | Ty::CPtr(_) => {
|
||||||
let t = self.fresh();
|
let t = self.fresh();
|
||||||
self.line(&format!("dstr {t} = {};", to_str(a.ty, v)));
|
self.line(&format!("dstr {t} = {};", to_str(a.ty, v)));
|
||||||
self.register_temp(&t, Ty::Str);
|
self.register_temp(&t, Ty::Str);
|
||||||
|
|||||||
+104
-17
@@ -5,13 +5,40 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use crate::ast::{Item, Program};
|
||||||
use crate::error::{Diag, PResult};
|
use crate::error::{Diag, PResult};
|
||||||
use crate::{check, codegen_c, loader, parser};
|
use crate::{cheader, check, codegen_c, loader, parser};
|
||||||
|
|
||||||
/// Dino source -> C source, for a program in a single file.
|
/// Read the C headers imported by the program. `dir` is the folder of the main file.
|
||||||
|
pub fn load_c_headers(main: &Program, modules: &[(String, Program)], dir: &Path) -> PResult<Vec<cheader::Lib>> {
|
||||||
|
let mut libs: Vec<cheader::Lib> = Vec::new();
|
||||||
|
let all = std::iter::once(("", main)).chain(modules.iter().map(|(k, p)| (k.as_str(), p)));
|
||||||
|
for (key, p) in all {
|
||||||
|
// `import a.b` lives in a/b.dino: its headers are found next to it
|
||||||
|
let mut here = dir.to_path_buf();
|
||||||
|
let parts: Vec<&str> = key.split('.').collect();
|
||||||
|
for part in &parts[..parts.len().saturating_sub(1)] {
|
||||||
|
here.push(part);
|
||||||
|
}
|
||||||
|
for item in &p.items {
|
||||||
|
let Item::Import(i) = item else { continue };
|
||||||
|
if !i.c || libs.iter().any(|l| l.header == i.path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let lib = cheader::load(&i.path, &here, i.link.as_deref())
|
||||||
|
.map_err(|m| Diag::new(m, i.span).in_module(key))?;
|
||||||
|
libs.push(lib);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(libs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dino source -> C source, for a program in a single file. C headers it imports are looked
|
||||||
|
/// for in the current folder and the system's.
|
||||||
pub fn compile_to_c(src: &str) -> PResult<String> {
|
pub fn compile_to_c(src: &str) -> PResult<String> {
|
||||||
let ast = parser::parse(src)?;
|
let ast = parser::parse(src)?;
|
||||||
let hir = check::check(&ast)?;
|
let libs = load_c_headers(&ast, &[], Path::new("."))?;
|
||||||
|
let hir = check::check_project_c(&ast, &[], libs)?;
|
||||||
Ok(codegen_c::generate(&hir))
|
Ok(codegen_c::generate(&hir))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +67,9 @@ pub fn compile_project(
|
|||||||
let loaded = loaded.map_err(&render)?;
|
let loaded = loaded.map_err(&render)?;
|
||||||
let modules: Vec<(String, crate::ast::Program)> =
|
let modules: Vec<(String, crate::ast::Program)> =
|
||||||
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
|
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
|
||||||
let hir = check::check_project(&loaded.main, &modules).map_err(&render)?;
|
let dir = Path::new(main_path).parent().map(Path::to_path_buf).unwrap_or_default();
|
||||||
|
let libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?;
|
||||||
|
let hir = check::check_project_c(&loaded.main, &modules, libs).map_err(&render)?;
|
||||||
Ok(codegen_c::generate(&hir))
|
Ok(codegen_c::generate(&hir))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,24 +102,80 @@ pub fn build_exe(c_code: &str, out: &Path) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings).
|
/// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings).
|
||||||
|
/// The generated code says which C libraries it needs in `// dino-...:` lines at the top.
|
||||||
pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> {
|
pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> {
|
||||||
|
let (mut cflags, mut libs, mut sources) = (Vec::new(), Vec::new(), Vec::new());
|
||||||
|
for line in c_code.lines().take_while(|l| l.starts_with("// dino-")) {
|
||||||
|
let (kind, value) = line["// dino-".len()..].split_once(": ").unwrap_or(("", ""));
|
||||||
|
match kind {
|
||||||
|
"cflag" => cflags.push(value.to_string()),
|
||||||
|
"lib" => libs.push(value.to_string()),
|
||||||
|
"source" => sources.push(PathBuf::from(value)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let uses_c = c_code.contains("\n#include \"");
|
||||||
|
// C headers often use GNU extensions
|
||||||
|
let flags: Vec<&str> = flags.iter().map(|f| if uses_c && *f == "-std=c11" { "-std=gnu11" } else { *f }).collect();
|
||||||
|
let cc = cheader::cc();
|
||||||
|
// C++ files are compiled on their own, then linked in with the C++ library
|
||||||
|
let mut objects: Vec<PathBuf> = Vec::new();
|
||||||
|
let mut c_sources: Vec<PathBuf> = Vec::new();
|
||||||
|
let cleanup = |objects: &[PathBuf]| {
|
||||||
|
for o in objects {
|
||||||
|
let _ = std::fs::remove_file(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for src in &sources {
|
||||||
|
let ext = src.extension().and_then(|e| e.to_str()).unwrap_or("");
|
||||||
|
if ext == "c" {
|
||||||
|
c_sources.push(src.clone());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let obj = unique_name("cpp", ".o");
|
||||||
|
let cxx = std::env::var("CXX").unwrap_or_else(|_| "c++".to_string());
|
||||||
|
let r = Command::new(&cxx).args(["-c", "-O2", "-w"]).args(&cflags).arg(src).arg("-o").arg(&obj).output();
|
||||||
|
match r {
|
||||||
|
Err(e) => {
|
||||||
|
cleanup(&objects);
|
||||||
|
return Err(format!("cannot run the C++ compiler `{cxx}` for {}: {e}", src.display()));
|
||||||
|
}
|
||||||
|
Ok(o) if !o.status.success() => {
|
||||||
|
cleanup(&objects);
|
||||||
|
return Err(format!("{} does not compile:\n{}", src.display(), String::from_utf8_lossy(&o.stderr)));
|
||||||
|
}
|
||||||
|
Ok(_) => objects.push(obj),
|
||||||
|
}
|
||||||
|
}
|
||||||
let c_file = unique_name("prog", ".c");
|
let c_file = unique_name("prog", ".c");
|
||||||
std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?;
|
std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?;
|
||||||
let cc = std::env::var("CC").unwrap_or_else(|_| "cc".to_string());
|
let mut cmd = Command::new(&cc);
|
||||||
let result = Command::new(&cc)
|
cmd.args(&flags).args(&cflags).arg("-o").arg(out).arg(&c_file);
|
||||||
.args(flags)
|
for s in &c_sources {
|
||||||
.arg("-o")
|
// the user's own C file: warnings are theirs to see, but not ours to fail on
|
||||||
.arg(out)
|
cmd.arg(s);
|
||||||
.arg(&c_file)
|
}
|
||||||
.arg("-lm") // the math functions
|
cmd.args(&objects).args(&libs).arg("-lm"); // the math functions
|
||||||
.output();
|
if !objects.is_empty() {
|
||||||
|
cmd.arg("-lstdc++");
|
||||||
|
}
|
||||||
|
let result = cmd.output();
|
||||||
let _ = std::fs::remove_file(&c_file);
|
let _ = std::fs::remove_file(&c_file);
|
||||||
|
cleanup(&objects);
|
||||||
match result {
|
match result {
|
||||||
Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")),
|
Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")),
|
||||||
Ok(o) if !o.status.success() => Err(format!(
|
Ok(o) if !o.status.success() => {
|
||||||
"the C compiler rejected the generated code (this is a bug in Dino):\n{}",
|
let err = String::from_utf8_lossy(&o.stderr);
|
||||||
String::from_utf8_lossy(&o.stderr)
|
if err.contains("undefined reference") || err.contains("cannot find -l") || err.contains("Undefined symbols") || err.contains("ld returned") {
|
||||||
)),
|
Err(format!(
|
||||||
|
"linking with the C library failed:\n{err}\nIf a function is missing, name its library in the import: `import \"header.h\" link \"name\"` (for libname.so)."
|
||||||
|
))
|
||||||
|
} else if uses_c {
|
||||||
|
Err(format!("the C compiler rejected the program:\n{err}"))
|
||||||
|
} else {
|
||||||
|
Err(format!("the C compiler rejected the generated code (this is a bug in Dino):\n{err}"))
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,11 +185,13 @@ pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), St
|
|||||||
/// The error's `file` says which module it is in (`None`: the main file).
|
/// The error's `file` says which module it is in (`None`: the main file).
|
||||||
pub fn diagnose(
|
pub fn diagnose(
|
||||||
main_src: &str,
|
main_src: &str,
|
||||||
|
dir: &Path,
|
||||||
mut read: impl FnMut(&str) -> Option<(String, String)>,
|
mut read: impl FnMut(&str) -> Option<(String, String)>,
|
||||||
) -> Result<(), Diag> {
|
) -> Result<(), Diag> {
|
||||||
let loaded = loader::load(main_src, &mut |key| read(key))?;
|
let loaded = loader::load(main_src, &mut |key| read(key))?;
|
||||||
let modules: Vec<(String, crate::ast::Program)> =
|
let modules: Vec<(String, crate::ast::Program)> =
|
||||||
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
|
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
|
||||||
check::check_project(&loaded.main, &modules)?;
|
let libs = load_c_headers(&loaded.main, &modules, dir)?;
|
||||||
|
check::check_project_c(&loaded.main, &modules, libs)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
+4
-3
@@ -35,7 +35,7 @@ const KEYWORDS: &[&str] = &[
|
|||||||
];
|
];
|
||||||
|
|
||||||
/// Words that make a following `{` open a block (rather than a struct or map literal).
|
/// Words that make a following `{` open a block (rather than a struct or map literal).
|
||||||
const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", "interface", "else"];
|
const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", "interface", "else", "import"];
|
||||||
|
|
||||||
const BINARY: &[&str] = &[
|
const BINARY: &[&str] = &[
|
||||||
"+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=", "%=", "=>",
|
"+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=", "%=", "=>",
|
||||||
@@ -434,14 +434,15 @@ fn gap(prev: &Piece, cur: &Piece, before_prev: Option<&Piece>, prev_is_prefix: b
|
|||||||
}
|
}
|
||||||
"(" => {
|
"(" => {
|
||||||
return match prev.kind {
|
return match prev.kind {
|
||||||
Kind::Word => is_keyword(prev) && prev.text != "func",
|
// `step` is only a keyword inside `for ... step n`; before `(` it is a name
|
||||||
|
Kind::Word => is_keyword(prev) && prev.text != "func" && prev.text != "step",
|
||||||
Kind::Punct => !matches!(pt, ")" | "]" | "}"),
|
Kind::Punct => !matches!(pt, ")" | "]" | "}"),
|
||||||
_ => true,
|
_ => true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
"[" => {
|
"[" => {
|
||||||
return match prev.kind {
|
return match prev.kind {
|
||||||
Kind::Word => is_keyword(prev),
|
Kind::Word => is_keyword(prev) && prev.text != "step",
|
||||||
Kind::Punct => !matches!(pt, ")" | "]"),
|
Kind::Punct => !matches!(pt, ")" | "]"),
|
||||||
_ => true,
|
_ => true,
|
||||||
};
|
};
|
||||||
|
|||||||
+44
-2
@@ -26,6 +26,8 @@ pub enum BuiltinShape {
|
|||||||
pub struct HProgram {
|
pub struct HProgram {
|
||||||
/// Structs and enums in dependency order: a type comes after the types it contains by value.
|
/// Structs and enums in dependency order: a type comes after the types it contains by value.
|
||||||
pub types: Vec<HTypeDef>,
|
pub types: Vec<HTypeDef>,
|
||||||
|
/// C headers to include, and what building with them needs.
|
||||||
|
pub c: CInfo,
|
||||||
pub funcs: Vec<HFunc>,
|
pub funcs: Vec<HFunc>,
|
||||||
/// Every interface: its table name and its methods (name, parameters, result).
|
/// Every interface: its table name and its methods (name, parameters, result).
|
||||||
pub ifaces: Vec<(String, Vec<(String, Vec<Ty>, Ty)>)>,
|
pub ifaces: Vec<(String, Vec<(String, Vec<Ty>, Ty)>)>,
|
||||||
@@ -85,6 +87,35 @@ pub enum HArmBody {
|
|||||||
pub struct HStruct {
|
pub struct HStruct {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub fields: Vec<(String, Ty)>,
|
pub fields: Vec<(String, Ty)>,
|
||||||
|
/// For a struct from a C header: how C spells it (`Color`, `struct timeval`). Dino keeps its
|
||||||
|
/// own copy of the fields and converts at calls.
|
||||||
|
pub c: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What the imported C headers need.
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct CInfo {
|
||||||
|
pub headers: Vec<String>,
|
||||||
|
pub cflags: Vec<String>,
|
||||||
|
pub libs: Vec<String>,
|
||||||
|
pub sources: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How one argument of a C function is passed.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum CArg {
|
||||||
|
/// As it is (numbers, pointers) or converted (structs).
|
||||||
|
Value,
|
||||||
|
/// A Dino string as a C string (`const char *`), copied for the call.
|
||||||
|
Str,
|
||||||
|
/// `nil`: a NULL pointer.
|
||||||
|
Null,
|
||||||
|
/// A variable the function writes into: its address (`int *out`).
|
||||||
|
Addr,
|
||||||
|
/// A value the function only reads through a pointer (`const Color *c`).
|
||||||
|
AddrIn,
|
||||||
|
/// The items of a list: a pointer to the first one (`const float *values`, `char *buf`).
|
||||||
|
Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -248,6 +279,10 @@ pub enum HExprKind {
|
|||||||
/// A struct value. Every field appears exactly once; initializers are listed in the
|
/// A struct value. Every field appears exactly once; initializers are listed in the
|
||||||
/// order they were written (so they run in that order), omitted fields last.
|
/// order they were written (so they run in that order), omitted fields last.
|
||||||
StructLit { name: String, fields: Vec<(String, HExpr)> },
|
StructLit { name: String, fields: Vec<(String, HExpr)> },
|
||||||
|
/// A call of a function from a C header.
|
||||||
|
CCall { name: String, args: Vec<HExpr>, modes: Vec<CArg> },
|
||||||
|
/// A C expression with no inputs, like a macro constant (`RED`).
|
||||||
|
CRaw(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The local variable at the start of a place like `a.b.c`, if it is one.
|
/// The local variable at the start of a place like `a.b.c`, if it is one.
|
||||||
@@ -272,6 +307,7 @@ pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn F
|
|||||||
| HExprKind::Bool(_)
|
| HExprKind::Bool(_)
|
||||||
| HExprKind::Str(_)
|
| HExprKind::Str(_)
|
||||||
| HExprKind::Local(..)
|
| HExprKind::Local(..)
|
||||||
|
| HExprKind::CRaw(_)
|
||||||
| HExprKind::Nil => {}
|
| HExprKind::Nil => {}
|
||||||
HExprKind::Wrap(x)
|
HExprKind::Wrap(x)
|
||||||
| HExprKind::MakeError(x)
|
| HExprKind::MakeError(x)
|
||||||
@@ -289,7 +325,7 @@ pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn F
|
|||||||
walk_expr(lhs, on_stmt, on_expr);
|
walk_expr(lhs, on_stmt, on_expr);
|
||||||
walk_expr(rhs, on_stmt, on_expr);
|
walk_expr(rhs, on_stmt, on_expr);
|
||||||
}
|
}
|
||||||
HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => {
|
HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } | HExprKind::CCall { args, .. } => {
|
||||||
args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr))
|
args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr))
|
||||||
}
|
}
|
||||||
HExprKind::CallValue { callee, args } => {
|
HExprKind::CallValue { callee, args } => {
|
||||||
@@ -430,6 +466,11 @@ pub fn expr_mutates_root(e: &HExpr, id: u32, is_mut_method: &dyn Fn(&str, &str)
|
|||||||
place_root(list).map(|(i, _)| i) == Some(id)
|
place_root(list).map(|(i, _)| i) == Some(id)
|
||||||
}
|
}
|
||||||
HExprKind::MapCall { map, op: MapOp::Remove, .. } => place_root(map).map(|(i, _)| i) == Some(id),
|
HExprKind::MapCall { map, op: MapOp::Remove, .. } => place_root(map).map(|(i, _)| i) == Some(id),
|
||||||
|
// a C function writing into a variable or a list
|
||||||
|
HExprKind::CCall { args, modes, .. } => args
|
||||||
|
.iter()
|
||||||
|
.zip(modes)
|
||||||
|
.any(|(a, m)| matches!(m, CArg::Addr | CArg::Buffer) && place_root(a).map(|(i, _)| i) == Some(id)),
|
||||||
// calling a function value may change what it captured
|
// calling a function value may change what it captured
|
||||||
HExprKind::CallValue { callee, .. } => place_root(callee).map(|(i, _)| i) == Some(id),
|
HExprKind::CallValue { callee, .. } => place_root(callee).map(|(i, _)| i) == Some(id),
|
||||||
// a method called through an interface may change the value inside
|
// a method called through an interface may change the value inside
|
||||||
@@ -449,6 +490,7 @@ pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) {
|
|||||||
| HExprKind::Bool(_)
|
| HExprKind::Bool(_)
|
||||||
| HExprKind::Str(_)
|
| HExprKind::Str(_)
|
||||||
| HExprKind::Local(..)
|
| HExprKind::Local(..)
|
||||||
|
| HExprKind::CRaw(_)
|
||||||
| HExprKind::Nil => {}
|
| HExprKind::Nil => {}
|
||||||
HExprKind::Wrap(x)
|
HExprKind::Wrap(x)
|
||||||
| HExprKind::MakeError(x)
|
| HExprKind::MakeError(x)
|
||||||
@@ -470,7 +512,7 @@ pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) {
|
|||||||
f(lhs);
|
f(lhs);
|
||||||
f(rhs);
|
f(rhs);
|
||||||
}
|
}
|
||||||
HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => {
|
HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } | HExprKind::CCall { args, .. } => {
|
||||||
args.iter_mut().for_each(|a| f(a))
|
args.iter_mut().for_each(|a| f(a))
|
||||||
}
|
}
|
||||||
HExprKind::CallValue { callee, args } => {
|
HExprKind::CallValue { callee, args } => {
|
||||||
|
|||||||
+62
-4
@@ -34,10 +34,14 @@ pub struct Sym {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Import {
|
pub struct Import {
|
||||||
/// The name the module is used by (`utils`, or `fs` for `tools.fs`).
|
/// The name the module is used by (`utils`, or `fs` for `tools.fs`, or `zlib` for "zlib.h").
|
||||||
pub name: String,
|
pub name: String,
|
||||||
/// The path as written (`utils`, `tools.fs`); empty for `import c "x.h"`.
|
/// The path as written (`utils`, `tools.fs`); empty for a C header.
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
/// For a C header: the header (`zlib.h`), the names picked in `{ }`, and `link "..."`.
|
||||||
|
pub header: Option<String>,
|
||||||
|
pub picked: Vec<String>,
|
||||||
|
pub link: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
@@ -84,6 +88,49 @@ impl Index {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `import "zlib.h"`, `import z "zlib.h" { a, b } link "z"`: the import and the token after it.
|
||||||
|
fn c_import(&self, toks: &[crate::lexer::Token], j: usize) -> Option<(Import, usize)> {
|
||||||
|
let text = |k: usize| match toks.get(k).map(|t| &t.tok) {
|
||||||
|
Some(Tok::Str(parts)) => match parts.as_slice() {
|
||||||
|
[crate::lexer::StrPart::Text(t)] => Some(t.clone()),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let mut k = j + 1;
|
||||||
|
let alias = match toks.get(k).map(|t| &t.tok) {
|
||||||
|
Some(Tok::Ident(a)) if text(k + 1).is_some() => {
|
||||||
|
k += 1;
|
||||||
|
Some(a.clone())
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let header = text(k)?;
|
||||||
|
k += 1;
|
||||||
|
let mut picked = Vec::new();
|
||||||
|
if toks.get(k).map(|t| &t.tok) == Some(&Tok::LBrace) {
|
||||||
|
k += 1;
|
||||||
|
while let Some(t) = toks.get(k) {
|
||||||
|
match &t.tok {
|
||||||
|
Tok::RBrace => {
|
||||||
|
k += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Tok::Ident(n) => picked.push(n.clone()),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
k += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut link = None;
|
||||||
|
if matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Ident(w)) if w == "link") {
|
||||||
|
link = text(k + 1);
|
||||||
|
k += 2;
|
||||||
|
}
|
||||||
|
let name = alias.unwrap_or_else(|| crate::cheader::prefix_of(&header));
|
||||||
|
Some((Import { name, path: String::new(), header: Some(header), picked, link }, k))
|
||||||
|
}
|
||||||
|
|
||||||
fn scan_top(&mut self) {
|
fn scan_top(&mut self) {
|
||||||
let toks = self.toks.clone();
|
let toks = self.toks.clone();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
@@ -96,8 +143,13 @@ impl Index {
|
|||||||
}
|
}
|
||||||
let Some(head) = toks.get(j) else { break };
|
let Some(head) = toks.get(j) else { break };
|
||||||
match head.tok {
|
match head.tok {
|
||||||
|
Tok::Import if self.c_import(&toks, j).is_some() => {
|
||||||
|
let (imp, next) = self.c_import(&toks, j).unwrap();
|
||||||
|
self.imports.push(imp);
|
||||||
|
i = next.max(i + 1);
|
||||||
|
}
|
||||||
Tok::Import => {
|
Tok::Import => {
|
||||||
// import a.b.c | import c "header.h"
|
// import a.b.c
|
||||||
let mut k = j + 1;
|
let mut k = j + 1;
|
||||||
let mut parts: Vec<String> = Vec::new();
|
let mut parts: Vec<String> = Vec::new();
|
||||||
while let Some(Tok::Ident(s)) = toks.get(k).map(|t| &t.tok) {
|
while let Some(Tok::Ident(s)) = toks.get(k).map(|t| &t.tok) {
|
||||||
@@ -111,7 +163,13 @@ impl Index {
|
|||||||
}
|
}
|
||||||
if let Some(last) = parts.last() {
|
if let Some(last) = parts.last() {
|
||||||
let aliased = matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Str(_)));
|
let aliased = matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Str(_)));
|
||||||
self.imports.push(Import { name: last.clone(), path: if aliased { String::new() } else { parts.join(".") } });
|
self.imports.push(Import {
|
||||||
|
name: last.clone(),
|
||||||
|
path: if aliased { String::new() } else { parts.join(".") },
|
||||||
|
header: None,
|
||||||
|
picked: Vec::new(),
|
||||||
|
link: None,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
i = k.max(i + 1);
|
i = k.max(i + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod check;
|
pub mod check;
|
||||||
|
pub mod cheader;
|
||||||
pub mod codegen_c;
|
pub mod codegen_c;
|
||||||
pub mod driver;
|
pub mod driver;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ fn load_imports(
|
|||||||
) -> PResult<()> {
|
) -> PResult<()> {
|
||||||
for item in &p.items {
|
for item in &p.items {
|
||||||
let Item::Import(i) = item else { continue };
|
let Item::Import(i) = item else { continue };
|
||||||
if i.alias.is_some() || stdlib::MODULES.contains(&i.path.as_str()) {
|
if i.c || stdlib::MODULES.contains(&i.path.as_str()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let key = i.path.clone();
|
let key = i.path.clone();
|
||||||
|
|||||||
+73
-3
@@ -169,7 +169,8 @@ impl Server {
|
|||||||
let dir: Option<PathBuf> = uri_to_path(uri).and_then(|p| p.parent().map(PathBuf::from));
|
let dir: Option<PathBuf> = uri_to_path(uri).and_then(|p| p.parent().map(PathBuf::from));
|
||||||
let docs = &self.docs;
|
let docs = &self.docs;
|
||||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||||
driver::diagnose(text, |key| {
|
let here = dir.clone().unwrap_or_default();
|
||||||
|
driver::diagnose(text, &here, |key| {
|
||||||
let file = dir.as_ref()?.join(format!("{}.dino", key.replace('.', "/")));
|
let file = dir.as_ref()?.join(format!("{}.dino", key.replace('.', "/")));
|
||||||
// an import that is open in the editor counts with its unsaved changes
|
// an import that is open in the editor counts with its unsaved changes
|
||||||
for (u, t) in docs {
|
for (u, t) in docs {
|
||||||
@@ -355,6 +356,39 @@ fn completion_kind(k: Kind) -> i64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
|
/// The C header behind an import, read the same way the compiler reads it.
|
||||||
|
fn c_lib(&self, uri: &str, imp: &crate::index::Import) -> Option<crate::cheader::Lib> {
|
||||||
|
let header = imp.header.as_ref()?;
|
||||||
|
let dir = uri_to_path(uri)?.parent()?.to_path_buf();
|
||||||
|
crate::cheader::load(header, &dir, imp.link.as_deref()).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If the word starting at character `a` of `line` names something from a C header
|
||||||
|
/// (`zlib.compress`, or a name picked in `import "zlib.h" { compress }`): the header and the name.
|
||||||
|
fn c_name_at(&self, uri: &str, line: &str, a: usize, word: &str) -> Option<(crate::cheader::Lib, String)> {
|
||||||
|
let ix = self.indexes.get(uri)?;
|
||||||
|
let chars: Vec<char> = line.chars().collect();
|
||||||
|
if a > 0 && chars[a - 1] == '.' {
|
||||||
|
let q = word_before(line, a - 1)?;
|
||||||
|
let imp = ix.imports.iter().find(|i| i.name == q && i.header.is_some() && i.picked.is_empty())?;
|
||||||
|
return Some((self.c_lib(uri, imp)?, word.to_string()));
|
||||||
|
}
|
||||||
|
let imp = ix.imports.iter().find(|i| i.picked.iter().any(|p| p == word))?;
|
||||||
|
Some((self.c_lib(uri, imp)?, word.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn c_completion_kind(lib: &crate::cheader::Lib, name: &str) -> i64 {
|
||||||
|
if lib.func(name).is_some() {
|
||||||
|
3
|
||||||
|
} else if lib.strukt(name).is_some() {
|
||||||
|
22
|
||||||
|
} else if lib.konst(name).is_some() {
|
||||||
|
21
|
||||||
|
} else {
|
||||||
|
7
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn set_text(&mut self, uri: &str, text: &str) {
|
fn set_text(&mut self, uri: &str, text: &str) {
|
||||||
self.docs.insert(uri.to_string(), text.to_string());
|
self.docs.insert(uri.to_string(), text.to_string());
|
||||||
if let Some(ix) = Index::new(text) {
|
if let Some(ix) = Index::new(text) {
|
||||||
@@ -540,6 +574,13 @@ impl Server {
|
|||||||
fn hover(&self, uri: &str, line: usize, ch: usize) -> Json {
|
fn hover(&self, uri: &str, line: usize, ch: usize) -> Json {
|
||||||
let line_text = self.line_of(uri, line).unwrap_or("");
|
let line_text = self.line_of(uri, line).unwrap_or("");
|
||||||
let col = char_of_units(line_text, ch);
|
let col = char_of_units(line_text, ch);
|
||||||
|
if let Some((word, a, _)) = word_at(line_text, col) {
|
||||||
|
if let Some((lib, name)) = self.c_name_at(uri, line_text, a, &word) {
|
||||||
|
if let Some(text) = lib.describe(&name) {
|
||||||
|
return hover_text(format!("{text}\n\nfrom the C header {}", lib.header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// a name from a built-in module: `math.sqrt`
|
// a name from a built-in module: `math.sqrt`
|
||||||
if let Some((word, a, _)) = word_at(line_text, col) {
|
if let Some((word, a, _)) = word_at(line_text, col) {
|
||||||
let chars: Vec<char> = line_text.chars().collect();
|
let chars: Vec<char> = line_text.chars().collect();
|
||||||
@@ -588,6 +629,16 @@ impl Server {
|
|||||||
};
|
};
|
||||||
if a > 0 && chars[a - 1] == '.' {
|
if a > 0 && chars[a - 1] == '.' {
|
||||||
let left = word_before(line_text, a - 1);
|
let left = word_before(line_text, a - 1);
|
||||||
|
// a C header: everything Dino can use from it
|
||||||
|
if let Some(imp) = left.as_deref().and_then(|q| ix.imports.iter().find(|i| i.name == q && i.header.is_some())) {
|
||||||
|
if let Some(lib) = self.c_lib(uri, imp) {
|
||||||
|
for n in lib.names() {
|
||||||
|
let detail = lib.func(n).map(|f| lib.signature(f)).unwrap_or_default();
|
||||||
|
push(&mut items, n, Self::c_completion_kind(&lib, n), &detail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Json::Arr(items);
|
||||||
|
}
|
||||||
if let Some(q) = left.as_deref() {
|
if let Some(q) = left.as_deref() {
|
||||||
// a built-in module
|
// a built-in module
|
||||||
if stdlib::MODULES.contains(&q) && ix.module(q).is_some() {
|
if stdlib::MODULES.contains(&q) && ix.module(q).is_some() {
|
||||||
@@ -651,7 +702,18 @@ impl Server {
|
|||||||
push(&mut items, &s.name, completion_kind(s.kind), "");
|
push(&mut items, &s.name, completion_kind(s.kind), "");
|
||||||
}
|
}
|
||||||
for imp in &ix.imports {
|
for imp in &ix.imports {
|
||||||
push(&mut items, &imp.name, 9, "module");
|
if imp.picked.is_empty() {
|
||||||
|
push(&mut items, &imp.name, 9, if imp.header.is_some() { "C header" } else { "module" });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let lib = self.c_lib(uri, imp);
|
||||||
|
for n in &imp.picked {
|
||||||
|
let (kind, detail) = match &lib {
|
||||||
|
Some(l) => (Self::c_completion_kind(l, n), l.func(n).map(|f| l.signature(f)).unwrap_or_default()),
|
||||||
|
None => (3, String::new()),
|
||||||
|
};
|
||||||
|
push(&mut items, n, kind, &detail);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for k in [
|
for k in [
|
||||||
"func", "var", "const", "import", "struct", "enum", "interface", "pub", "if", "else", "for", "in", "step", "match",
|
"func", "var", "const", "import", "struct", "enum", "interface", "pub", "if", "else", "for", "in", "step", "match",
|
||||||
@@ -706,13 +768,21 @@ impl Server {
|
|||||||
let Some(name) = word_before(open_line, oc) else { return Json::Null };
|
let Some(name) = word_before(open_line, oc) else { return Json::Null };
|
||||||
let name_start = oc - name.chars().count();
|
let name_start = oc - name.chars().count();
|
||||||
let units_before_name: usize = utf16_len(&open_line.chars().take(name_start).collect::<String>());
|
let units_before_name: usize = utf16_len(&open_line.chars().take(name_start).collect::<String>());
|
||||||
|
let label = match self.c_name_at(uri, open_line, name_start, &name) {
|
||||||
|
Some((lib, n)) => match lib.func(&n) {
|
||||||
|
Some(f) => lib.signature(f),
|
||||||
|
None => return Json::Null,
|
||||||
|
},
|
||||||
|
None => {
|
||||||
let Some(f) = self.resolve(uri, ol, units_before_name) else { return Json::Null };
|
let Some(f) = self.resolve(uri, ol, units_before_name) else { return Json::Null };
|
||||||
if !matches!(f.sym.kind, Kind::Func | Kind::Method) {
|
if !matches!(f.sym.kind, Kind::Func | Kind::Method) {
|
||||||
return Json::Null;
|
return Json::Null;
|
||||||
}
|
}
|
||||||
let decl = Self::declaration_text(&f);
|
let decl = Self::declaration_text(&f);
|
||||||
// `pub func add(a: int, b: int): int` -> `add(a: int, b: int): int`
|
// `pub func add(a: int, b: int): int` -> `add(a: int, b: int): int`
|
||||||
let label = decl.find("func ").map_or(decl.clone(), |i| decl[i + 5..].trim().to_string());
|
decl.find("func ").map_or(decl.clone(), |i| decl[i + 5..].trim().to_string())
|
||||||
|
}
|
||||||
|
};
|
||||||
let (Some(p0), Some(p1)) = (label.find('('), label.rfind(')')) else { return Json::Null };
|
let (Some(p0), Some(p1)) = (label.find('('), label.rfind(')')) else { return Json::Null };
|
||||||
// the parameters: split the text between the parentheses at top-level commas
|
// the parameters: split the text between the parentheses at top-level commas
|
||||||
let mut params: Vec<Json> = Vec::new();
|
let mut params: Vec<Json> = Vec::new();
|
||||||
|
|||||||
+57
@@ -12,6 +12,7 @@ fn usage() -> ! {
|
|||||||
eprintln!(" dino check <file.dino> check for errors without building");
|
eprintln!(" dino check <file.dino> check for errors without building");
|
||||||
eprintln!(" dino fmt [--check] <file|dir>.. format code in place (--check only reports; `-` = stdin to stdout)");
|
eprintln!(" dino fmt [--check] <file|dir>.. format code in place (--check only reports; `-` = stdin to stdout)");
|
||||||
eprintln!(" dino lsp run the language server (for editors)");
|
eprintln!(" dino lsp run the language server (for editors)");
|
||||||
|
eprintln!(" dino header <file.h> [name] show what Dino can use from a C header");
|
||||||
eprintln!(" dino parse <file.dino> print the syntax tree");
|
eprintln!(" dino parse <file.dino> print the syntax tree");
|
||||||
eprintln!(" dino tokens <file.dino> print the tokens");
|
eprintln!(" dino tokens <file.dino> print the tokens");
|
||||||
process::exit(2);
|
process::exit(2);
|
||||||
@@ -35,6 +36,59 @@ fn dino_files(path: &std::path::Path, out: &mut Vec<PathBuf>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `dino header zlib.h [name] [--link z]`: what Dino code can use from a C header.
|
||||||
|
fn run_header(args: &[String]) -> ! {
|
||||||
|
let mut header: Option<&str> = None;
|
||||||
|
let mut filter: Option<&str> = None;
|
||||||
|
let mut link: Option<&str> = None;
|
||||||
|
let mut it = args.iter();
|
||||||
|
while let Some(a) = it.next() {
|
||||||
|
match a.as_str() {
|
||||||
|
"--link" => link = it.next().map(String::as_str),
|
||||||
|
_ if header.is_none() => header = Some(a),
|
||||||
|
_ => filter = Some(a),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let Some(header) = header else { usage() };
|
||||||
|
let dir = env::current_dir().unwrap_or_default();
|
||||||
|
let lib = dino::cheader::load(header, &dir, link).unwrap_or_else(|e| die(&format!("error: {e}")));
|
||||||
|
let shown = |n: &str| filter.map_or(true, |f| n.to_lowercase().contains(&f.to_lowercase()));
|
||||||
|
let mut text = format!("import \"{header}\" // use the names below as {}.name\n", lib.prefix);
|
||||||
|
if !lib.libs.is_empty() || !lib.cflags.is_empty() {
|
||||||
|
text.push_str(&format!("// builds with: {}\n", lib.cflags.iter().chain(&lib.libs).cloned().collect::<Vec<_>>().join(" ")));
|
||||||
|
}
|
||||||
|
let mut section = |title: &str, lines: Vec<String>| {
|
||||||
|
if !lines.is_empty() {
|
||||||
|
text.push_str(&format!("\n// {title}\n"));
|
||||||
|
for l in lines {
|
||||||
|
text.push_str(&l);
|
||||||
|
text.push('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
section("types", lib.structs.iter().filter(|s| shown(&s.name)).map(|s| lib.describe(&s.name).unwrap()).collect());
|
||||||
|
section(
|
||||||
|
"pointer types (pass them around, compare them with nil)",
|
||||||
|
lib.handles.iter().filter(|h| shown(h)).map(|h| format!("{}.{h}", lib.prefix)).collect(),
|
||||||
|
);
|
||||||
|
section(
|
||||||
|
"functions",
|
||||||
|
lib.funcs.iter().filter(|f| f.problem.is_none() && shown(&f.name)).map(|f| format!("func {}", lib.signature(f))).collect(),
|
||||||
|
);
|
||||||
|
section("constants", lib.consts.iter().filter(|c| shown(&c.name)).map(|c| lib.describe(&c.name).unwrap()).collect());
|
||||||
|
section(
|
||||||
|
"functions Dino can't call yet",
|
||||||
|
lib.funcs
|
||||||
|
.iter()
|
||||||
|
.filter(|f| shown(&f.name))
|
||||||
|
.filter_map(|f| f.problem.as_ref().map(|p| format!("{}: it {p}", f.name)))
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
|
// a closed pipe (`| head`) is not an error
|
||||||
|
let _ = std::io::Write::write_all(&mut std::io::stdout(), text.as_bytes());
|
||||||
|
process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
/// `dino fmt [--check] files-or-folders...`
|
/// `dino fmt [--check] files-or-folders...`
|
||||||
fn run_fmt(args: &[String]) -> ! {
|
fn run_fmt(args: &[String]) -> ! {
|
||||||
// `dino fmt -`: text in on stdin, tidy text out on stdout (for editors)
|
// `dino fmt -`: text in on stdin, tidy text out on stdout (for editors)
|
||||||
@@ -107,6 +161,9 @@ fn main() {
|
|||||||
if args.first().map(String::as_str) == Some("fmt") {
|
if args.first().map(String::as_str) == Some("fmt") {
|
||||||
run_fmt(&args[1..]);
|
run_fmt(&args[1..]);
|
||||||
}
|
}
|
||||||
|
if args.first().map(String::as_str) == Some("header") {
|
||||||
|
run_header(&args[1..]);
|
||||||
|
}
|
||||||
if args.len() < 2 {
|
if args.len() < 2 {
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-9
@@ -164,21 +164,65 @@ impl Parser {
|
|||||||
|
|
||||||
fn import(&mut self) -> PResult<Item> {
|
fn import(&mut self) -> PResult<Item> {
|
||||||
let span = self.bump().span;
|
let span = self.bump().span;
|
||||||
let first = self.ident()?;
|
let plain_text = |p: &mut Self| -> PResult<String> {
|
||||||
let item = if let Tok::Str(parts) = self.peek().clone() {
|
let t = p.bump();
|
||||||
self.bump();
|
match &t.tok {
|
||||||
let path = match parts.as_slice() {
|
Tok::Str(parts) => match parts.as_slice() {
|
||||||
[LexPart::Text(t)] => t.clone(),
|
[LexPart::Text(t)] => Ok(t.clone()),
|
||||||
_ => return Err(Diag::new("an import path must be plain text", span)),
|
[] => Ok(String::new()),
|
||||||
|
_ => Err(Diag::new("this must be plain text, without `{...}` inside", t.span)),
|
||||||
|
},
|
||||||
|
_ => Err(Diag::new("expected text in quotes here", t.span)),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Import { alias: Some(first), path, span }
|
// `import "zlib.h"` or `import z "zlib.h"`
|
||||||
|
let alias = if matches!(self.peek(), Tok::Ident(_)) && matches!(self.peek_n(1), Tok::Str(_)) {
|
||||||
|
Some(self.ident()?)
|
||||||
} else {
|
} else {
|
||||||
let mut path = first;
|
None
|
||||||
|
};
|
||||||
|
let item = if matches!(self.peek(), Tok::Str(_)) {
|
||||||
|
let path = plain_text(self)?;
|
||||||
|
if path.is_empty() {
|
||||||
|
return Err(Diag::new("the name of a C header can't be empty", span));
|
||||||
|
}
|
||||||
|
let mut names = None;
|
||||||
|
if self.eat(&Tok::LBrace) {
|
||||||
|
let mut list = Vec::new();
|
||||||
|
loop {
|
||||||
|
self.skip_newlines();
|
||||||
|
if self.eat(&Tok::RBrace) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let s = self.span();
|
||||||
|
list.push((self.ident()?, s));
|
||||||
|
self.skip_newlines();
|
||||||
|
if !self.eat(&Tok::Comma) {
|
||||||
|
self.skip_newlines();
|
||||||
|
if !self.eat(&Tok::RBrace) {
|
||||||
|
return self.unexpected("`,` or `}` after a name");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if list.is_empty() {
|
||||||
|
return Err(Diag::new("pick at least one name in `{ }`, or leave the braces out to get everything", span));
|
||||||
|
}
|
||||||
|
names = Some(list);
|
||||||
|
}
|
||||||
|
let mut link = None;
|
||||||
|
if matches!(self.peek(), Tok::Ident(w) if w == "link") {
|
||||||
|
self.bump();
|
||||||
|
link = Some(plain_text(self)?);
|
||||||
|
}
|
||||||
|
Import { alias, path, span, c: true, names, link }
|
||||||
|
} else {
|
||||||
|
let mut path = self.ident()?;
|
||||||
while self.eat(&Tok::Dot) {
|
while self.eat(&Tok::Dot) {
|
||||||
path.push('.');
|
path.push('.');
|
||||||
path.push_str(&self.ident()?);
|
path.push_str(&self.ident()?);
|
||||||
}
|
}
|
||||||
Import { alias: None, path, span }
|
Import { alias: None, path, span, c: false, names: None, link: None }
|
||||||
};
|
};
|
||||||
self.terminator()?;
|
self.terminator()?;
|
||||||
Ok(Item::Import(item))
|
Ok(Item::Import(item))
|
||||||
|
|||||||
@@ -608,3 +608,16 @@ static void dino_time_sleep(double secs) {
|
|||||||
ts.tv_nsec = (long)((secs - (double)ts.tv_sec) * 1e9);
|
ts.tv_nsec = (long)((secs - (double)ts.tv_sec) * 1e9);
|
||||||
nanosleep(&ts, NULL);
|
nanosleep(&ts, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ---- C libraries ---- */
|
||||||
|
|
||||||
|
static dstr dino_from_cstr(const char *p) {
|
||||||
|
return p ? dino_new_str(p, (int64_t)strlen(p)) : dino_lit("", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static dstr dino_ptr_str(const void *p) {
|
||||||
|
if (!p) return dino_lit("nil", 3);
|
||||||
|
char b[32];
|
||||||
|
int n = snprintf(b, sizeof b, "%p", p);
|
||||||
|
return dino_new_str(b, n);
|
||||||
|
}
|
||||||
@@ -85,6 +85,9 @@ pub enum Ty {
|
|||||||
Iface(&'static str),
|
Iface(&'static str),
|
||||||
/// A function value: `func(int, string): bool`.
|
/// A function value: `func(int, string): bool`.
|
||||||
Func(&'static FuncTy),
|
Func(&'static FuncTy),
|
||||||
|
/// A pointer from a C library that Dino only passes around: `SDL.SDL_Window`, `zlib.gzFile`.
|
||||||
|
/// `"pointer"` (C's `void *`) goes with every other pointer type.
|
||||||
|
CPtr(&'static str),
|
||||||
/// The "type" of a call that returns nothing.
|
/// The "type" of a call that returns nothing.
|
||||||
Unit,
|
Unit,
|
||||||
}
|
}
|
||||||
@@ -201,6 +204,7 @@ impl fmt::Display for Ty {
|
|||||||
}
|
}
|
||||||
f.write_str(")")
|
f.write_str(")")
|
||||||
}
|
}
|
||||||
|
Ty::CPtr(name) => f.write_str(name),
|
||||||
Ty::Unit => f.write_str("nothing"),
|
Ty::Unit => f.write_str("nothing"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+300
@@ -0,0 +1,300 @@
|
|||||||
|
//! Using C (and C++) libraries: `import "header.h"`.
|
||||||
|
//! Programs run under the address sanitizer, like the other tests.
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use dino::driver;
|
||||||
|
|
||||||
|
/// A fresh folder with the given files in it.
|
||||||
|
fn folder(files: &[(&str, &str)]) -> PathBuf {
|
||||||
|
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||||
|
let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
let dir = std::env::temp_dir().join(format!("dino-clib-{}-{n}", std::process::id()));
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
for (name, text) in files {
|
||||||
|
std::fs::write(dir.join(name), text).unwrap();
|
||||||
|
}
|
||||||
|
dir
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compile `main.dino` in a folder with these files and run it; returns what it printed.
|
||||||
|
fn run(files: &[(&str, &str)]) -> String {
|
||||||
|
let dir = folder(files);
|
||||||
|
let c = driver::compile_file(dir.join("main.dino").to_str().unwrap()).unwrap_or_else(|e| panic!("{e}"));
|
||||||
|
let exe = dir.join("prog");
|
||||||
|
let sanitized = driver::build_exe_with(&c, &exe, &["-std=c11", "-fwrapv", "-w", "-fsanitize=address", "-g"]);
|
||||||
|
if let Err(e) = sanitized {
|
||||||
|
assert!(e.contains("asan"), "{e}");
|
||||||
|
driver::build_exe(&c, &exe).unwrap();
|
||||||
|
}
|
||||||
|
let o = Command::new(&exe).env("ASAN_OPTIONS", "detect_leaks=1").output().unwrap();
|
||||||
|
let stderr = String::from_utf8_lossy(&o.stderr).to_string();
|
||||||
|
assert!(!stderr.contains("Sanitizer"), "memory error:\n{stderr}");
|
||||||
|
assert!(o.status.success(), "exit {:?}\n{stderr}", o.status.code());
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
String::from_utf8_lossy(&o.stdout).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The error message for a program that should not compile.
|
||||||
|
fn error(files: &[(&str, &str)]) -> String {
|
||||||
|
let dir = folder(files);
|
||||||
|
let r = driver::compile_file(dir.join("main.dino").to_str().unwrap());
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
match r {
|
||||||
|
Ok(_) => panic!("expected an error"),
|
||||||
|
Err(e) => e,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn have_pkg(name: &str) -> bool {
|
||||||
|
Command::new("pkg-config").args(["--exists", name]).status().map_or(false, |s| s.success())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_c_library() {
|
||||||
|
let out = run(&[(
|
||||||
|
"main.dino",
|
||||||
|
"import \"math.h\" { cbrt, hypot }\nimport \"string.h\"\nimport \"stdlib.h\"\n\n\
|
||||||
|
func main() {\n print(cbrt(27.0), hypot(3.0, 4.0))\n print(string.strlen(\"héllo\"), stdlib.abs(-4))\n print(stdlib.atoi(\"42abc\") + 1)\n}\n",
|
||||||
|
)]);
|
||||||
|
assert_eq!(out, "3.0 5.0\n6 4\n43\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHAPES_H: &str = r#"
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
typedef struct { double w, h; } Size;
|
||||||
|
typedef struct { Size size; int id; } Box;
|
||||||
|
typedef struct Counter Counter;
|
||||||
|
enum Mode { MODE_A, MODE_B = 10, MODE_C };
|
||||||
|
#define SHAPES_VERSION "1.2"
|
||||||
|
#define MAX_SIDES (4 * 3)
|
||||||
|
#define BIG ((unsigned)1 << 20)
|
||||||
|
#define UNIT_BOX (Box){ { 1.0, 1.0 }, 7 }
|
||||||
|
double area(Size s);
|
||||||
|
void grow(Size *s, double by);
|
||||||
|
Box make_box(int id);
|
||||||
|
int sum(const int *values, int n);
|
||||||
|
void fill(char *buf, int n);
|
||||||
|
int count_chars(const char *text, char c);
|
||||||
|
Counter *counter_new(const char *name);
|
||||||
|
void counter_add(Counter *c, int n);
|
||||||
|
int counter_total(const Counter *c);
|
||||||
|
const char *counter_name(const Counter *c);
|
||||||
|
void counter_free(Counter *c);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
"#;
|
||||||
|
|
||||||
|
const SHAPES_C: &str = r#"
|
||||||
|
#include "shapes.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
struct Counter { char name[32]; int total; };
|
||||||
|
double area(Size s) { return s.w * s.h; }
|
||||||
|
void grow(Size *s, double by) { s->w *= by; s->h *= by; }
|
||||||
|
Box make_box(int id) { Box b = { { 2.0, 3.0 }, id }; return b; }
|
||||||
|
int sum(const int *values, int n) { int t = 0; for (int i = 0; i < n; i++) t += values[i]; return t; }
|
||||||
|
void fill(char *buf, int n) { for (int i = 0; i < n; i++) buf[i] = 'a' + i; }
|
||||||
|
int count_chars(const char *text, char c) { int k = 0; for (; *text; text++) k += *text == c; return k; }
|
||||||
|
Counter *counter_new(const char *name) { Counter *c = calloc(1, sizeof *c); strncpy(c->name, name, 31); return c; }
|
||||||
|
void counter_add(Counter *c, int n) { c->total += n; }
|
||||||
|
int counter_total(const Counter *c) { return c->total; }
|
||||||
|
const char *counter_name(const Counter *c) { return c->name; }
|
||||||
|
void counter_free(Counter *c) { free(c); }
|
||||||
|
"#;
|
||||||
|
|
||||||
|
const SHAPES_MAIN: &str = r#"import "shapes.h"
|
||||||
|
|
||||||
|
struct Holder {
|
||||||
|
size: shapes.Size
|
||||||
|
func double_it() {
|
||||||
|
shapes.grow(size, 2.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s = shapes.Size{2.0, 3.0}
|
||||||
|
print(shapes.area(s), shapes.SHAPES_VERSION, shapes.MAX_SIDES, shapes.BIG)
|
||||||
|
shapes.grow(s, 10.0)
|
||||||
|
print(s)
|
||||||
|
var h = Holder{s}
|
||||||
|
h.double_it()
|
||||||
|
print(h.size.w)
|
||||||
|
print(shapes.make_box(5), shapes.UNIT_BOX.id)
|
||||||
|
print(shapes.MODE_A, shapes.MODE_B, shapes.MODE_C)
|
||||||
|
var nums: []i32 = [1, 2, 3, 4]
|
||||||
|
print(shapes.sum(nums, i32(nums.len())))
|
||||||
|
var buf: []u8 = [0, 0, 0]
|
||||||
|
shapes.fill(buf, 3)
|
||||||
|
print(buf)
|
||||||
|
print(shapes.count_chars("banana", 'a'))
|
||||||
|
var c = shapes.counter_new("apples")
|
||||||
|
for i in 1..5 {
|
||||||
|
shapes.counter_add(c, i32(i))
|
||||||
|
}
|
||||||
|
print(shapes.counter_name(c), shapes.counter_total(c), c != nil)
|
||||||
|
shapes.counter_free(c)
|
||||||
|
var none: shapes.Counter
|
||||||
|
print(none == nil)
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
const SHAPES_OUT: &str = "6.0 1.2 12 1048576\nshapes.Size{w: 20.0, h: 30.0}\n40.0\n\
|
||||||
|
shapes.Box{size: shapes.Size{w: 2.0, h: 3.0}, id: 5} 7\n0 10 11\n10\n[97, 98, 99]\n3\napples 10 true\ntrue\n";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn your_own_c_file() {
|
||||||
|
let out = run(&[("shapes.h", SHAPES_H), ("shapes.c", SHAPES_C), ("main.dino", SHAPES_MAIN)]);
|
||||||
|
assert_eq!(out, SHAPES_OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn your_own_cpp_file() {
|
||||||
|
if Command::new("c++").arg("--version").output().is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let cpp = r#"
|
||||||
|
#include "shapes.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <numeric>
|
||||||
|
struct Counter { std::string name; std::vector<int> items; };
|
||||||
|
extern "C" {
|
||||||
|
double area(Size s) { return s.w * s.h; }
|
||||||
|
void grow(Size *s, double by) { s->w *= by; s->h *= by; }
|
||||||
|
Box make_box(int id) { return Box{ { 2.0, 3.0 }, id }; }
|
||||||
|
int sum(const int *values, int n) { return std::accumulate(values, values + n, 0); }
|
||||||
|
void fill(char *buf, int n) { for (int i = 0; i < n; i++) buf[i] = 'a' + i; }
|
||||||
|
int count_chars(const char *text, char c) { std::string s(text); int k = 0; for (char x : s) k += x == c; return k; }
|
||||||
|
Counter *counter_new(const char *name) { return new Counter{name, {}}; }
|
||||||
|
void counter_add(Counter *c, int n) { c->items.push_back(n); }
|
||||||
|
int counter_total(const Counter *c) { return std::accumulate(c->items.begin(), c->items.end(), 0); }
|
||||||
|
const char *counter_name(const Counter *c) { return c->name.c_str(); }
|
||||||
|
void counter_free(Counter *c) { delete c; }
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
let out = run(&[("shapes.h", SHAPES_H), ("shapes.cpp", cpp), ("main.dino", SHAPES_MAIN)]);
|
||||||
|
assert_eq!(out, SHAPES_OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn picked_names_are_used_bare() {
|
||||||
|
let out = run(&[
|
||||||
|
("shapes.h", SHAPES_H),
|
||||||
|
("shapes.c", SHAPES_C),
|
||||||
|
(
|
||||||
|
"main.dino",
|
||||||
|
"import \"shapes.h\" { Size, area, MAX_SIDES }\n\nfunc main() {\n var s: Size = Size{1.5, 2.0}\n print(area(s), MAX_SIDES)\n}\n",
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
assert_eq!(out, "3.0 12\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zlib_through_pkg_config() {
|
||||||
|
if !have_pkg("zlib") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let out = run(&[(
|
||||||
|
"main.dino",
|
||||||
|
r#"import "zlib.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var text = "hello hello hello hello hello hello hello"
|
||||||
|
var data: []u8
|
||||||
|
for c in text {
|
||||||
|
data.push(u8(int(c)))
|
||||||
|
}
|
||||||
|
var size = zlib.compressBound(u64(data.len()))
|
||||||
|
var packed: []u8
|
||||||
|
for i in 0..int(size) {
|
||||||
|
packed.push(0)
|
||||||
|
}
|
||||||
|
print(zlib.compress(packed, size, data, u64(data.len())) == zlib.Z_OK, size < u64(data.len()))
|
||||||
|
var back: []u8
|
||||||
|
for i in 0..data.len() {
|
||||||
|
back.push(0)
|
||||||
|
}
|
||||||
|
var n = u64(back.len())
|
||||||
|
print(zlib.uncompress(back, n, packed, size) == zlib.Z_OK, string(back) == string(data), n)
|
||||||
|
print(zlib.gzopen("/no/such/dir/x.gz", "rb") == nil)
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
assert_eq!(out, "true true\ntrue true 41\ntrue\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sqlite_fills_in_pointer_variables() {
|
||||||
|
if !have_pkg("sqlite3") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let out = run(&[(
|
||||||
|
"main.dino",
|
||||||
|
r#"import "sqlite3.h"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var db: sqlite3.sqlite3
|
||||||
|
print(sqlite3.sqlite3_open(":memory:", db) == sqlite3.SQLITE_OK, db != nil)
|
||||||
|
var stmt: sqlite3.sqlite3_stmt
|
||||||
|
sqlite3.sqlite3_prepare_v2(db, "select 6 * 7 union select 1 order by 1", -1, stmt, nil)
|
||||||
|
for sqlite3.sqlite3_step(stmt) == sqlite3.SQLITE_ROW {
|
||||||
|
print(sqlite3.sqlite3_column_int(stmt, 0))
|
||||||
|
}
|
||||||
|
sqlite3.sqlite3_finalize(stmt)
|
||||||
|
print(sqlite3.sqlite3_close(db) == sqlite3.SQLITE_OK)
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
assert_eq!(out, "true true\n1\n42\ntrue\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn raylib_structs_without_a_window() {
|
||||||
|
if !have_pkg("raylib") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let out = run(&[(
|
||||||
|
"main.dino",
|
||||||
|
"import \"raylib.h\"\n\nfunc main() {\n var c = raylib.Fade(raylib.RED, 0.5)\n print(c, raylib.ColorToInt(raylib.Color{0, 0, 0, 255}))\n var p = raylib.Vector2{1.0, 1.0}\n print(raylib.CheckCollisionPointRec(p, raylib.Rectangle{0.0, 0.0, 2.0, 2.0}))\n}\n",
|
||||||
|
)]);
|
||||||
|
assert_eq!(out, "raylib.Color{r: 230, g: 41, b: 55, a: 127} 255\ntrue\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn errors_name_the_problem() {
|
||||||
|
let e = error(&[("main.dino", "import \"no_such_header.h\"\n\nfunc main() {}\n")]);
|
||||||
|
assert!(e.contains("cannot find the C header `no_such_header.h`"), "{e}");
|
||||||
|
|
||||||
|
let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\" { aera }\n\nfunc main() {}\n")]);
|
||||||
|
assert!(e.contains("`shapes.h` has no `aera` (did you mean `area`?)"), "{e}");
|
||||||
|
|
||||||
|
let e = error(&[("main.dino", "import \"stdio.h\"\n\nfunc main() {\n stdio.printf(\"hi\")\n}\n")]);
|
||||||
|
assert!(e.contains("`printf` can't be called from Dino: it takes a variable number of arguments"), "{e}");
|
||||||
|
|
||||||
|
let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n shapes.grow(shapes.Size{1.0, 1.0}, 2.0)\n}\n")]);
|
||||||
|
assert!(e.contains("`grow` writes into `s`, so it must be a variable"), "{e}");
|
||||||
|
|
||||||
|
let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n var n = 3\n print(shapes.make_box(n))\n}\n")]);
|
||||||
|
assert!(e.contains("expected `i32`, found `int` (convert it with `i32(...)`)"), "{e}");
|
||||||
|
|
||||||
|
let e = error(&[("shapes.h", SHAPES_H), ("main.dino", "import \"shapes.h\"\n\nfunc main() {\n print(shapes.area())\n}\n")]);
|
||||||
|
assert!(e.contains("`area` takes 1 argument (s: shapes.Size), but 0 were given"), "{e}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_missing_library_says_how_to_link() {
|
||||||
|
let dir = folder(&[(
|
||||||
|
"main.dino",
|
||||||
|
"import \"math.h\" link \"dino_no_such_lib\"\n\nfunc main() {\n print(math.sqrt(4.0))\n}\n",
|
||||||
|
)]);
|
||||||
|
let c = driver::compile_file(dir.join("main.dino").to_str().unwrap()).unwrap();
|
||||||
|
let e = driver::build_exe(&c, &dir.join("prog")).unwrap_err();
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
assert!(e.contains("linking with the C library failed") && e.contains("link \"name\""), "{e}");
|
||||||
|
}
|
||||||
@@ -104,3 +104,12 @@ fn doc_examples_are_stable() {
|
|||||||
}
|
}
|
||||||
assert!(n > 20, "only {n} doc examples were formattable");
|
assert!(n > 20, "only {n} doc examples were formattable");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn step_as_a_name_and_c_imports() {
|
||||||
|
let src = "import \"raylib.h\" { Color,RED }\nimport \"zlib.h\" link \"z\"\n\nfunc step(dt: f32) {}\n\nfunc main() {\n step(1.0)\n for i in 0..10 step 2 { print(i) }\n}\n";
|
||||||
|
assert_eq!(
|
||||||
|
fmt(src),
|
||||||
|
"import \"raylib.h\" { Color, RED }\nimport \"zlib.h\" link \"z\"\n\nfunc step(dt: f32) {}\n\nfunc main() {\n step(1.0)\n for i in 0..10 step 2 { print(i) }\n}\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -225,3 +225,33 @@ fn signature_help_marks_the_parameter() {
|
|||||||
assert_eq!(r.get("activeParameter").and_then(Json::as_i64), Some(1), "{r:?}");
|
assert_eq!(r.get("activeParameter").and_then(Json::as_i64), Some(1), "{r:?}");
|
||||||
assert!(r.to_string().contains("add(a: int, b: int): int"), "{r:?}");
|
assert!(r.to_string().contains("add(a: int, b: int): int"), "{r:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- C headers ----------
|
||||||
|
|
||||||
|
fn with_header(src: &str, method: &str, line: i64, ch: i64) -> Json {
|
||||||
|
let dir = std::env::temp_dir().join(format!("dino-lsp-c-{}-{}", std::process::id(), method.replace('/', "_")));
|
||||||
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
std::fs::write(dir.join("geo.h"), "typedef struct { double x, y; } Point;\n#define ORIGIN_NAME \"origin\"\n/* distance */\ndouble dist(Point a, Point b);\nint clamp(int v, int lo, int hi);\n").unwrap();
|
||||||
|
let uri = format!("file://{}/main.dino", dir.display());
|
||||||
|
let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, src), request(2, method, at(&uri, line, ch))];
|
||||||
|
msgs.extend(exit_messages());
|
||||||
|
let (out, _) = talk(msgs);
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap_or(Json::Null)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn completion_lists_what_a_c_header_has() {
|
||||||
|
let r = with_header("import \"geo.h\"\n\nfunc main() {\n geo.\n}\n", "textDocument/completion", 3, 8).to_string();
|
||||||
|
assert!(r.contains("\"dist\"") && r.contains("\"Point\"") && r.contains("\"ORIGIN_NAME\"") && r.contains("dist(a: geo.Point, b: geo.Point): float"), "{r}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hover_and_hints_for_c_functions() {
|
||||||
|
let src = "import \"geo.h\" { clamp }\n\nfunc main() {\n print(clamp(5, 0, \n}\n";
|
||||||
|
let h = with_header(src, "textDocument/hover", 3, 11);
|
||||||
|
let v = h.path(&["contents", "value"]).and_then(Json::as_str).unwrap_or("").to_string();
|
||||||
|
assert!(v.contains("func clamp(v: i32, lo: i32, hi: i32): i32") && v.contains("geo.h"), "{v}");
|
||||||
|
let s = with_header(src, "textDocument/signatureHelp", 3, 22);
|
||||||
|
assert_eq!(s.get("activeParameter").and_then(Json::as_i64), Some(2), "{s:?}");
|
||||||
|
}
|
||||||
@@ -2213,6 +2213,17 @@ fn docs_examples_run() {
|
|||||||
}
|
}
|
||||||
// the output block, if one follows right away
|
// the output block, if one follows right away
|
||||||
let expected = rest.strip_prefix("\n```text\n").and_then(|r| r.find("```").map(|e| &r[..e]));
|
let expected = rest.strip_prefix("\n```text\n").and_then(|r| r.find("```").map(|e| &r[..e]));
|
||||||
|
if code.contains("import \"") {
|
||||||
|
// C libraries: the header may be one written next to the program in the text,
|
||||||
|
// or a library that isn't installed here
|
||||||
|
match driver::compile_to_c(code) {
|
||||||
|
Err(d) if d.msg.contains("cannot find the C header") => continue,
|
||||||
|
Err(d) => panic!("{} failed:\n{code}\n{}", path.display(), d.msg),
|
||||||
|
// without printed output it is a program to look at (a window), not to run
|
||||||
|
Ok(_) if expected.is_none() => continue,
|
||||||
|
Ok(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
let o = run_source(code);
|
let o = run_source(code);
|
||||||
assert_eq!(o.code, 0, "{} failed:\n{code}\n{}", path.display(), o.stderr);
|
assert_eq!(o.code, 0, "{} failed:\n{code}\n{}", path.display(), o.stderr);
|
||||||
if let Some(want) = expected {
|
if let Some(want) = expected {
|
||||||
|
|||||||
Reference in new issue
Block a user